class Coals::Menu

Attributes

selection[R]

Public Class Methods

new(options:, prompt: nil, title: '') click to toggle source

Show a menu of options in a loop until the user makes a selection. Return the selection value

@param menu {Array} - list of options to choose from

# File lib/coals/menu.rb, line 10
def initialize(options:, prompt: nil, title: '')
  @select = nil
  @options = options
  @title = title
  @prompt = prompt
  show_menu until @selection
end

Private Instance Methods

format_menu_options() click to toggle source
# File lib/coals/menu.rb, line 50
def format_menu_options
  @options.keys.each_with_index.inject('') do |result, (option, i)|
    result + "\n#{i + 1}.".ljust(4) + option.ljust(30)
  end
end
integer_input() click to toggle source
# File lib/coals/menu.rb, line 27
def integer_input
  print "\nChoose an option: ".bold
  option_index = raw_input.to_i - 1
  @selection = @options.values[option_index]
end
min_column_width() click to toggle source
# File lib/coals/menu.rb, line 46
def min_column_width
  @options.keys.max_by(&:length).length
end
print(str) click to toggle source
println(str) click to toggle source
# File lib/coals/menu.rb, line 38
def println(str)
  $stdout.puts str
end
raw_input() click to toggle source
# File lib/coals/menu.rb, line 33
def raw_input
  input = $stdin.gets.chomp
  input =~ /quit|exit|q/ ? abort('Goodbye 👋') : input
end
show_menu() click to toggle source
# File lib/coals/menu.rb, line 20
def show_menu
  menu = "\nCOALS -- #{@title}\n".brown
  menu += format_menu_options
  println menu
  integer_input
end