Module: Dev::UI::Prompt

Defined in:
lib/dev/ui/prompt.rb

Class Method Summary collapse

Class Method Details

.ask(question, options: nil, default: nil, is_file: nil, allow_empty: true) ⇒ Object



8
9
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
35
# File 'lib/dev/ui/prompt.rb', line 8

def ask(question, options: nil, default: nil, is_file: nil, allow_empty: true)
  if (default && !allow_empty) || (options && (default || is_file))
    raise(ArgumentError, 'conflicting arguments')
  end

  if default
    puts_question("#{question} (empty = #{default})")
  elsif options
    puts_question("#{question} {{yellow:(choose with ↑ ↓ ⏎)}}")
  else
    puts_question(question)
  end

  return InteractivePrompt.call(options) if options

  loop do
    line = readline(is_file: is_file)

    if line.empty? && default
      write_default_over_empty_input(default)
      return default
    end

    if !line.empty? || allow_empty
      return line
    end
  end
end

.confirm(question) ⇒ Object



37
38
39
40
# File 'lib/dev/ui/prompt.rb', line 37

def confirm(question)
  puts_question("#{question} {{yellow:(choose with ↑ ↓ ⏎)}}")
  InteractivePrompt.call(%w(yes no)) == 'yes'
end