Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/cli_class_tool/string.rb
Overview
Extension to the core String class to add colorization support
Constant Summary collapse
- @@is_a_tty =
colorization
nil
Instance Method Summary collapse
-
#black ⇒ String
Make the string black.
-
#blue ⇒ String
Make the string blue.
-
#bold ⇒ String
Make the string bold.
-
#brown ⇒ String
Make the string brown (yellow).
-
#colorize(color_code) ⇒ String
Colorize the string using ANSI escape codes.
-
#cyan ⇒ String
Make the string cyan.
-
#gray ⇒ String
(also: #grey)
Make the string gray/grey / light black.
-
#green ⇒ String
Make the string green.
-
#hyperlink(url) ⇒ String
Wrap the string in an OSC 8 terminal hyperlink if stdout is a TTY.
-
#light_blue ⇒ String
Make the string light blue.
-
#light_cyan ⇒ String
Make the string light cyan.
-
#light_green ⇒ String
Make the string light green.
-
#light_magenta ⇒ String
Make the string light magenta.
-
#light_red ⇒ String
Make the string light red.
-
#light_white ⇒ String
Make the string light white.
-
#light_yellow ⇒ String
Make the string light yellow.
-
#magenta ⇒ String
Make the string magenta.
-
#red ⇒ String
Make the string red.
-
#visible_length ⇒ Integer
Compute the visible length of the string Without any of ANSI/ASCII control characters nor embedded hyperlinks.
-
#white ⇒ String
Make the string white.
-
#yellow ⇒ String
Make the string yellow.
Instance Method Details
#black ⇒ String
Make the string black
36 37 38 |
# File 'lib/cli_class_tool/string.rb', line 36 def black colorize(30) end |
#blue ⇒ String
Make the string blue
66 67 68 |
# File 'lib/cli_class_tool/string.rb', line 66 def blue colorize(34) end |
#bold ⇒ String
Make the string bold
90 91 92 |
# File 'lib/cli_class_tool/string.rb', line 90 def bold colorize(1) end |
#brown ⇒ String
Make the string brown (yellow)
54 55 56 |
# File 'lib/cli_class_tool/string.rb', line 54 def brown colorize(33) end |
#colorize(color_code) ⇒ String
Colorize the string using ANSI escape codes
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/cli_class_tool/string.rb', line 10 def colorize(color_code) @@is_a_tty = $stdout.isatty() if @@is_a_tty == nil if @@is_a_tty then if self =~ /\A\e\[([\d;]+)m(.*)\e\[0m\z/ codes = $1.split(';') content = $2 new_codes = codes.dup if color_code.to_i == 1 new_codes.unshift("1") unless new_codes.include?("1") else new_codes.reject! { |c| (30..37).include?(c.to_i) || (90..97).include?(c.to_i) } new_codes << color_code.to_s end new_codes.uniq! new_codes.sort_by! { |c| c.to_i } return "\e[#{new_codes.join(';')}m#{content}\e[0m" else return "\e[#{color_code}m#{self}\e[0m" end else return self end end |
#cyan ⇒ String
Make the string cyan
78 79 80 |
# File 'lib/cli_class_tool/string.rb', line 78 def cyan colorize(36) end |
#gray ⇒ String Also known as: grey
Make the string gray/grey / light black
96 97 98 |
# File 'lib/cli_class_tool/string.rb', line 96 def gray colorize(90) end |
#green ⇒ String
Make the string green
48 49 50 |
# File 'lib/cli_class_tool/string.rb', line 48 def green colorize(32) end |
#hyperlink(url) ⇒ String
Wrap the string in an OSC 8 terminal hyperlink if stdout is a TTY.
147 148 149 150 151 152 153 154 |
# File 'lib/cli_class_tool/string.rb', line 147 def hyperlink(url) @@is_a_tty = $stdout.isatty() if @@is_a_tty == nil if @@is_a_tty && url && !url.to_s.empty? "\e]8;;#{url}\e\\#{self}\e]8;;\e\\" else self end end |
#light_blue ⇒ String
Make the string light blue
121 122 123 |
# File 'lib/cli_class_tool/string.rb', line 121 def light_blue colorize(94) end |
#light_cyan ⇒ String
Make the string light cyan
133 134 135 |
# File 'lib/cli_class_tool/string.rb', line 133 def light_cyan colorize(96) end |
#light_green ⇒ String
Make the string light green
109 110 111 |
# File 'lib/cli_class_tool/string.rb', line 109 def light_green colorize(92) end |
#light_magenta ⇒ String
Make the string light magenta
127 128 129 |
# File 'lib/cli_class_tool/string.rb', line 127 def light_magenta colorize(95) end |
#light_red ⇒ String
Make the string light red
103 104 105 |
# File 'lib/cli_class_tool/string.rb', line 103 def light_red colorize(91) end |
#light_white ⇒ String
Make the string light white
139 140 141 |
# File 'lib/cli_class_tool/string.rb', line 139 def light_white colorize(97) end |
#light_yellow ⇒ String
Make the string light yellow
115 116 117 |
# File 'lib/cli_class_tool/string.rb', line 115 def light_yellow colorize(93) end |
#magenta ⇒ String
Make the string magenta
72 73 74 |
# File 'lib/cli_class_tool/string.rb', line 72 def magenta colorize(35) end |
#red ⇒ String
Make the string red
42 43 44 |
# File 'lib/cli_class_tool/string.rb', line 42 def red colorize(31) end |
#visible_length ⇒ Integer
Compute the visible length of the string Without any of ANSI/ASCII control characters nor embedded hyperlinks
160 161 162 163 164 165 |
# File 'lib/cli_class_tool/string.rb', line 160 def visible_length() return self.gsub(/\e\]8;.*?(?:\e\\|\a)/, "") # Removes opening and closing hyperlink tags .gsub(/\e\[[0-9;?]*[a-zA-Z]/, "") # Removes ANSI escape codes (colors, cursor control) .gsub(/[[:cntrl:]]/, "") # Removes any residual control characters .length end |
#white ⇒ String
Make the string white
84 85 86 |
# File 'lib/cli_class_tool/string.rb', line 84 def white colorize(37) end |
#yellow ⇒ String
Make the string yellow
60 61 62 |
# File 'lib/cli_class_tool/string.rb', line 60 def yellow colorize(33) end |