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?, #non_empty?, #present?, #pub_name, #x

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
# 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 /^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.



46
47
48
# File 'lib/ansi_string.rb', line 46

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

#bgreenObject



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

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.



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

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

#blinkingObject



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

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

#blueObject

More named colors using RGB hex values



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

def blue;    fg_rgbh_00_00_FF; end

#boldObject

Graphics modes



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

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

#bold_italicObject



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

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

#bold_underlineObject



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

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

#bredObject



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

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

#bwhiteObject



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

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

#byellowObject



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

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

#cyanObject



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

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

#dimObject



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

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.



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

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.



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

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

#greenObject



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

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.



71
72
73
74
75
76
77
# File 'lib/ansi_string.rb', line 71

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.



83
84
85
86
87
88
89
# File 'lib/ansi_string.rb', line 83

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



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

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

#indigoObject



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

def indigo;  fg_rgbh_4B_00_82; end

#inverseObject



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

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

#italicObject



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

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

#magentaObject



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

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

#orangeObject



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

def orange;  fg_rgbh_FF_7F_00; end

#plainAnsiString

Provides a plain, unmodified version of the string.

Returns:



94
95
96
# File 'lib/ansi_string.rb', line 94

def plain
  self.class.new(self)
end

#redObject



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

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.



39
40
41
# File 'lib/ansi_string.rb', line 39

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

#strikethroughObject



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

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

#underlineObject



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

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

#underline_italicObject



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

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

#violetObject



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

def violet;  fg_rgbh_94_00_D3; end

#whiteObject



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

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

#yellowObject



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

def yellow;  fg_rgbh_FF_FF_00; end