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?, #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.
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_sequence ⇒ AnsiString
Generates an ANSI control sequence for the string.
46
47
48
|
# File 'lib/ansi_string.rb', line 46
def ansi_control_sequence
self.class.new("\033[#{self}\033[0m")
end
|
104
|
# File 'lib/ansi_string.rb', line 104
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.
102
|
# File 'lib/ansi_string.rb', line 102
def black; self.class.new("30m#{self}").ansi_control_sequence; end
|
139
|
# File 'lib/ansi_string.rb', line 139
def blinking; self.class.new("\033[5m#{self}\033[25m"); end
|
More named colors using RGB hex values
112
|
# File 'lib/ansi_string.rb', line 112
def blue; fg_rgbh_00_00_FF; end
|
121
|
# File 'lib/ansi_string.rb', line 121
def bold; self.class.new("\033[1m#{self}\033[22m"); end
|
#bold_italic ⇒ Object
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_underline ⇒ Object
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
|
103
|
# File 'lib/ansi_string.rb', line 103
def bred; self.class.new("1;31m#{self}").ansi_control_sequence; end
|
109
|
# File 'lib/ansi_string.rb', line 109
def bwhite; self.class.new("1;37m#{self}").ansi_control_sequence; end
|
105
|
# File 'lib/ansi_string.rb', line 105
def byellow; self.class.new("1;33m#{self}").ansi_control_sequence; end
|
107
|
# File 'lib/ansi_string.rb', line 107
def cyan; self.class.new("36m#{self}").ansi_control_sequence; end
|
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.
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.
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
|
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.
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.
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
|
141
|
# File 'lib/ansi_string.rb', line 141
def hidden; self.class.new("\033[8m#{self}\033[28m"); end
|
114
|
# File 'lib/ansi_string.rb', line 114
def indigo; fg_rgbh_4B_00_82; end
|
140
|
# File 'lib/ansi_string.rb', line 140
def inverse; self.class.new("\033[7m#{self}\033[27m"); end
|
132
|
# File 'lib/ansi_string.rb', line 132
def italic; self.class.new("\033[3m#{self}\033[23m"); end
|
106
|
# File 'lib/ansi_string.rb', line 106
def magenta; self.class.new("35m#{self}").ansi_control_sequence; end
|
115
|
# File 'lib/ansi_string.rb', line 115
def orange; fg_rgbh_FF_7F_00; end
|
Provides a plain, unmodified version of the string.
94
95
96
|
# File 'lib/ansi_string.rb', line 94
def plain
self.class.new(self)
end
|
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.
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
|
#strikethrough ⇒ Object
142
|
# File 'lib/ansi_string.rb', line 142
def strikethrough; self.class.new("\033[9m#{self}\033[29m"); end
|
#underline ⇒ Object
133
|
# File 'lib/ansi_string.rb', line 133
def underline; self.class.new("\033[4m#{self}\033[24m"); end
|
#underline_italic ⇒ Object
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
|
117
|
# File 'lib/ansi_string.rb', line 117
def violet; fg_rgbh_94_00_D3; end
|
108
|
# File 'lib/ansi_string.rb', line 108
def white; self.class.new("37m#{self}").ansi_control_sequence; end
|
118
|
# File 'lib/ansi_string.rb', line 118
def yellow; fg_rgbh_FF_FF_00; end
|