class MenuCommander::Menu
Attributes
config[R]
Public Class Methods
new(config)
click to toggle source
# File lib/menu_commander/menu.rb, line 8 def initialize(config) config = ExtendedYAML.load config if config.is_a? String @config = config end
Public Instance Methods
call(menu=nil)
click to toggle source
# File lib/menu_commander/menu.rb, line 13 def call(menu=nil) menu ||= config['menu'] response = select menu response = combine_commands response if response.is_a? Array history << menu response.is_a?(String) ? evaluate(response) : call(response) rescue MenuNavigation => e puts "\n\n" call e.menu end
options()
click to toggle source
# File lib/menu_commander/menu.rb, line 27 def options @options ||= MenuOptions.new(config['options']) end
Private Instance Methods
apply_suffix(choices)
click to toggle source
# File lib/menu_commander/menu.rb, line 127 def apply_suffix(choices) choices.map do |key, value| key = "#{key}#{options.submenu_marker}" if value.is_a? Hash [key, value] end.to_h end
args()
click to toggle source
# File lib/menu_commander/menu.rb, line 58 def args config['args'] end
ask(title)
click to toggle source
# File lib/menu_commander/menu.rb, line 117 def ask(title) prompt.ask "> #{title}:" rescue TTY::Reader::InputInterrupt # :nocov: raise ExitMenu # :nocov: end
combine_commands(command_array)
click to toggle source
# File lib/menu_commander/menu.rb, line 37 def combine_commands(command_array) if command_array.size == 1 command_array.first else command_array.map { |cmd| "(#{cmd})" }.join ' && ' end end
enable_filter?(choices)
click to toggle source
# File lib/menu_commander/menu.rb, line 134 def enable_filter?(choices) if options.filter === true true elsif options.filter === false false elsif options.filter.is_a? Numeric choices.size > options.filter else choices.size > options.page_size end end
evaluate(response)
click to toggle source
# File lib/menu_commander/menu.rb, line 45 def evaluate(response) params = {} placeholders(response).each do |key| params[key.to_sym] = get_user_response key end response % params end
get_opts(key)
click to toggle source
# File lib/menu_commander/menu.rb, line 86 def get_opts(key) opts = args ? args[key] : nil opts.is_a?(String) ? `#{opts}`.split("\n") : opts end
get_opts_type(opts)
click to toggle source
# File lib/menu_commander/menu.rb, line 76 def get_opts_type(opts) if !opts :free_text elsif options.auto_select and opts.is_a? Array and opts.size == 1 :static else :menu end end
get_user_response(key)
click to toggle source
# File lib/menu_commander/menu.rb, line 62 def get_user_response(key) opts = get_opts key opts_type = get_opts_type opts case opts_type when :free_text ask(key) when :static opts.first when :menu select(opts, key) end end
handle_keypress(event)
click to toggle source
# File lib/menu_commander/menu.rb, line 101 def handle_keypress(event) case event.key.name when :page_up parent_menu = history.pop raise MenuNavigation.new(parent_menu) if parent_menu when :home home_menu = history.first if home_menu @history = [] raise MenuNavigation.new(home_menu) end end end
history()
click to toggle source
# File lib/menu_commander/menu.rb, line 33 def history @history ||= [] end
placeholders(template)
click to toggle source
# File lib/menu_commander/menu.rb, line 54 def placeholders(template) template.scan(/%{([^}]+)}/).flatten.uniq end
prompt()
click to toggle source
# File lib/menu_commander/menu.rb, line 91 def prompt @prompt ||= prompt! end
prompt!()
click to toggle source
# File lib/menu_commander/menu.rb, line 95 def prompt! result = TTY::Prompt.new result.on(:keypress) { |event| handle_keypress event } result end
select(choices, title=nil)
click to toggle source
# File lib/menu_commander/menu.rb, line 146 def select(choices, title=nil) title = title ? "#{options.title_marker} #{title}:" : options.title_marker choices = apply_suffix choices if options.submenu_marker and choices.is_a? Hash select! choices, title end
select!(choices, title)
click to toggle source
# File lib/menu_commander/menu.rb, line 152 def select!(choices, title) prompt.select title, choices, symbols: { marker: options.select_marker }, per_page: options.page_size, filter: enable_filter?(choices) rescue TTY::Reader::InputInterrupt # :nocov: raise ExitMenu # :nocov: end