Class: CLAide::ANSI

Inherits:
Object
  • Object
show all
Extended by:
Cursor, Graphics
Defined in:
lib/claide/ansi.rb,
lib/claide/ansi/cursor.rb,
lib/claide/ansi/graphics.rb,
lib/claide/ansi/string_escaper.rb

Overview

Provides support for ANSI Escape sequences

For more information see:

This functionality has been inspired and derived from the following gems:

  • colored
  • colorize

Defined Under Namespace

Modules: Cursor, Graphics Classes: StringEscaper

Constant Summary collapse

TEXT_ATTRIBUTES =

Returns The text attributes codes by their English name.

Returns:

  • (Hash{Symbol => Fixnum})

    The text attributes codes by their English name.

{
  :bold       => 1,
  :underline  => 4,
  :blink      => 5,
  :reverse    => 7,
  :hidden     => 8,
}
TEXT_DISABLE_ATTRIBUTES =

Returns The codes to disable a text attribute by their name.

Returns:

  • (Hash{Symbol => Fixnum})

    The codes to disable a text attribute by their name.

{
  :bold       => 21,
  :underline  => 24,
  :blink      => 25,
  :reverse    => 27,
  :hidden     => 28,
}
RESET_SEQUENCE =

Return [String] The escape sequence to reset the graphics.

"\e[0m"
COLORS =

Returns The colors codes by their English name.

Returns:

  • (Hash{Symbol => Fixnum})

    The colors codes by their English name.

{
  :black      => 0,
  :red        => 1,
  :green      => 2,
  :yellow     => 3,
  :blue       => 4,
  :magenta    => 5,
  :cyan       => 6,
  :white      => 7,
}
DEFAULT_FOREGROUND_COLOR =

Return [String] The escape sequence for the default foreground color.

"\e[39m"
DEFAULT_BACKGROUND_COLOR =

Return [String] The escape sequence for the default background color.

"\e[49m"

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Cursor

erase_display, erase_line, move_cursor, restore_cursor_position, save_cursor_position, set_cursor_position

Methods included from Graphics

background_color, background_color_256, foreground_color, foreground_color_256, graphics_mode, text_attribute

Class Attribute Details

.disabledBool

original string. This method is intended to offer a central location where to disable ANSI logic without needed to implement conditionals across the code base of clients.

Examples:


"example".ansi.yellow #=> "\e[33mexample\e[39m"
ANSI.disabled = true
"example".ansi.yellow #=> "example"

Returns:

  • (Bool)

    Wether the string mixin should be disabled to return the



35
36
37
# File 'lib/claide/ansi.rb', line 35

def disabled
  @disabled
end

Class Method Details

.code_for_key(key, map) ⇒ Fixnum

Returns The code of a key given the map.

Parameters:

  • key (Symbol)

    The key for which the code is needed.

  • map (Hash{Symbol => Fixnum})

    A hash which associates each code to each key.

Returns:

  • (Fixnum)

    The code of a key given the map.

Raises:

  • If the key is not provided.

  • If the key is not present in the map.



96
97
98
99
100
101
102
103
104
105
# File 'lib/claide/ansi.rb', line 96

def self.code_for_key(key, map)
  unless key
    raise ArgumentError, 'A key must be provided'
  end
  code = map[key]
  unless code
    raise ArgumentError, "Unsupported key: `#{key}`"
  end
  code
end