Module: Aspera::Prompt
Overview
Console input
Instance Method Summary collapse
-
#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 thatstdoutcontains only results. -
#prompt_user_input_in_list(prompt, sym_list) ⇒ Symbol
Prompt user for input in a list of symbols.
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.
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.}. 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
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 |