Module: Colored
Constant Summary collapse
- COLORS =
{ 'black' => 30, 'red' => 31, 'green' => 32, 'yellow' => 33, 'blue' => 34, 'magenta' => 35, 'cyan' => 36, 'white' => 37 }
- EXTRAS =
{ 'clear' => 0, 'bold' => 1, 'underline' => 4, 'reversed' => 7 }
Instance Method Summary collapse
- #color(color_name) ⇒ Object
- #colorize(string, options = {}) ⇒ Object
- #colors ⇒ Object
- #extra(extra_name) ⇒ Object
Instance Method Details
#color(color_name) ⇒ Object
71 72 73 74 75 76 77 |
# File 'lib/colored.rb', line 71 def color(color_name) return '' if String.colors_disabled background = color_name.to_s =~ /on_/ color_name = color_name.to_s.sub('on_', '') return unless color_name && COLORS[color_name] "\e[#{COLORS[color_name] + (background ? 10 : 0)}m" end |
#colorize(string, options = {}) ⇒ Object
55 56 57 58 59 |
# File 'lib/colored.rb', line 55 def colorize(string, = {}) colored = [color([:foreground]), color("on_#{[:background]}"), extra([:extra])].compact * '' colored << string colored << extra(:clear) end |
#colors ⇒ Object
61 62 63 |
# File 'lib/colored.rb', line 61 def colors @@colors ||= COLORS.keys.sort end |
#extra(extra_name) ⇒ Object
65 66 67 68 69 |
# File 'lib/colored.rb', line 65 def extra(extra_name) return '' if String.colors_disabled extra_name = extra_name.to_s "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name] end |