Module: CLIColorize
- Defined in:
- lib/cli-colorize.rb
Overview
A simple module for use when colorizing output to the terminal.
Constant Summary collapse
- CTRLSTR_START =
ANSI control codes 0 Turn off all attributes 1 Set bright mode 4 Set underline mode 5 Set blink mode 7 Exchange foreground and background colors 8 Hide text (foreground color would be the same as background) 30 Black text 31 Red text 32 Green text 33 Yellow text 34 Blue text 35 Magenta text 36 Cyan text 37 White text 39 Default text color 40 Black background 41 Red background 42 Green background 43 Yellow background 44 Blue background 45 Magenta background 46 Cyan background 47 White background 49 Default background color
"\033["- CTRLSTR_END =
"\033[0m"- CTRLSTR_DELIM =
'm'- CONFIG =
{ :reset => '0', :bright => '1', :underline => '4', :blink => '5', :swap => '7', :hide => '8' }
- SET_FORE =
{ :black => '30', :red => '31', :green => '32', :yellow => '33', :blue => '34', :magenta => '35', :cyan => '36', :white => '37', :default => '39' }
- SET_BACK =
{ :black => '40', :red => '41', :green => '42', :yellow => '43', :blue => '44', :magenta => '45', :cyan => '46', :white => '47', :default => '49' }
- @@off =
false- @@default_color =
:blue
Class Method Summary collapse
-
.colorize(text, color = nil) ⇒ Object
Set control characters around text to colorize output to the terminal.
- .colorize_if_tty(text, color = nil) ⇒ Object
- .default_color ⇒ Object
- .default_color=(color) ⇒ Object
- .on? ⇒ Boolean
-
.print(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text.
-
.print_colorized_if_tty(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text if STDOUT is a tty device.
-
.puts(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text.
-
.puts_colorized_if_tty(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text if STDOUT is a tty device.
-
.safe_colorize(text, color = nil) ⇒ Object
Use safe_colorize in conjunction with CLIColorize.off and CLIColorize.on to conditionally determine whether or not output will be given the control characters for colorization.
-
.safe_print(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text.
-
.safe_puts(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text.
Instance Method Summary collapse
-
#colorize(text, color = nil) ⇒ Object
Instance method that exposes the class method colorize to classes that
include 'CLIColorize'. - #colorize_if_tty(text, color = nil) ⇒ Object
- #default_color ⇒ Object
- #default_color=(color) ⇒ Object
-
#print_colorized_if_tty(text, color = nil) ⇒ Object
instance method delegating to class method, see CLIColorize.print_if_tty.
-
#puts_colorized_if_tty(text, color = nil) ⇒ Object
instance method delegating to class method, see CLIColorize.puts_if_tty.
-
#safe_colorize(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_colorize to classes that
include 'CLIColorize'. -
#safe_colorize_active ⇒ Object
(also: #safe_colorize_activate)
Makes the safe_colorize method return text with colorization control codes.
-
#safe_colorize_deactive ⇒ Object
(also: #safe_colorize_deactivate)
Makes the safe_colorize method return text without colorization control codes.
-
#safe_print(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_puts to classes that
include 'CLIColorize'. -
#safe_puts(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_puts to classes that
include 'CLIColorize'.
Class Method Details
.colorize(text, color = nil) ⇒ Object
Set control characters around text to colorize output to the terminal.
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/cli-colorize.rb', line 73 def CLIColorize.colorize(text, color=nil) unless color.is_a? Hash color = @@default_color if color.nil? "#{CTRLSTR_START+SET_FORE[color.to_sym]+CTRLSTR_DELIM}#{text}#{CTRLSTR_END}" else args = [] args << SET_FORE[color[:foreground].to_sym] unless color[:foreground].nil? args << SET_BACK[color[:background].to_sym] unless color[:background].nil? args << CONFIG[color[:config].to_sym] unless color[:config].nil? control_string = args.join(';') "#{CTRLSTR_START+control_string+CTRLSTR_DELIM}#{text}#{CTRLSTR_END}" end end |
.colorize_if_tty(text, color = nil) ⇒ Object
101 102 103 |
# File 'lib/cli-colorize.rb', line 101 def CLIColorize.colorize_if_tty(text, color=nil) STDOUT.isatty ? CLIColorize.colorize(text, color) : text end |
.default_color ⇒ Object
172 |
# File 'lib/cli-colorize.rb', line 172 def CLIColorize.default_color; @@default_color; end |
.default_color=(color) ⇒ Object
175 176 177 |
# File 'lib/cli-colorize.rb', line 175 def CLIColorize.default_color=(color) @@default_color = color.to_sym end |
.print(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text.
97 98 99 |
# File 'lib/cli-colorize.rb', line 97 def CLIColorize.print(text, color=nil) STDOUT.print CLIColorize.colorize(text, color) end |
.print_colorized_if_tty(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text if STDOUT is a tty device. (If STDOUT has been redirected to a file, it will be a block device, not a tty device, and we wouldn't want the ANSI codes inserted.
122 123 124 |
# File 'lib/cli-colorize.rb', line 122 def CLIColorize.print_colorized_if_tty(text, color=nil) STDOUT.print(CLIColorize.colorize_if_tty(text, color)) end |
.puts(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text.
92 93 94 |
# File 'lib/cli-colorize.rb', line 92 def CLIColorize.puts(text, color=nil) STDOUT.puts CLIColorize.colorize(text, color) end |
.puts_colorized_if_tty(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text if STDOUT is a tty device. (If STDOUT has been redirected to a file, it will be a block device, not a tty device, and we wouldn't want the ANSI codes inserted.
111 112 113 |
# File 'lib/cli-colorize.rb', line 111 def CLIColorize.puts_colorized_if_tty(text, color=nil) STDOUT.puts(CLIColorize.colorize_if_tty(text, color)) end |
.safe_colorize(text, color = nil) ⇒ Object
Use safe_colorize in conjunction with CLIColorize.off and CLIColorize.on to conditionally determine whether or not output will be given the control characters for colorization. This is designed to work with a command-line switch to the script that uses this module.
133 134 135 136 |
# File 'lib/cli-colorize.rb', line 133 def CLIColorize.safe_colorize(text, color=nil) return text if @@off colorize(text, color) end |
.safe_print(text, color = nil) ⇒ Object
Call STDOUT.print with the colorized text.
164 165 166 |
# File 'lib/cli-colorize.rb', line 164 def CLIColorize.safe_print(text, color=nil) STDOUT.print CLIColorize.safe_colorize(text, color) end |
.safe_puts(text, color = nil) ⇒ Object
Call STDOUT.puts with the colorized text.
155 156 157 |
# File 'lib/cli-colorize.rb', line 155 def CLIColorize.safe_puts(text, color=nil) STDOUT.puts CLIColorize.safe_colorize(text, color) end |
Instance Method Details
#colorize(text, color = nil) ⇒ Object
Instance method that exposes the class method colorize to classes that include 'CLIColorize'
87 88 89 |
# File 'lib/cli-colorize.rb', line 87 def colorize(text, color=nil) CLIColorize.colorize(text, color) end |
#colorize_if_tty(text, color = nil) ⇒ Object
104 105 106 |
# File 'lib/cli-colorize.rb', line 104 def colorize_if_tty(text, color=nil) CLIColorize.colorize_if_tty(text, color) end |
#default_color ⇒ Object
173 |
# File 'lib/cli-colorize.rb', line 173 def default_color; CLIColorize.default_color; end |
#default_color=(color) ⇒ Object
178 179 180 |
# File 'lib/cli-colorize.rb', line 178 def default_color=(color) CLIColorize.default_color=(color) end |
#print_colorized_if_tty(text, color = nil) ⇒ Object
instance method delegating to class method, see CLIColorize.print_if_tty
126 127 128 |
# File 'lib/cli-colorize.rb', line 126 def print_colorized_if_tty(text, color=nil) CLIColorize.print_colorized_if_tty(text, color) end |
#puts_colorized_if_tty(text, color = nil) ⇒ Object
instance method delegating to class method, see CLIColorize.puts_if_tty
115 116 117 |
# File 'lib/cli-colorize.rb', line 115 def puts_colorized_if_tty(text, color=nil) CLIColorize.puts_colorized_if_tty(text, color) end |
#safe_colorize(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_colorize to classes that include 'CLIColorize'
138 139 140 |
# File 'lib/cli-colorize.rb', line 138 def safe_colorize(text, color=nil) CLIColorize.safe_colorize(text, color) end |
#safe_colorize_active ⇒ Object Also known as: safe_colorize_activate
Makes the safe_colorize method return text with colorization control codes. This is the default state.
143 144 145 |
# File 'lib/cli-colorize.rb', line 143 def safe_colorize_active CLIColorize.on end |
#safe_colorize_deactive ⇒ Object Also known as: safe_colorize_deactivate
Makes the safe_colorize method return text without colorization control codes.
149 150 151 |
# File 'lib/cli-colorize.rb', line 149 def safe_colorize_deactive CLIColorize.off end |
#safe_print(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_puts to classes that include 'CLIColorize'
168 169 170 |
# File 'lib/cli-colorize.rb', line 168 def safe_print(text, color=nil) CLIColorize.safe_print(text, color) end |
#safe_puts(text, color = nil) ⇒ Object
Instance method that exposes the class method safe_puts to classes that include 'CLIColorize'
159 160 161 |
# File 'lib/cli-colorize.rb', line 159 def safe_puts(text, color=nil) CLIColorize.safe_puts(text, color) end |