Module: Aspera::Prompt

Extended by:
Prompt
Included in:
Cli::Parser, Prompt
Defined in:
lib/aspera/prompt.rb

Overview

Console input

Instance Method Summary collapse

Instance Method Details

#prompt_user_input(prompt, sensitive: false, validate: nil) ⇒ String

Prompt user for console input The prompt is displayed on stderr, like for sensitive input, so that stdout contains only results.

Parameters:

  • prompt (String) —

    prompt string to display

  • sensitive (Boolean) (defaults to: false) —

    whether to hide typed input

  • validate (Proc, nil) (defaults to: nil) —

    Called with the input: raises ParameterError (with the reason) if the input is not valid. Its result is not used. On a terminal, hidden input shows whether it is valid, and if not, the error is displayed and the input is asked again.

Returns:

  • (String) —

    user input stripped of trailing newline



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/aspera/prompt.rb', line 26

def prompt_user_input(prompt, sensitive: false, validate: nil)
  loop do
    line = read_input(prompt, sensitive, validate)
    begin
      validate&.call(line)
      return line
    rescue ParameterError => e
      # not a terminal: nobody to ask again
      raise unless $stdin.tty?
      $stderr.puts("#{e.message}. Try again, or Ctrl-C to stop.".red) # rubocop:disable Style/StderrPuts
    end
  end
end

#prompt_user_input_in_list(prompt, sym_list) ⇒ Symbol

Prompt user for input in a list of symbols

Parameters:

  • prompt (String) —

    prompt to display

  • sym_list (Array) —

    list of symbols to select from

Returns:

  • (Symbol) —

    selected symbol



44
45
46
47
48
49
50
# File 'lib/aspera/prompt.rb', line 44

def prompt_user_input_in_list(prompt, sym_list)
  loop do
    input = prompt_user_input(prompt).to_sym
    return input if sym_list.any? { |a| a.eql?(input) }
    $stderr.puts("No such #{prompt}: #{input}, select one of: #{sym_list.join(', ')}") # rubocop:disable Style/StderrPuts
  end
end