Module: EasyAppHelper::Input

Defined in:
lib/easy_app_helper/input.rb

Constant Summary collapse

DEFAULT_CONFIRMATION_CHOICES =
{
    true => %w(Yes y),
    false => %w(No n)
}

Instance Method Summary collapse

Instance Method Details

#get_user_confirmation(choices: DEFAULT_CONFIRMATION_CHOICES, default_choice: 'No', prompt: 'Are you sure ?', strict: false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/easy_app_helper/input.rb', line 10

def get_user_confirmation(choices: DEFAULT_CONFIRMATION_CHOICES,
                          default_choice: 'No',
                          prompt: 'Are you sure ?',
                          strict: false)

  raise 'Invalid choices !' unless choices.is_a? Hash
  values = choices.values.flatten
  raise "Invalid default choice '#{default_choice}' !" unless values.include? default_choice
  return true if EasyAppHelper.config[:auto]
  full_prompt = '%s (%s): ' % [prompt, choices_string(values, default_choice)]
  STDOUT.print full_prompt
  STDOUT.flush
  input = nil
  until values.include? input
    input = STDIN.gets.chomp
    input = default_choice if input.nil? || input.empty?
    unless strict
      input = default_choice unless values.include? input
    end
  end
  choices.each_pair do |res, possible_choices|
    return res if possible_choices.include? input
  end
  raise 'Something wrong happened !'
end

#get_user_input(prompt, default = nil) ⇒ Object



37
38
39
40
41
42
# File 'lib/easy_app_helper/input.rb', line 37

def get_user_input(prompt, default=nil)
  full_prompt = (default.nil? or default.empty?) ? "#{prompt}: " : "#{prompt} (default: #{default}): "
  STDOUT.print full_prompt
  STDOUT.flush
  STDIN.gets.chomp
end