Class: AnsiString
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.
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_sequence ⇒ AnsiString
Generates an ANSI control sequence for the string.
55
56
57
|
# File 'lib/ansi_string.rb', line 55
def ansi_control_sequence
self.class.new("\033[#{self}\033[0m")
end
|
113
|
# File 'lib/ansi_string.rb', line 113
def bgreen; self.class.new("1;32m#{self}").ansi_control_sequence; end
|
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
|
148
|
# File 'lib/ansi_string.rb', line 148
def blinking; self.class.new("\033[5m#{self}\033[25m"); end
|
More named colors using RGB hex values
121
|
# File 'lib/ansi_string.rb', line 121
def blue; fg_rgbh_00_00_FF; end
|
130
|
# File 'lib/ansi_string.rb', line 130
def bold; self.class.new("\033[1m#{self}\033[22m"); end
|
#bold_italic ⇒ Object
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_underline ⇒ Object
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
|
112
|
# File 'lib/ansi_string.rb', line 112
def bred; self.class.new("1;31m#{self}").ansi_control_sequence; end
|
118
|
# File 'lib/ansi_string.rb', line 118
def bwhite; self.class.new("1;37m#{self}").ansi_control_sequence; end
|
114
|
# File 'lib/ansi_string.rb', line 114
def byellow; self.class.new("1;33m#{self}").ansi_control_sequence; end
|
116
|
# File 'lib/ansi_string.rb', line 116
def cyan; self.class.new("36m#{self}").ansi_control_sequence; end
|
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.
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.
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
|
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.
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.
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
|
150
|
# File 'lib/ansi_string.rb', line 150
def hidden; self.class.new("\033[8m#{self}\033[28m"); end
|
123
|
# File 'lib/ansi_string.rb', line 123
def indigo; fg_rgbh_4B_00_82; end
|
149
|
# File 'lib/ansi_string.rb', line 149
def inverse; self.class.new("\033[7m#{self}\033[27m"); end
|
141
|
# File 'lib/ansi_string.rb', line 141
def italic; self.class.new("\033[3m#{self}\033[23m"); end
|
115
|
# File 'lib/ansi_string.rb', line 115
def magenta; self.class.new("35m#{self}").ansi_control_sequence; end
|
124
|
# File 'lib/ansi_string.rb', line 124
def orange; fg_rgbh_FF_7F_00; end
|
Provides a plain, unmodified version of the string.
103
104
105
|
# File 'lib/ansi_string.rb', line 103
def plain
self.class.new(self)
end
|
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.
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
|
#strikethrough ⇒ Object
151
|
# File 'lib/ansi_string.rb', line 151
def strikethrough; self.class.new("\033[9m#{self}\033[29m"); end
|
#underline ⇒ Object
142
|
# File 'lib/ansi_string.rb', line 142
def underline; self.class.new("\033[4m#{self}\033[24m"); end
|
#underline_italic ⇒ Object
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
|
126
|
# File 'lib/ansi_string.rb', line 126
def violet; fg_rgbh_94_00_D3; end
|
117
|
# File 'lib/ansi_string.rb', line 117
def white; self.class.new("37m#{self}").ansi_control_sequence; end
|
127
|
# File 'lib/ansi_string.rb', line 127
def yellow; fg_rgbh_FF_FF_00; end
|