Module: Db::Clone::CmdPrompt

Included in:
Base
Defined in:
lib/db/clone/cmd_prompt.rb

Instance Method Summary collapse

Instance Method Details

#ask_yes_no(question, default_to_yes = true) ⇒ Object

def ask_for_text instruction, default_text=nil

if default_text
  print "\n#{instruction} [default = \"#{default_text}\"]: "
else
  print "\n#{instruction}: "
end
text = STDIN.gets.chomp.strip
text = default_text if text.blank? && default_text
text

end



15
16
17
18
19
# File 'lib/db/clone/cmd_prompt.rb', line 15

def ask_yes_no question, default_to_yes=true
  print "\n#{question} [#{default_to_yes ? 'Yn' : 'yN'}]: "
  answer = STDIN.gets.chomp.strip.downcase
  default_to_yes ? !(answer == 'n') : answer == 'y'
end

#prompt(question, default, options = {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/db/clone/cmd_prompt.rb', line 21

def prompt question, default, options={}
  default_num = nil
  numbered_opts = options.map.with_index do |opt, idx|
    default_num = idx+1 if opt[0] == default
    [idx+1, {ret_key: opt[0], label: opt[1]}]
  end.to_h

  puts "\n"
  numbered_opts.each{|choice_num, opt| puts "  [#{choice_num.to_s.white}] #{opt[:label]}" }

  print "\n#{question} [1-#{options.length}, default=#{default_num}]: "

  selection = STDIN.gets.chomp.strip.to_i
  selection = default_num if selection.zero?

  if numbered_opts.has_key? selection
    numbered_opts[selection][:ret_key]
  else
    puts "#{'Invalid selection:'.red} #{selection}"
    prompt question, default, options
  end
end