module Prompt def self.prompt(*args) print(*args) STDIN.gets.chomp end def self.bool(message) message = message + " (y/n): " input = prompt message input = input.strip return input.casecmp("y") == 0 || input.casecmp("yes") == 0 end def self.integer(min, max, message) input = prompt "> #{message}: " input = input.strip.to_i if input < min || input > max puts "! Specified index #{input} is outside allowed range #{min}-#{max}" return integer(min, max, message) else return input end end def self.index(options_array, message) selected_index = integer(0, options_array.size - 1, message) puts "selected_index: #{selected_index}" puts options_array.inspect return options_array[selected_index] end def self.option(options_hash, message) options_hash.each { |k,v| puts "[#{k}] #{v}" } input = prompt "> #{message}: " input = input.strip selected_option = options_hash[input] if selected_option.nil? puts "! Specified option '#{input}' is not available" return option(options_hash, message) else return selected_option end end end