Class: ANSI

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/ansi.rb

Overview

The ANSI class is responsible for generating ANSI codes for text styling in terminals. It allows the combination of multiple style and color codes.

Examples:

ansi = ANSI.new(:bold)
puts ansi.to_s # => "\e[1m"
ansi = ANSI.new([:bold, :red])
puts ansi.to_s # => "\e[1;31m"

Constant Summary collapse

CODES_HASH =

Hash that maps style and color names to their respective ANSI codes.

{
  reset_all: 0,

  # Effects
  bold: 1,
  faint: 2,
  italic: 3,
  underlined: 4,
  blinking: 5,
  inverse: 7,
  hidden: 8,
  strike: 9,
  plain: 21,

  # Remove Effects
  remove_bold: 22,
  remove_faint: 22,
  remove_italic: 23,
  remove_underlined: 24,
  remove_blinking: 25,
  remove_inverse: 27,
  remove_hidden: 28,
  remove_strike: 29,
  remove_plain: 24,

  # Colors
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37,
  reset_color: 39,
  rgb_format: [38, 2],
  numeric_format: [38, 5],

  # Background Colors
  background_black: 40,
  background_red: 41,
  background_green: 42,
  background_yellow: 43,
  background_blue: 44,
  background_magenta: 45,
  background_cyan: 46,
  background_white: 47,
  background_rgb_format: [48, 2],
  background_numeric_format: [48, 5],
  reset_background: 49
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code) ⇒ ANSI

Initializes a new instance of the ANSI class.

Parameters:

  • code (Array<Symbol>, Symbol, String)

    One or more ANSI codes represented as symbols, integers, or strings.



75
76
77
78
79
80
81
82
83
# File 'lib/utils/ansi.rb', line 75

def initialize(code)
  @codes = if code.is_a?(Array)
             ANSI.normalize_array_codes(code)
           elsif code.is_a?(Symbol)
             [CODES_HASH[code]]
           else
             ANSI.normalize_array_codes(code.split(';'))
           end
end

Instance Attribute Details

#codesArray<Integer> (readonly)

Returns the ANSI codes stored in the instance.

Returns:

  • (Array<Integer>)

    the ANSI codes stored in the instance.



70
71
72
# File 'lib/utils/ansi.rb', line 70

def codes
  @codes
end

Class Method Details

.normalize_array_codes(array) ⇒ Array<Integer>

Normalizes an array of codes, converting symbols and strings to their respective ANSI codes.

Parameters:

  • array (Array<Symbol, String, Integer>)

    An array of codes to be normalized.

Returns:

  • (Array<Integer>)

    The normalized array of ANSI codes.



115
116
117
118
119
120
121
122
123
124
# File 'lib/utils/ansi.rb', line 115

def self.normalize_array_codes(array)
  array.map do |value|
    next normalize_array_codes(value) if value.is_a?(Array)
    next value if value.is_a?(Integer)
    next CODES_HASH[value] if value.is_a?(Symbol)
    next value.to_i if value.match?(CONSTANTS::ONLY_DIGITS_REGEX)

    CODES_HASH[value.to_sym]
  end
end

Instance Method Details

#append(ansi) ⇒ ANSI

Adds ANSI codes from another instance of the ANSI class.

Parameters:

  • ansi (ANSI)

    The instance of the ANSI class whose codes will be added.

Returns:

  • (ANSI)

    A new instance of the ANSI class with the combined codes.

Raises:

  • (RuntimeError)

    If the argument is not an instance of the ANSI class.



90
91
92
93
94
# File 'lib/utils/ansi.rb', line 90

def append(ansi)
  raise 'Needs be an instance of ANSI' unless ansi.is_a?(ANSI)

  ANSI.new(@codes.union(ansi.codes))
end

#append!(ansi) ⇒ void

This method returns an undefined value.

Adds ANSI codes from another instance of the ANSI class and updates the current instance.

Parameters:

  • ansi (ANSI)

    The instance of the ANSI class whose codes will be added.



100
101
102
# File 'lib/utils/ansi.rb', line 100

def append!(ansi)
  @codes = append(ansi).codes
end

#to_sString

Converts the ANSI codes stored in the instance to a formatted string.

Returns:

  • (String)

    The formatted string with the ANSI codes.



107
108
109
# File 'lib/utils/ansi.rb', line 107

def to_s
  "\e[#{@codes.flatten.join(';')}m"
end