Class: App::Menu

Inherits:
Object
  • Object
show all
Defined in:
lib/app/menu.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#indexObject (readonly)

Returns the value of attribute index.



14
15
16
# File 'lib/app/menu.rb', line 14

def index
  @index
end

#itemsObject (readonly)

Returns the value of attribute items.



12
13
14
# File 'lib/app/menu.rb', line 12

def items
  @items
end

#titleObject (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_commandObject



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