class Abt::Cli::Prompt
Attributes
output[R]
Public Class Methods
new(output:)
click to toggle source
# File lib/abt/cli/prompt.rb, line 8 def initialize(output:) @output = output end
Public Instance Methods
boolean(text, default: nil)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# File lib/abt/cli/prompt.rb, line 12 def text(question) output.print("#{question.strip}: ") Abt::Helpers.read_user_input end
Private Instance Methods
get_search_result(options)
click to toggle source
# File lib/abt/cli/prompt.rb, line 116 def get_search_result(options) matches = matches_for_string(text("Enter search"), options) if matches.empty? output.puts("No matches") return end output.puts("Showing the 10 first matches") if matches.size > 10 choice("Select a match", matches[0...10], nil_option: true) end
matches_for_string(string, options)
click to toggle source
# File lib/abt/cli/prompt.rb, line 127 def matches_for_string(string, options) search_string = sanitize_string(string) options.select do |option| sanitize_string(option["name"]).include?(search_string) end end
nil_option_character(nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 103 def nil_option_character(nil_option) return "q" if nil_option == true nil_option[0] end
nil_option_description(nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 109 def nil_option_description(nil_option) return "back" if nil_option == true return nil_option if nil_option.is_a?(String) nil_option[1] end
nil_option_string(nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 97 def nil_option_string(nil_option) return "" unless nil_option ", #{nil_option_character(nil_option)}: #{nil_option_description(nil_option)}" end
options_info(options, nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 89 def options_info(options, nil_option) str = "(" str += options.length > 1 ? "1-#{options.length}" : "1" str += nil_option_string(nil_option) str += "): " str end
print_options(options)
click to toggle source
# File lib/abt/cli/prompt.rb, line 58 def print_options(options) options.each_with_index do |option, index| output.puts "(#{index + 1}) #{option['name']}" end end
prompt_valid_option_number(options, nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 74 def prompt_valid_option_number(options, nil_option) output.print(options_info(options, nil_option)) input = Abt::Helpers.read_user_input return nil if nil_option && input == nil_option_character(nil_option) option_number = input.to_i return option_number if (1..options.length).cover?(option_number) output.puts "Invalid selection" # Prompt again if the selection was invalid prompt_valid_option_number(options, nil_option) end
sanitize_string(string)
click to toggle source
# File lib/abt/cli/prompt.rb, line 135 def sanitize_string(string) string.downcase.gsub(/[^\w]/, "") end
select_option(options, nil_option)
click to toggle source
# File lib/abt/cli/prompt.rb, line 64 def select_option(options, nil_option) number = prompt_valid_option_number(options, nil_option) return nil if number.nil? option = options[number - 1] output.puts "Selected: (#{number}) #{option['name']}" option end