Module: Dex::UI::Prompt

Defined in:
lib/dex/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/dex/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})")
  else
    puts_question(question)
  end

  if options
    return ask_options(options)
  end

  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
41
42
43
44
45
46
# File 'lib/dex/ui/prompt.rb', line 37

def confirm(question)
  puts_question(question + ' {{yellow:[y/n]}}')

  loop do
    line = readline(is_file: false)
    char = line[0].downcase
    return true  if char == 'y'
    return false if char == 'n'
  end
end