This class provides a menu using Hirb’s table helpers by default to display choices. Menu choices (syntax at Hirb::Util.choose_from_array) refer to rows. However, when in two_d mode, choices refer to specific cells by appending a ’:field’ to a choice. A field name can be an abbreviated. Menus can also have an action mode, which turns the menu prompt into a commandline that executes the choices as arguments and uses methods as actions/commands.
Constants
CHOSEN_REGEXP | = | /^(\d([^:]+)?)(?::)?(\S+)?/ | Detects valid choices and optional field/column | |
CHOSEN_ARG | = | '%s' | ||
DIRECTIONS | = | "Specify individual choices (4,7), range of choices (1-3) or all choices (*)." |
Public class methods
This method will return an array unless it’s exited by simply pressing return, which returns nil. If given a block, the block will yield if and with any menu items are chosen. All options except for the ones below are passed to render the menu.
Options:
- :helper_class
- Helper class to render menu. Helper class is expected to implement numbering given a :number option. To use a very basic menu, set this to false. Defaults to Hirb::Helpers::AutoTable.
- :prompt
- String for menu prompt. Defaults to “Choose: “.
- :ask
- Always ask for input, even if there is only one choice. Default is true.
- :directions
- Display directions before prompt. Default is true.
- :readline
- Use readline to get user input if available. Input strings are added to readline history. Default is false.
- :two_d
- Turn menu into a 2 dimensional (2D) menu by allowing user to pick values from table cells. Default is false.
- :default_field
- Default field for a 2D menu. Defaults to first field in a table.
- :action
- Turn menu into an action menu by letting user pass menu choices as an argument to a method/command. A menu choice’s place amongst other arguments is preserved. Default is false.
- :multi_action
- Execute action menu multiple times iterating over the menu choices. Default is false.
- :action_object
- Object that takes method/command calls. Default is main.
- :command
- Default method/command to call when no command given.
Examples:
>> extend Hirb::Console => self >> menu [1,2,3], :prompt=> "So many choices, so little time: " >> menu [{:a=>1, :b=>2}, {:a=>3, :b=>4}], :fields=>[:a,b], :two_d=>true)
# File lib/hirb/menu.rb, line 40 def self.render(output, options={}, &block) new(options).render(output, &block) rescue Error=>e $stderr.puts "Error: #{e.message}" end