Class: AgentsControl::Menu

Inherits:
Object
  • Object
show all
Includes:
Keyboard
Defined in:
lib/agents_control/menu.rb

Overview

A list navigated with arrow keys.

Needed wherever picking is more natural than typing: settings are shown with human-readable names, and requiring the internal name instead would be a trap. See a row, hit Enter, it changes in place.

Constant Summary collapse

HINT =
"↑↓ — select · Enter — toggle · Esc — exit"

Constants included from Keyboard

Keyboard::BACKSPACE, Keyboard::CTRL_A, Keyboard::CTRL_C, Keyboard::CTRL_D, Keyboard::CTRL_E, Keyboard::CTRL_U, Keyboard::DOWN, Keyboard::ENTER, Keyboard::ESC, Keyboard::LEFT, Keyboard::RIGHT, Keyboard::TAB, Keyboard::UP

Instance Method Summary collapse

Constructor Details

#initialize(input: $stdin, output: $stdout) ⇒ Menu

Returns a new instance of Menu.



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

def initialize(input: $stdin, output: $stdout)
  @in = input
  @out = output
end

Instance Method Details

#run(title:, rows:) ⇒ Object

rows — something callable: the row list is re-read after every change, or the screen would keep showing the old value.

The block receives the index of the selected row.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/agents_control/menu.rb', line 23

def run(title:, rows:)
  @rows = rows
  @items = nil
  @selected = 0
  @drawn = 0

  @in.raw do
    loop do
      draw(title)
      break unless step(read_key) { |index| yield(index) }
    end
  end

  nil
ensure
  finish
end