Module: Eco::API::Common::Session::Helpers::PromptUser

Included in:
InstanceMethods
Defined in:
lib/eco/api/common/session/helpers/prompt_user.rb

Instance Method Summary collapse

Instance Method Details

#prompt_user(question, default:, explanation: "", timeout: nil) {|response| ... } ⇒ Object

Prompts user for input with option for default on timeout.

Yields:

  • (response)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/eco/api/common/session/helpers/prompt_user.rb', line 8

def prompt_user(question, default:, explanation: "", timeout: nil)
  require 'timeout'
  response =
    if config.run_mode_remote?
      default
    else
      puts explanation
      print "#{question} "

      if timeout
        begin
          Timeout::timeout(timeout) { STDIN.gets.chop }
        rescue Timeout::Error
          default
        end
      else
        STDIN.gets.chop
      end
    end
  return response unless block_given?

  yield(response)
end