Module ANSI::Code
In: lib/ansi/code.rb

ANSI Codes

Ansi::Code module makes it very easy to use ANSI codes. These are especially nice for beautifying shell output.

  Ansi::Code.red + "Hello" + Ansi::Code.blue + "World"
  => "\e[31mHello\e[34mWorld"

  Ansi::Code.red{ "Hello" } + Ansi::Code.blue{ "World" }
  => "\e[31mHello\e[0m\e[34mWorld\e[0m"

IMPORTANT! Do not mixin Ansi::Code, instead use {ANSI::Mixin}.

See {ANSI::CHART} for list of all supported codes.

Methods

[]   ansi   back   code   color   colors   display   down   forward   hex_code   left   method_missing   move   random   rgb   rgb_256   rgb_code   right   style   styles   unansi   uncolor   unstyle   up  

Included Modules

Constants

Constants

PATTERN = /\e\[(\d+)m/   Regexp for matching most ANSI codes.
ENDCODE = "\e[0m"   ANSI clear code.

Public Class methods

List of primary colors.

List of primary styles.

Public Instance methods

Return ANSI code given a list of symbolic names.

Apply ANSI codes to a first argument or block value.

@example

  ansi("Valentine", :red, :on_white)

@example

  ansi(:red, :on_white){ "Valentine" }

@return [String]

  String wrapped ANSI code.
back(spaces=1)

Alias for left

Look-up code from chart, or if Integer simply pass through. Also resolves :random and :on_random.

@param codes [Array<Symbol,Integer]

  Symbols or integers to convert to ANSI code.

@return [String] ANSI code

color(*codes)

Alias for ansi

Like move but returns to original position after yielding the block.

Move cursor down a specified number of spaces.

forward(spaces=1)

Alias for right

Creates an xterm-256 color code from a CSS-style color string.

@param string [String]

  Hex string in CSS style, .e.g. `#5FA0C2`.

@param background [Boolean]

  Use `true` for background color, otherwise foreground color.

Move cursor left a specified number of spaces.

Use method missing to dispatch ANSI code methods.

Move cursor to line and column.

Provides a random primary ANSI color.

@param background [Boolean]

  Use `true` for background color, otherwise foreground color.

@return [Integer] ANSI color number

Creates an XTerm 256 color escape code from RGB value(s). The RGB value can be three arguments red, green and blue respectively each from 0 to 255, or the RGB value can be a single CSS-style hex string.

@param background [Boolean]

  Use `true` for background color, otherwise foreground color.

Given red, green and blue values between 0 and 255, this method returns the closest XTerm 256 color value.

Creates an xterm-256 color from rgb value.

@param background [Boolean]

  Use `true` for background color, otherwise foreground color.

Move cursor right a specified number of spaces.

style(*codes)

Alias for ansi

Remove ANSI codes from string or block value.

@param [String] string

  String from which to remove ANSI codes.

@return [String]

  String wrapped ANSI code.
uncolor(string=nil)

Alias for unansi

unstyle(string=nil)

Alias for unansi

Move cursor up a specified number of spaces.

[Validate]