class Hirb::Menu

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_ARG
CHOSEN_REGEXP

Detects valid choices and optional field/column

DIRECTIONS

Public Class Methods

render(output, options={}, &block) click to toggle source

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.

:reopen

Reopens $stdin with given file or with /dev/tty when set to true. Use when $stdin is already reading in piped data.

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 42
def self.render(output, options={}, &block)
  new(options).render(output, &block)
rescue Error=>e
  $stderr.puts "Error: #{e.message}"
end