Class: Abt::Cli::Prompt

Inherits:
Object
  • Object
show all
Defined in:
lib/abt/cli/prompt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output:) ⇒ Prompt

Returns a new instance of Prompt.



8
9
10
# File 'lib/abt/cli/prompt.rb', line 8

def initialize(output:)
  @output = output
end

Instance Attribute Details

#outputObject (readonly)

Returns the value of attribute output.



6
7
8
# File 'lib/abt/cli/prompt.rb', line 6

def output
  @output
end

Instance Method Details

#boolean(text, default: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/abt/cli/prompt.rb', line 17

def boolean(text, default: nil)
  choices = [default == true ? "Y" : "y",
             default == false ? "N" : "n"].join("/")

  output.print("#{text} (#{choices}): ")

  input = Abt::Helpers.read_user_input.downcase

  return true if input == "y"
  return false if input == "n"
  return default if input.empty? && !default.nil?

  output.puts "Invalid choice"
  boolean(text, default: default)
end

#choice(text, options, nil_option: false) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/abt/cli/prompt.rb', line 33

def choice(text, options, nil_option: false)
  output.puts "#{text.strip}:"

  if options.length.zero?
    raise Abort, "No available options" unless nil_option

    output.puts "No available options"
    return nil
  end

  print_options(options)
  select_option(options, nil_option)
end

#search(text, options) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/abt/cli/prompt.rb', line 47

def search(text, options)
  output.puts text

  loop do
    choice = get_search_result(options)
    break choice unless choice.nil?
  end
end

#text(question) ⇒ Object



12
13
14
15
# File 'lib/abt/cli/prompt.rb', line 12

def text(question)
  output.print("#{question.strip}: ")
  Abt::Helpers.read_user_input
end