Class: AgentsControl::Prompt

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

Overview

A line input with arrow-key command selection.

Written by hand because reline can't do this: its own list moves with Tab and Ctrl-N, arrows are already claimed by history, and there's no way to remap them just while the list is open. And picking with arrows is exactly what you'd expect from a line that starts with a slash.

Constant Summary collapse

WINDOW =

How many list rows to show at once.

8

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, completer: nil, describer: nil) ⇒ Prompt

Returns a new instance of Prompt.



16
17
18
19
20
21
22
# File 'lib/agents_control/prompt.rb', line 16

def initialize(input: $stdin, output: $stdout, completer: nil, describer: nil)
  @in = input
  @out = output
  @completer = completer || ->(_word) { [] }
  @describer = describer || ->(_item) { nil }
  @history = []
end

Instance Method Details

#read(prompt_text) ⇒ Object

Returns the entered line, or nil if input closed (Ctrl-D).



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

def read(prompt_text)
  @prompt = prompt_text
  @buffer = +""
  @cursor = 0
  @selected = 0
  @dismissed = false
  @drawn = 0
  @history_at = @history.size
  @result = nil

  render
  @in.raw { loop { break if handle(read_key) == :done } }

  @result
ensure
  finish
end