Class: ANSI
- Inherits:
-
Object
- Object
- ANSI
- 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.
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
-
#codes ⇒ Array<Integer>
readonly
The ANSI codes stored in the instance.
Class Method Summary collapse
-
.normalize_array_codes(array) ⇒ Array<Integer>
Normalizes an array of codes, converting symbols and strings to their respective ANSI codes.
-
.remove_from_string(value) ⇒ String
Removes ANSI escape codes from a given string.
Instance Method Summary collapse
-
#append(ansi) ⇒ ANSI
Adds ANSI codes from another instance of the ANSI class.
-
#append!(ansi) ⇒ void
Adds ANSI codes from another instance of the ANSI class and updates the current instance.
-
#initialize(code) ⇒ ANSI
constructor
Initializes a new instance of the ANSI class.
-
#to_s ⇒ String
Converts the ANSI codes stored in the instance to a formatted string.
Constructor Details
#initialize(code) ⇒ ANSI
Initializes a new instance of the ANSI class.
87 88 89 90 91 92 93 94 95 |
# File 'lib/utils/ansi.rb', line 87 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
#codes ⇒ Array<Integer> (readonly)
Returns 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.
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/utils/ansi.rb', line 127 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 |
.remove_from_string(value) ⇒ String
Removes ANSI escape codes from a given string.
This class method is designed to take a string input and remove any ANSI escape codes, which are often used for text formatting in terminal outputs.
80 81 82 |
# File 'lib/utils/ansi.rb', line 80 def self.remove_from_string(value) value.to_s.gsub(CONSTANTS::ANSI::TOKEN_REGEX, '') end |
Instance Method Details
#append(ansi) ⇒ ANSI
Adds ANSI codes from another instance of the ANSI class.
102 103 104 105 106 |
# File 'lib/utils/ansi.rb', line 102 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.
112 113 114 |
# File 'lib/utils/ansi.rb', line 112 def append!(ansi) @codes = append(ansi).codes end |
#to_s ⇒ String
Converts the ANSI codes stored in the instance to a formatted string.
119 120 121 |
# File 'lib/utils/ansi.rb', line 119 def to_s "\e[#{@codes.flatten.join(';')}m" end |