Module: Smol::Input

Extended by:
Input
Included in:
Command, Input
Defined in:
lib/smol/input.rb

Instance Method Summary collapse

Instance Method Details

#ask(question, default: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/smol/input.rb', line 30

def ask(question, default: nil)
  prompt = default ? "#{question} [#{default}]" : question
  Smol.output.print "#{prompt}: ".yellow
  response = Smol.input.gets&.strip

  if response.nil? || response.empty?
    default
  else
    response
  end
end

#choose(question, choices, default: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/smol/input.rb', line 42

def choose(question, choices, default: nil)
  Smol.output.puts question.yellow
  choices.each_with_index do |choice, i|
    marker = (i + 1) == default ? "*" : " "
    Smol.output.puts "#{marker} #{i + 1}) #{choice}"
  end

  Smol.output.print "choice: ".yellow
  response = Smol.input.gets&.strip

  if response.nil? || response.empty?
    default ? choices[default - 1] : nil
  else
    idx = response.to_i - 1
    idx >= 0 && idx < choices.size ? choices[idx] : nil
  end
end

#confirm(question, default: nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/smol/input.rb', line 8

def confirm(question, default: nil)
  hint = case default
         when true then "[Y/n]"
         when false then "[y/N]"
         else "[y/n]"
         end

  Smol.output.print "#{question} #{hint} ".yellow
  response = Smol.input.gets&.strip&.downcase

  case response
  when "", nil
    default
  when "y", "yes"
    true
  when "n", "no"
    false
  else
    default
  end
end