Class: App::Menu
- Inherits:
-
Object
- Object
- App::Menu
- Defined in:
- lib/app/menu.rb
Instance Attribute Summary collapse
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#items ⇒ Object
readonly
Returns the value of attribute items.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Instance Method Summary collapse
-
#initialize(title, items) ⇒ Menu
constructor
A new instance of Menu.
- #select_command ⇒ Object
- #select_item(index) ⇒ Object
Constructor Details
#initialize(title, items) ⇒ Menu
Returns a new instance of Menu.
16 17 18 19 20 21 22 23 |
# File 'lib/app/menu.rb', line 16 def initialize(title, items) super() @index = 0 @title = title @items = items.map.with_index do |item, i| item.is_a?(String) ? MenuItem.new(item, nil, i == 0) : MenuItem.new(item[:command], item[:hint], i == 0) end end |
Instance Attribute Details
#index ⇒ Object (readonly)
Returns the value of attribute index.
14 15 16 |
# File 'lib/app/menu.rb', line 14 def index @index end |
#items ⇒ Object (readonly)
Returns the value of attribute items.
12 13 14 |
# File 'lib/app/menu.rb', line 12 def items @items end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
13 14 15 |
# File 'lib/app/menu.rb', line 13 def title @title end |
Instance Method Details
#select_command ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/app/menu.rb', line 31 def select_command display interaction = Remedy::Interaction.new interaction.loop do |key| if key.nil? break end case key.to_s.to_sym when :down index = @index + 1 index = 0 if index >= @items.size select_item(index) when :up index = @index - 1 index = 0 if index < 0 select_item(index) when :control_m return @items[@index].command end end end |
#select_item(index) ⇒ Object
25 26 27 28 29 |
# File 'lib/app/menu.rb', line 25 def select_item(index) @index = index @items.each_with_index { |item, i| item.selected = (i == index) } display end |