Class: String
- Defined in:
- lib/colorize.rb,
lib/namer.rb,
lib/hash_delegator.rb,
lib/object_present.rb,
lib/object_present.rb
Overview
is the value non-empty?
Direct Known Subclasses
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 =
except ‘.’, ‘,’, ‘~’ reserved for tokenization / !“#$%&‘()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ^_`abcdefghijklmnopqrstuvwxyz{|}~
'_'
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
graphics modes.
- #bold_italic ⇒ Object
- #bold_underline ⇒ Object
- #bred ⇒ Object
- #bwhite ⇒ Object
- #byellow ⇒ Object
- #cyan ⇒ Object
- #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.
-
#non_empty? ⇒ Boolean
Checks if the string is not empty.
- #orange ⇒ Object
-
#plain ⇒ String
Provides a plain, unmodified version of the string.
- #present? ⇒ Boolean
-
#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
- #strikethrough ⇒ Object
- #underline ⇒ Object
- #underline_italic ⇒ Object
- #violet ⇒ Object
- #white ⇒ Object
- #x ⇒ 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 31 32 33 34 35 36 37 38 |
# File 'lib/colorize.rb', line 14 def method_missing(method_name, *args, &block) case method_name.to_s # when /^bg_rgb_/ # bytes = $'.split('_') # bg_rgb_color(bytes[0..2].join(';')) when /^fg_bg_rgb_/ pp [__LINE__, caller[0]]; binding.irb bytes = $'.split('_') fg_bg_rgb_color(bytes[0..2].join(';'), bytes[3..5].join(';')) when /^fg_bg_rgbh_/ pp [__LINE__, caller[0]]; binding.irb hex_to_fg_bg_rgb($') when /^fg_rgb_/ pp [__LINE__, caller[0]]; binding.irb fg_rgb_color($'.gsub('_', ';')) when /^fg_rgbh_/ pp [__LINE__, caller[0]]; binding.irb 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.
43 44 45 46 |
# File 'lib/colorize.rb', line 43 def ansi_control_sequence pp [__LINE__, caller[0]]; binding.irb "\033[#{self}\033[0m" end |
#bgreen ⇒ Object
113 |
# File 'lib/colorize.rb', line 113 def bgreen; "1;32m#{self}".ansi_control_sequence; end |
#black ⇒ Object
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/colorize.rb', line 111 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 |
#blinking ⇒ Object
141 |
# File 'lib/colorize.rb', line 141 def blinking; x; "\033[5m#{self}\033[25m"; end |
#blue ⇒ Object
More named colors using RGB hex values
121 |
# File 'lib/colorize.rb', line 121 def blue; fg_rgbh_00_00_FF; end |
#bold ⇒ Object
graphics modes
134 |
# File 'lib/colorize.rb', line 134 def bold; x; "\033[1m#{self}\033[22m"; end |
#bold_italic ⇒ Object
135 |
# File 'lib/colorize.rb', line 135 def bold_italic; x; "\033[1m\033[3m#{self}\033[22m\033[23m"; end |
#bold_underline ⇒ Object
136 |
# File 'lib/colorize.rb', line 136 def bold_underline; x; "\033[1m\033[4m#{self}\033[22m\033[24m"; end |
#bred ⇒ Object
112 |
# File 'lib/colorize.rb', line 112 def bred; "1;31m#{self}".ansi_control_sequence; end |
#bwhite ⇒ Object
118 |
# File 'lib/colorize.rb', line 118 def bwhite; "1;37m#{self}".ansi_control_sequence; end |
#byellow ⇒ Object
114 |
# File 'lib/colorize.rb', line 114 def byellow; "1;33m#{self}".ansi_control_sequence; end |
#cyan ⇒ Object
116 |
# File 'lib/colorize.rb', line 116 def cyan; "36m#{self}".ansi_control_sequence; end |
#dim ⇒ Object
137 |
# File 'lib/colorize.rb', line 137 def dim; x; "\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.
60 61 62 63 |
# File 'lib/colorize.rb', line 60 def fg_bg_rgb_color(fg_rgb, bg_rgb) pp [__LINE__, caller[0]]; binding.irb "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.
69 70 71 72 |
# File 'lib/colorize.rb', line 69 def fg_rgb_color(rgb) pp [__LINE__, caller[0]]; binding.irb "38;2;#{rgb}m#{self}".ansi_control_sequence end |
#green ⇒ Object
122 |
# File 'lib/colorize.rb', line 122 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.
78 79 80 81 82 83 84 85 |
# File 'lib/colorize.rb', line 78 def hex_to_fg_bg_rgb(hex_str) pp [__LINE__, caller[0]]; binding.irb 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.
91 92 93 94 95 96 |
# File 'lib/colorize.rb', line 91 def hex_to_rgb(hex_str) pp [__LINE__, caller[0]]; binding.irb fg_rgb_color( hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';') ) end |
#hidden ⇒ Object
143 |
# File 'lib/colorize.rb', line 143 def hidden; x; "\033[8m#{self}\033[28m"; end |
#indigo ⇒ Object
123 |
# File 'lib/colorize.rb', line 123 def indigo; fg_rgbh_4B_00_82; end |
#inverse ⇒ Object
142 |
# File 'lib/colorize.rb', line 142 def inverse; x; "\033[7m#{self}\033[27m"; end |
#italic ⇒ Object
138 |
# File 'lib/colorize.rb', line 138 def italic; x; "\033[3m#{self}\033[23m"; end |
#magenta ⇒ Object
115 |
# File 'lib/colorize.rb', line 115 def magenta; "35m#{self}".ansi_control_sequence; end |
#non_empty? ⇒ Boolean
Checks if the string is not empty.
51 52 53 |
# File 'lib/hash_delegator.rb', line 51 def non_empty? !empty? end |
#orange ⇒ Object
124 |
# File 'lib/colorize.rb', line 124 def orange; fg_rgbh_FF_7F_00; end |
#plain ⇒ String
Provides a plain, unmodified version of the string.
101 102 103 104 |
# File 'lib/colorize.rb', line 101 def plain pp [__LINE__, caller[0]]; binding.irb self end |
#present? ⇒ Boolean
30 31 32 |
# File 'lib/object_present.rb', line 30 def present? !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
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/namer.rb', line 28 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 { |ret| pp [__LINE__, 'String.pub_name() ->', ret] if $pd } end |
#red ⇒ Object
125 |
# File 'lib/colorize.rb', line 125 def red; fg_rgbh_FF_00_00; end |
#strikethrough ⇒ Object
144 |
# File 'lib/colorize.rb', line 144 def strikethrough; x; "\033[9m#{self}\033[29m"; end |
#underline ⇒ Object
139 |
# File 'lib/colorize.rb', line 139 def underline; x; "\033[4m#{self}\033[24m"; end |
#underline_italic ⇒ Object
140 |
# File 'lib/colorize.rb', line 140 def underline_italic; x; "\033[4m\033[3m#{self}\033[23m\033[24m"; end |
#violet ⇒ Object
126 |
# File 'lib/colorize.rb', line 126 def violet; fg_rgbh_94_00_D3; end |
#white ⇒ Object
117 |
# File 'lib/colorize.rb', line 117 def white; "37m#{self}".ansi_control_sequence; end |
#x ⇒ Object
129 130 131 |
# File 'lib/colorize.rb', line 129 def x pp [__LINE__, caller[1]]; binding.irb end |
#yellow ⇒ Object
127 |
# File 'lib/colorize.rb', line 127 def yellow; fg_rgbh_FF_FF_00; end |