Class HighLine
In: lib/highline/style.rb
lib/highline/color_scheme.rb
lib/highline/question.rb
lib/highline/simulate.rb
lib/highline/string_extensions.rb
lib/highline/system_extensions.rb
lib/highline/menu.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

Included Modules

HighLine::SystemExtensions

Classes and Modules

Module HighLine::StringExtensions
Module HighLine::SystemExtensions
Class HighLine::ColorScheme
Class HighLine::Menu
Class HighLine::Question
Class HighLine::QuestionError
Class HighLine::SampleColorScheme
Class HighLine::Simulate
Class HighLine::String
Class HighLine::Style

Constants

VERSION = "1.6.15".freeze   The version of the installed library.
ERASE_LINE_STYLE = Style.new(:name=>:erase_line, :builtin=>true, :code=>"\e[K")   Embed in a String to clear all previous ANSI sequences. This MUST be done before the program exits!
ERASE_CHAR_STYLE = Style.new(:name=>:erase_char, :builtin=>true, :code=>"\e[P")
CLEAR_STYLE = Style.new(:name=>:clear, :builtin=>true, :code=>"\e[0m")
RESET_STYLE = Style.new(:name=>:reset, :builtin=>true, :code=>"\e[0m")
BOLD_STYLE = Style.new(:name=>:bold, :builtin=>true, :code=>"\e[1m")
DARK_STYLE = Style.new(:name=>:dark, :builtin=>true, :code=>"\e[2m")   for example bold black. Bold without a color displays the system-defined bold color (e.g. red on Mac iTerm)
UNDERLINE_STYLE = Style.new(:name=>:underline, :builtin=>true, :code=>"\e[4m")
UNDERSCORE_STYLE = Style.new(:name=>:underscore, :builtin=>true, :code=>"\e[4m")
BLINK_STYLE = Style.new(:name=>:blink, :builtin=>true, :code=>"\e[5m")
REVERSE_STYLE = Style.new(:name=>:reverse, :builtin=>true, :code=>"\e[7m")
CONCEALED_STYLE = Style.new(:name=>:concealed, :builtin=>true, :code=>"\e[8m")
STYLES = %w{CLEAR RESET BOLD DARK UNDERLINE UNDERSCORE BLINK REVERSE CONCEALED}
BLACK_STYLE = Style.new(:name=>:black, :builtin=>true, :code=>"\e[30m", :rgb=>[ 0, 0, 0])   These RGB colors are approximate; see en.wikipedia.org/wiki/ANSI_escape_code
RED_STYLE = Style.new(:name=>:red, :builtin=>true, :code=>"\e[31m", :rgb=>[128, 0, 0])
GREEN_STYLE = Style.new(:name=>:green, :builtin=>true, :code=>"\e[32m", :rgb=>[ 0,128, 0])
BLUE_STYLE = Style.new(:name=>:blue, :builtin=>true, :code=>"\e[34m", :rgb=>[ 0, 0,128])
YELLOW_STYLE = Style.new(:name=>:yellow, :builtin=>true, :code=>"\e[33m", :rgb=>[128,128, 0])
MAGENTA_STYLE = Style.new(:name=>:magenta, :builtin=>true, :code=>"\e[35m", :rgb=>[128, 0,128])
CYAN_STYLE = Style.new(:name=>:cyan, :builtin=>true, :code=>"\e[36m", :rgb=>[ 0,128,128])
WHITE_STYLE = Style.new(:name=>:white, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])   On Mac OSX Terminal, white is actually gray
GRAY_STYLE = Style.new(:name=>:gray, :builtin=>true, :code=>"\e[37m", :rgb=>[192,192,192])   Alias for WHITE, since WHITE is actually a light gray on Macs
NONE_STYLE = Style.new(:name=>:none, :builtin=>true, :code=>"\e[38m", :rgb=>[ 0, 0, 0])   On Mac OSX Terminal, this is black foreground, or bright white background. Also used as base for RGB colors, if available
BASIC_COLORS = %w{BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE GRAY NONE}
COLORS = colors

Attributes

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

Public Class methods

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.

In case you just want the color code, without the embedding and the CLEAR

Returns the current color scheme.

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

For RGB colors:

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

For checking if the current version of HighLine supports RGB colors Usage: HighLine.supports_rgb_color? rescue false # rescue for compatibility with older versions Note: color usage also depends on HighLine.use_color being set

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

Returns true if HighLine is currently tracking EOF for input.

Remove color codes from a string

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

Returns true if HighLine is currently using color escapes.

Returns true if HighLine is currently using a color scheme.

Public Instance methods

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.

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.

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.

Works as an instance method, same as the class method

Works as an instance method, same as the class method

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.
:uneven_columns_across:Like :columns_across but each column is sized independently.
:uneven_columns_down:Like :columns_down but each column is sized independently.
: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.

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

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

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.

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.

Works as an instance method, same as the class method

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]