Class: AnsiString

Inherits:
String show all
Defined in:
lib/ansi_string.rb

Overview

Extends Ruby’s native String class to include ANSI coloring functionality. Adds methods to apply RGB colors, named colors, and other formatting to strings.

Constant Summary

Constants inherited from String

String::FN_ID_LEN, String::FN_MAX_LEN, String::FN_PATTERN, String::FN_REPLACEMENT

Instance Method Summary collapse

Methods inherited from String

#blank?, #delete_even_chars, #delete_even_chars!, #deref, #present?, #pub_name, #sort_chars, #sort_chars!

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ AnsiString

Handles dynamic method calls to create RGB colors.

Parameters:

  • method_name (Symbol)

    The name of the method being called.

  • args (Array)

    The arguments passed to the method.

  • block (Proc)

    An optional block.

Returns:



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ansi_string.rb', line 14

def method_missing(method_name, *args, &block)
  if dynamic_color_method?(method_name)
    case method_name.to_s
    when /^ansi_/
      segments = $'.split('__')
      codes = ''
      segments[0..-2].each do |segment|
        codes += "\033[#{segment.split('_').join(';')}m"
      end
      codes += to_s
      codes += "\033[#{segments.last.split('_').join(';')}m"
      self.class.new(codes)
    when /^fg_bg_rgb_/
      bytes = $'.split('_')
      fg_bg_rgb_color(bytes[0..2].join(';'), bytes[3..5].join(';'))
    when /^fg_bg_rgbh_/
      hex_to_fg_bg_rgb($')
    when /^fg_rgb_/
      fg_rgb_color($'.gsub('_', ';'))
    when /^fg_rgbh_/
      hex_to_rgb($')
    else
      super
    end
  else
    super
  end
end

Instance Method Details

#ansi_control_sequenceAnsiString

Generates an ANSI control sequence for the string.

Returns:

  • (AnsiString)

    The string wrapped in an ANSI control sequence.



55
56
57
# File 'lib/ansi_string.rb', line 55

def ansi_control_sequence
  self.class.new("\033[#{self}\033[0m")
end

#bgreenObject



113
# File 'lib/ansi_string.rb', line 113

def bgreen;  self.class.new("1;32m#{self}").ansi_control_sequence; end

#blackObject

A collection of methods for applying named colors.

For example, #black applies a black foreground color to the string. These are provided for convenience and easy readability.



111
# File 'lib/ansi_string.rb', line 111

def black;   self.class.new("30m#{self}").ansi_control_sequence; end

#blinkingObject



148
# File 'lib/ansi_string.rb', line 148

def blinking;         self.class.new("\033[5m#{self}\033[25m"); end

#blueObject

More named colors using RGB hex values



121
# File 'lib/ansi_string.rb', line 121

def blue;    fg_rgbh_00_00_FF; end

#boldObject

Graphics modes



130
# File 'lib/ansi_string.rb', line 130

def bold; self.class.new("\033[1m#{self}\033[22m"); end

#bold_italicObject



132
133
134
# File 'lib/ansi_string.rb', line 132

def bold_italic;
  self.class.new("\033[1m\033[3m#{self}\033[22m\033[23m");
end

#bold_underlineObject



136
137
138
# File 'lib/ansi_string.rb', line 136

def bold_underline;
  self.class.new("\033[1m\033[4m#{self}\033[22m\033[24m");
end

#bredObject



112
# File 'lib/ansi_string.rb', line 112

def bred;    self.class.new("1;31m#{self}").ansi_control_sequence; end

#bwhiteObject



118
# File 'lib/ansi_string.rb', line 118

def bwhite;  self.class.new("1;37m#{self}").ansi_control_sequence; end

#byellowObject



114
# File 'lib/ansi_string.rb', line 114

def byellow; self.class.new("1;33m#{self}").ansi_control_sequence; end

#cyanObject



116
# File 'lib/ansi_string.rb', line 116

def cyan;    self.class.new("36m#{self}").ansi_control_sequence; end

#dimObject



140
# File 'lib/ansi_string.rb', line 140

def dim;              self.class.new("\033[2m#{self}\033[22m"); end

#fg_bg_rgb_color(fg_rgb, bg_rgb) ⇒ AnsiString

Applies a 24-bit RGB foreground and background color to the string.

Parameters:

  • fg_rgb (String)

    The RGB foreground color, expressed as a string like “1;2;3”.

  • bg_rgb (String)

    The RGB background color, expressed as a string like “4;5;6”.

Returns:

  • (AnsiString)

    The string with the applied RGB foreground and background colors.



64
65
66
# File 'lib/ansi_string.rb', line 64

def fg_bg_rgb_color(fg_rgb, bg_rgb)
  self.class.new("38;2;#{fg_rgb}m\033[48;2;#{bg_rgb}m#{self}").ansi_control_sequence
end

#fg_rgb_color(rgb) ⇒ AnsiString

Applies a 24-bit RGB foreground color to the string.

Parameters:

  • rgb (String)

    The RGB color, expressed as a string like “1;2;3”.

Returns:

  • (AnsiString)

    The string with the applied RGB foreground color.



72
73
74
# File 'lib/ansi_string.rb', line 72

def fg_rgb_color(rgb)
  self.class.new("38;2;#{rgb}m#{self}").ansi_control_sequence
end

#greenObject



122
# File 'lib/ansi_string.rb', line 122

def green;   fg_rgbh_00_FF_00; end

#hex_to_fg_bg_rgb(hex_str) ⇒ AnsiString

Converts hex color codes to RGB and applies them to the string.

Parameters:

  • hex_str (String)

    The RGB color, expressed as a hex string like “FF00FF”.

Returns:

  • (AnsiString)

    The string with the applied RGB foreground color.



80
81
82
83
84
85
86
# File 'lib/ansi_string.rb', line 80

def hex_to_fg_bg_rgb(hex_str)
  values = hex_str.split('_').map { |hex| hex.to_i(16).to_s }
  fg_bg_rgb_color(
    values[0..2].join(';'),
    values[3..5].join(';')
  )
end

#hex_to_rgb(hex_str) ⇒ AnsiString

Converts hex color codes to RGB and applies them to the string.

Parameters:

  • hex_str (String)

    The RGB color, expressed as a hex string like “FF00FF”.

Returns:

  • (AnsiString)

    The string with the applied RGB foreground color.



92
93
94
95
96
97
98
# File 'lib/ansi_string.rb', line 92

def hex_to_rgb(hex_str)
  self.class.new(
    fg_rgb_color(
      hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
    )
  )
end

#hiddenObject



150
# File 'lib/ansi_string.rb', line 150

def hidden;           self.class.new("\033[8m#{self}\033[28m"); end

#indigoObject



123
# File 'lib/ansi_string.rb', line 123

def indigo;  fg_rgbh_4B_00_82; end

#inverseObject



149
# File 'lib/ansi_string.rb', line 149

def inverse;          self.class.new("\033[7m#{self}\033[27m"); end

#italicObject



141
# File 'lib/ansi_string.rb', line 141

def italic;           self.class.new("\033[3m#{self}\033[23m"); end

#magentaObject



115
# File 'lib/ansi_string.rb', line 115

def magenta; self.class.new("35m#{self}").ansi_control_sequence; end

#orangeObject



124
# File 'lib/ansi_string.rb', line 124

def orange;  fg_rgbh_FF_7F_00; end

#plainAnsiString

Provides a plain, unmodified version of the string.

Returns:



103
104
105
# File 'lib/ansi_string.rb', line 103

def plain
  self.class.new(self)
end

#redObject



125
# File 'lib/ansi_string.rb', line 125

def red;     fg_rgbh_FF_00_00; end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Checks if the AnsiString instance responds to a particular method.

Parameters:

  • method_name (Symbol)

    The name of the method being checked.

  • include_private (Boolean) (defaults to: false)

    Whether to include private methods in the check.

Returns:

  • (Boolean)

    True if the method is supported, otherwise false.



48
49
50
# File 'lib/ansi_string.rb', line 48

def respond_to_missing?(method_name, include_private = false)
  dynamic_color_method?(method_name) || super
end

#strikethroughObject



151
# File 'lib/ansi_string.rb', line 151

def strikethrough;    self.class.new("\033[9m#{self}\033[29m"); end

#underlineObject



142
# File 'lib/ansi_string.rb', line 142

def underline;        self.class.new("\033[4m#{self}\033[24m"); end

#underline_italicObject



144
145
146
# File 'lib/ansi_string.rb', line 144

def underline_italic;
  self.class.new("\033[4m\033[3m#{self}\033[23m\033[24m");
end

#violetObject



126
# File 'lib/ansi_string.rb', line 126

def violet;  fg_rgbh_94_00_D3; end

#whiteObject



117
# File 'lib/ansi_string.rb', line 117

def white;   self.class.new("37m#{self}").ansi_control_sequence; end

#yellowObject



127
# File 'lib/ansi_string.rb', line 127

def yellow;  fg_rgbh_FF_FF_00; end