Module: PiotrbCliUtils::CmdLoop

Defined in:
lib/piotrb_cli_utils/cmd_loop.rb

Instance Method Summary collapse

Instance Method Details

#run_cmd_loop(prompt = '=> ') ⇒ Object

Parameters:

  • prompt (String, Proc) (defaults to: '=> ')

    The prompt to use for the command loop. If a proc is given, it will be called for each time the prompt is displayed.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/piotrb_cli_utils/cmd_loop.rb', line 8

def run_cmd_loop(prompt = '=> ')
  reader = TTY::Reader.new(interrupt: :noop)
  reader.on(:keyctrl_c, :keyescape) do
    return :abort
  end

  reader.on(:keyctrl_d) do
    return :eof
  end

  catch(:stop) do
    loop do
      actual_prompt = prompt.respond_to?(:call) ? prompt.call : prompt
      line = reader.read_line(actual_prompt)
      line.strip!
      yield(line)
    end
  end || :stopped
end