Class: HighLine

Class | HighLine
In: | lib/highline/color_scheme.rb lib/highline/menu.rb lib/highline/question.rb lib/highline/system_extensions.rb lib/highline.rb
Parent: | Object

A HighLine object is a "high-level line oriented" shell over an input and an output stream. HighLine simplifies common console interaction, effectively replacing puts() and gets(). User code can simply specify the question to ask and any details about user interaction, then leave the rest of the work to HighLine. When HighLine.ask() returns, you‘ll have the answer you requested, even if HighLine had to ask many times, validate results, perform range checking, convert types, etc.

### Methods

agree ask choose color color_scheme color_scheme= list new output_cols output_rows page_at= say track_eof= track_eof? use_color= use_color? using_color_scheme? wrap_at=

### Included Modules

HighLine::SystemExtensions

### Classes and Modules

Module HighLine::SystemExtensions
Class HighLine::ColorScheme
Class HighLine::Menu
Class HighLine::Question
Class HighLine::QuestionError
Class HighLine::SampleColorScheme

### Constants

VERSION | = | "1.5.1".freeze |  | The version of the installed library.
CLEAR | = | "\e[0m" |  | Embed in a String to clear all previous ANSI sequences. This MUST be done before the program exits!
RESET | = | CLEAR |  | An alias for CLEAR.
ERASE_LINE | = | "\e[K" |  | Erase the current line of terminal output.
ERASE_CHAR | = | "\e[P" |  | Erase the character under the cursor.
BOLD | = | "\e[1m" |  | The start of an ANSI bold sequence.
DARK | = | "\e[2m" |  | The start of an ANSI dark sequence. (Terminal support uncommon.)
UNDERLINE | = | "\e[4m" |  | The start of an ANSI underline sequence.
UNDERSCORE | = | UNDERLINE |  | An alias for UNDERLINE.
BLINK | = | "\e[5m" |  | The start of an ANSI blink sequence. (Terminal support uncommon.)
REVERSE | = | "\e[7m" |  | The start of an ANSI reverse sequence.
CONCEALED | = | "\e[8m" |  | The start of an ANSI concealed sequence. (Terminal support uncommon.)
BLACK | = | "\e[30m" |  | Set the terminal‘s foreground ANSI color to black.
RED | = | "\e[31m" |  | Set the terminal‘s foreground ANSI color to red.
GREEN | = | "\e[32m" |  | Set the terminal‘s foreground ANSI color to green.
YELLOW | = | "\e[33m" |  | Set the terminal‘s foreground ANSI color to yellow.
BLUE | = | "\e[34m" |  | Set the terminal‘s foreground ANSI color to blue.
MAGENTA | = | "\e[35m" |  | Set the terminal‘s foreground ANSI color to magenta.
CYAN | = | "\e[36m" |  | Set the terminal‘s foreground ANSI color to cyan.
WHITE | = | "\e[37m" |  | Set the terminal‘s foreground ANSI color to white.
ON_BLACK | = | "\e[40m" |  | Set the terminal‘s background ANSI color to black.
ON_RED | = | "\e[41m" |  | Set the terminal‘s background ANSI color to red.
ON_GREEN | = | "\e[42m" |  | Set the terminal‘s background ANSI color to green.
ON_YELLOW | = | "\e[43m" |  | Set the terminal‘s background ANSI color to yellow.
ON_BLUE | = | "\e[44m" |  | Set the terminal‘s background ANSI color to blue.
ON_MAGENTA | = | "\e[45m" |  | Set the terminal‘s background ANSI color to magenta.
ON_CYAN | = | "\e[46m" |  | Set the terminal‘s background ANSI color to cyan.
ON_WHITE | = | "\e[47m" |  | Set the terminal‘s background ANSI color to white.

### Attributes

page_at | [R] | The current row setting for paging output.
wrap_at | [R] | The current column setting for wrapping output.

### Public Class methods

color_scheme()

Returns the current color scheme.

color_scheme=( setting )

Pass ColorScheme to setting to turn set a HighLine color scheme.

new( input = $stdin, output = $stdout, wrap_at = nil, page_at = nil )

Create an instance of HighLine, connected to the streams input and output.

track_eof=( setting )

Pass `false` to setting to turn off HighLine‘s EOF tracking.

track_eof?()

Returns true if HighLine is currently tracking EOF for input.

use_color=( setting )

Pass `false` to setting to turn off HighLine‘s color escapes.

use_color?()

Returns true if HighLine is currently using color escapes.

using_color_scheme?()

Returns `true` if HighLine is currently using a color scheme.

### Public Instance methods

agree( yes_or_no_question, character = nil ) {|q if block_given?| ...}

A shortcut to HighLine.ask() a question that only accepts "yes" or "no" answers ("y" and "n" are allowed) and returns `true` or `false` (`true` for "yes"). If provided a `true` value, character will cause HighLine to fetch a single character response. A block can be provided to further configure the question as in HighLine.ask()

Raises EOFError if input is exhausted.

ask( question, answer_type = String ) {|question| ...}

This method is the primary interface for user input. Just provide a question to ask the user, the answer_type you want returned, and optionally a code block setting up details of how you want the question handled. See HighLine.say() for details on the format of question, and HighLine::Question for more information about answer_type and what‘s valid in the code block.

If `@question` is set before ask() is called, parameters are ignored and that object (must be a HighLine::Question) is used to drive the process instead.

Raises EOFError if input is exhausted.

choose( *items, &details )

This method is HighLine‘s menu handler. For simple usage, you can just pass all the menu items you wish to display. At that point, choose() will build and display a menu, walk the user through selection, and return their choice amoung the provided items. You might use this in a case statement for quick and dirty menus.

However, choose() is capable of much more. If provided, a block will be passed a HighLine::Menu object to configure. Using this method, you can customize all the details of menu handling from index display, to building a complete shell-like menuing system. See HighLine::Menu for all the methods it responds to.

Raises EOFError if input is exhausted.

color( string, *colors )

This method provides easy access to ANSI color sequences, without the user needing to remember to CLEAR at the end of each sequence. Just pass the string to color, followed by a list of colors you would like it to be affected by. The colors can be HighLine class constants, or symbols (:blue for BLUE, for example). A CLEAR will automatically be embedded to the end of the returned String.

This method returns the original string unchanged if HighLine::use_color? is `false`.

list( items, mode = :rows, option = nil )

This method is a utility for quickly and easily laying out lists. It can be accessed within ERb replacements of any text that will be sent to the user.

The only required parameter is items, which should be the Array of items to list. A specified mode controls how that list is formed and option has different effects, depending on the mode. Recognized modes are:

`:columns_across`: | items will be placed in columns, flowing from left to right. If given, option is the number of columns to be used. When absent, columns will be determined based on wrap_at or a default of 80 characters.
`:columns_down`: | Identical to `:columns_across`, save flow goes down.
`:inline`: | All items are placed on a single line. The last two items are separated by option or a default of " or ". All other items are separated by ", ".
`:rows`: | The default mode. Each of the items is placed on it‘s own line. The option parameter is ignored in this mode.

Each member of the items Array is passed through ERb and thus can contain their own expansions. Color escape expansions do not contribute to the final field width.

output_cols()

Returns the number of columns for the console, or a default it they cannot be determined.

output_rows()

Returns the number of rows for the console, or a default if they cannot be determined.

page_at=( setting )

Set to an integer value to cause HighLine to page output lines over the indicated line limit. When `nil`, the default, no paging occurs. If set to `:auto`, HighLine will attempt to determing the rows available for the `@output` or use a sensible default.

say( statement )

The basic output method for HighLine objects. If the provided statement ends with a space or tab character, a newline will not be appended (output will be flush()ed). All other cases are passed straight to Kernel.puts().

The statement parameter is processed as an ERb template, supporting embedded Ruby code. The template is evaluated with a binding inside the HighLine instance, providing easy access to the ANSI color constants and the HighLine.color() method.

wrap_at=( setting )

Set to an integer value to cause HighLine to wrap output lines at the indicated character limit. When `nil`, the default, no wrapping occurs. If set to `:auto`, HighLine will attempt to determing the columns available for the `@output` or use a sensible default.

[Validate]