Class: String
- Inherits:
-
Object
show all
- Defined in:
- lib/colorize.rb,
lib/ww.rb,
lib/namer.rb,
lib/string_util.rb,
lib/string_util.rb,
lib/string_util.rb,
lib/object_present.rb,
lib/object_present.rb
Overview
Constant Summary
collapse
- FN_ID_LEN =
4
- FN_MAX_LEN =
64
- FN_PATTERN =
characters than can be used in a file name without quotes or escaping
%r{[^!#%\+\-0-9=@A-Z_a-z()\[\]{}]}.freeze
- FN_REPLACEMENT =
'_'
Instance Method Summary
collapse
-
#ansi_control_sequence ⇒ String
Generates an ANSI control sequence for the string.
-
#bgreen ⇒ Object
-
#black ⇒ Object
A collection of methods for applying named colors.
-
#blank? ⇒ Boolean
-
#blinking ⇒ Object
-
#blue ⇒ Object
More named colors using RGB hex values.
-
#bold ⇒ Object
-
#bold_italic ⇒ Object
-
#bold_underline ⇒ Object
-
#bred ⇒ Object
-
#bwhite ⇒ Object
-
#byellow ⇒ Object
-
#cyan ⇒ Object
-
#delete_even_chars ⇒ Object
-
#delete_even_chars! ⇒ Object
-
#deref ⇒ Object
replace the app’s directory with a .
-
#dim ⇒ Object
-
#fg_bg_rgb_color(fg_rgb, bg_rgb) ⇒ String
Applies a 24-bit RGB foreground color to the string.
-
#fg_rgb_color(rgb) ⇒ String
Applies a 24-bit RGB foreground color to the string.
-
#green ⇒ Object
-
#hex_to_fg_bg_rgb(hex_str) ⇒ String
Converts hex color codes to RGB and applies them to the string.
-
#hex_to_rgb(hex_str) ⇒ String
Converts hex color codes to RGB and applies them to the string.
-
#hidden ⇒ Object
-
#indigo ⇒ Object
-
#inverse ⇒ Object
-
#italic ⇒ Object
-
#magenta ⇒ Object
-
#method_missing(method_name, *args, &block) ⇒ String
Handles dynamic method calls to create RGB colors.
-
#orange ⇒ Object
-
#plain ⇒ String
Provides a plain, unmodified version of the string.
-
#present? ⇒ Boolean
Checks if the string contains any non-whitespace characters.
-
#pub_name(id_len: FN_ID_LEN, max_len: FN_MAX_LEN, pattern: FN_PATTERN, replacement: FN_REPLACEMENT) ⇒ Object
block name in commands and documents.
-
#red ⇒ Object
-
#sort_chars(reverse: false, casefold: false, &block) ⇒ Object
-
#sort_chars!(reverse: false, casefold: false, &block) ⇒ Object
-
#strikethrough ⇒ Object
-
#underline ⇒ Object
-
#underline_italic ⇒ Object
-
#violet ⇒ Object
-
#white ⇒ Object
-
#yellow ⇒ Object
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ String
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
|
# File 'lib/colorize.rb', line 14
def method_missing(method_name, *args, &block)
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($')
when 'to_a', 'to_ary', 'to_hash', 'to_int', 'to_io', 'to_regexp'
nil
else
super
end
end
|
Instance Method Details
#ansi_control_sequence ⇒ String
Generates an ANSI control sequence for the string.
35
36
37
|
# File 'lib/colorize.rb', line 35
def ansi_control_sequence
"\033[#{self}\033[0m"
end
|
91
|
# File 'lib/colorize.rb', line 91
def bgreen; "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.
89
|
# File 'lib/colorize.rb', line 89
def black; "30m#{self}".ansi_control_sequence; end
|
#blank? ⇒ Boolean
20
21
22
|
# File 'lib/object_present.rb', line 20
def blank?
empty? || /\A[[:space:]]*\z/.freeze.match?(self)
end
|
115
|
# File 'lib/colorize.rb', line 115
def blinking; "\033[5m#{self}\033[25m"; end
|
More named colors using RGB hex values
99
|
# File 'lib/colorize.rb', line 99
def blue; fg_rgbh_00_00_FF; end
|
108
|
# File 'lib/colorize.rb', line 108
def bold; "\033[1m#{self}\033[22m"; end
|
#bold_italic ⇒ Object
109
|
# File 'lib/colorize.rb', line 109
def bold_italic; "\033[1m\033[3m#{self}\033[22m\033[23m"; end
|
#bold_underline ⇒ Object
110
|
# File 'lib/colorize.rb', line 110
def bold_underline; "\033[1m\033[4m#{self}\033[22m\033[24m"; end
|
90
|
# File 'lib/colorize.rb', line 90
def bred; "1;31m#{self}".ansi_control_sequence; end
|
96
|
# File 'lib/colorize.rb', line 96
def bwhite; "1;37m#{self}".ansi_control_sequence; end
|
92
|
# File 'lib/colorize.rb', line 92
def byellow; "1;33m#{self}".ansi_control_sequence; end
|
94
|
# File 'lib/colorize.rb', line 94
def cyan; "36m#{self}".ansi_control_sequence; end
|
#delete_even_chars ⇒ Object
51
52
53
54
55
56
57
58
59
|
# File 'lib/string_util.rb', line 51
def delete_even_chars
clusters = scan(/\X/)
return dup if clusters.length <= 1
clusters.each_with_index
.select { |_, i| i.even? } .map(&:first)
.join
end
|
#delete_even_chars! ⇒ Object
61
62
63
|
# File 'lib/string_util.rb', line 61
def delete_even_chars!
replace(delete_even_chars)
end
|
replace the app’s directory with a .
262
263
264
|
# File 'lib/ww.rb', line 262
def deref
sub(%r{^#{Dir.pwd}}, '')
end
|
111
|
# File 'lib/colorize.rb', line 111
def dim; "\033[2m#{self}\033[22m"; end
|
#fg_bg_rgb_color(fg_rgb, bg_rgb) ⇒ String
Applies a 24-bit RGB foreground color to the string.
43
44
45
|
# File 'lib/colorize.rb', line 43
def fg_bg_rgb_color(fg_rgb, bg_rgb)
"38;2;#{fg_rgb}m\033[48;2;#{bg_rgb}m#{self}".ansi_control_sequence
end
|
#fg_rgb_color(rgb) ⇒ String
Applies a 24-bit RGB foreground color to the string.
51
52
53
|
# File 'lib/colorize.rb', line 51
def fg_rgb_color(rgb)
"38;2;#{rgb}m#{self}".ansi_control_sequence
end
|
100
|
# File 'lib/colorize.rb', line 100
def green; fg_rgbh_00_FF_00; end
|
#hex_to_fg_bg_rgb(hex_str) ⇒ String
Converts hex color codes to RGB and applies them to the string.
59
60
61
62
63
64
65
|
# File 'lib/colorize.rb', line 59
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) ⇒ String
Converts hex color codes to RGB and applies them to the string.
71
72
73
74
75
|
# File 'lib/colorize.rb', line 71
def hex_to_rgb(hex_str)
fg_rgb_color(
hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
)
end
|
117
|
# File 'lib/colorize.rb', line 117
def hidden; "\033[8m#{self}\033[28m"; end
|
101
|
# File 'lib/colorize.rb', line 101
def indigo; fg_rgbh_4B_00_82; end
|
116
|
# File 'lib/colorize.rb', line 116
def inverse; "\033[7m#{self}\033[27m"; end
|
112
|
# File 'lib/colorize.rb', line 112
def italic; "\033[3m#{self}\033[23m"; end
|
93
|
# File 'lib/colorize.rb', line 93
def magenta; "35m#{self}".ansi_control_sequence; end
|
102
|
# File 'lib/colorize.rb', line 102
def orange; fg_rgbh_FF_7F_00; end
|
Provides a plain, unmodified version of the string.
80
81
82
|
# File 'lib/colorize.rb', line 80
def plain
self
end
|
#present? ⇒ Boolean
Checks if the string contains any non-whitespace characters. characters, false otherwise.
27
28
29
|
# File 'lib/string_util.rb', line 27
def present?
!strip.empty?
end
|
#pub_name(id_len: FN_ID_LEN, max_len: FN_MAX_LEN, pattern: FN_PATTERN, replacement: FN_REPLACEMENT) ⇒ Object
block name in commands and documents
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/namer.rb', line 25
def pub_name(
id_len: FN_ID_LEN, max_len: FN_MAX_LEN,
pattern: FN_PATTERN, replacement: FN_REPLACEMENT
)
trimmed = if self[max_len]
rand(((10**(id_len - 1)) + 1)..(10**id_len)).to_s
dig = Digest::MD5.hexdigest(self)[0, id_len]
self[0..max_len - id_len] + dig
else
self
end
trimmed.gsub(pattern, replacement).tap do |ret|
pp [__LINE__, 'String.pub_name() ->', ret] if $pd
end
end
|
103
|
# File 'lib/colorize.rb', line 103
def red; fg_rgbh_FF_00_00; end
|
#sort_chars(reverse: false, casefold: false, &block) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/string_util.rb', line 91
def sort_chars(reverse: false, casefold: false, &block)
clusters = scan(/\X/)
return self if clusters.length <= 1
sorted =
if block
clusters.sort(&block)
elsif casefold
clusters.sort_by(&:downcase)
else
clusters.sort
end
sorted.reverse! if reverse
sorted.join
end
|
#sort_chars!(reverse: false, casefold: false, &block) ⇒ Object
108
109
110
|
# File 'lib/string_util.rb', line 108
def sort_chars!(reverse: false, casefold: false, &block)
replace(sort_chars(reverse: reverse, casefold: casefold, &block))
end
|
#strikethrough ⇒ Object
118
|
# File 'lib/colorize.rb', line 118
def strikethrough; "\033[9m#{self}\033[29m"; end
|
#underline ⇒ Object
113
|
# File 'lib/colorize.rb', line 113
def underline; "\033[4m#{self}\033[24m"; end
|
#underline_italic ⇒ Object
114
|
# File 'lib/colorize.rb', line 114
def underline_italic; "\033[4m\033[3m#{self}\033[23m\033[24m"; end
|
104
|
# File 'lib/colorize.rb', line 104
def violet; fg_rgbh_94_00_D3; end
|
95
|
# File 'lib/colorize.rb', line 95
def white; "37m#{self}".ansi_control_sequence; end
|
105
|
# File 'lib/colorize.rb', line 105
def yellow; fg_rgbh_FF_FF_00; end
|