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/items.rb
2017-07-22 18:46:50 +00:00

59 lines
1.2 KiB
Ruby

# frozen_string_literal: true
module Widgets
class Menu < Container
class Items < Base
ITEMS = [
'Foo menu item',
'Bar Car menu item',
'Hello, World!',
].freeze
SIDE_PADDING = 3
attr_reader :active
def initialize(*)
super
@active = 0
end
def trigger(event)
case event
when Events::Panel::Up
up
when Events::Panel::Down
down
end
end
def draw
ITEMS.each_with_index do |item, index|
draw_item index, item
end
end
def draw_item(index, name)
Style.default.public_send(index == active ? :active_menu_item : :menu_item) do
setpos SIDE_PADDING, 4 * index + 0
addstr ' ' * (width - 2 * SIDE_PADDING)
setpos SIDE_PADDING, 4 * index + 1
addstr " #{name}".ljust width - 2 * SIDE_PADDING
setpos SIDE_PADDING, 4 * index + 2
addstr ' ' * (width - 2 * SIDE_PADDING)
end
end
def up
@active -= 1 if active.positive?
end
def down
@active += 1 if active < ITEMS.count - 1
end
end
end
end