Class: Tomo::Console::Menu

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Tomo::Colors
Defined in:
lib/tomo/console/menu.rb

Constant Summary collapse

ARROW_UP =
"\e[A"
ARROW_DOWN =
"\e[B"
RETURN =
"\r"
ENTER =
"\n"

Instance Method Summary collapse

Methods included from Tomo::Colors

enabled?

Constructor Details

#initialize(question, options, key_reader: KeyReader.new, output: $stdout) ⇒ Menu

Returns a new instance of Menu.



17
18
19
20
21
22
23
# File 'lib/tomo/console/menu.rb', line 17

def initialize(question, options, key_reader: KeyReader.new, output: $stdout)
  @question = question
  @options = options
  @position = 0
  @key_reader = key_reader
  @output = output
end

Instance Method Details

#prompt_for_selectionObject



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/tomo/console/menu.rb', line 29

def prompt_for_selection
  render_loop do |key|
    case key
    when RETURN, ENTER then break
    when ARROW_UP then move(-1)
    when ARROW_DOWN then move(1)
    else self.position = find_match_index(key)
    end
  end
  print "#{yellow(question)} #{blue(selected_option)}\n"
  selected_option
end

#selected_optionObject



25
26
27
# File 'lib/tomo/console/menu.rb', line 25

def selected_option
  options[position]
end