Archived
1
0
Fork 0
This repository has been archived on 2023-03-27. You can view files and clone it, but cannot push or open issues or pull requests.
cli-old/lib/widgets/menu.rb

77 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Widgets
class Menu < Base
2017-07-24 18:34:50 -04:00
def active
props[:active]
end
2017-07-24 18:34:50 -04:00
def top
props[:top]
end
2017-07-24 18:34:50 -04:00
def items
props[:items]
end
def draw
items[top...(top + height)].each_with_index.each do |item, offset|
index = top + offset
setpos 0, offset
if item[:online]
Style.default.online_mark window do
addstr '*'
end
else
addstr 'o'
end
addstr ' '
Style.default.public_send(index == active && focused ? :selection : :text, window) do
if item[:name].length <= width - 2
addstr item[:name].ljust width - 2
else
addstr "#{item[:name][0...width - 5]}..."
end
end
end
end
def trigger(event)
case event
when Events::Panel::Up
up
when Events::Panel::Down
down
end
end
def up
@active -= 1
update
end
def down
@active += 1
update
end
def update
if active.negative?
@active = items.count - 1
elsif active >= items.count
@active = 0
end
if active < top
@top = active
elsif active >= top + height
@top = active - height + 1
end
end
end
end