module RequestLogAnalyzer::Output::FixedWidth::Color

Colorize module

Constants

COLORS
STYLES

Public Instance Methods

colorize(text, *options) click to toggle source

Colorize text text The text to colorize Options

* <tt>:background</tt> The background color to paint. Defined in Color::COLORS
* <tt>:color</tt> The foreground color to paint. Defined in Color::COLORS
* <tt>:on</tt> Alias for :background
* <tt>:style</tt> Font style, defined in Color::STYLES

Returns ASCII colored string

   # File lib/request_log_analyzer/output/fixed_width.rb
27 def colorize(text, *options)
28   font_style       = ''
29   foreground_color = '0'
30   background_color = ''
31 
32   options.each do |option|
33     if option.is_a?(Symbol)
34       foreground_color = "3#{COLORS[option]}" if COLORS.include?(option)
35       font_style       = "#{STYLES[option]};" if STYLES.include?(option)
36     elsif option.is_a?(Hash)
37       option.each do |key, value|
38         case key
39         when :color then      foreground_color = "3#{COLORS[value]}"  if COLORS.include?(value)
40         when :background then background_color = "4#{COLORS[value]};" if COLORS.include?(value)
41         when :on then         background_color = "4#{COLORS[value]};" if COLORS.include?(value)
42         when :style then      font_style       = "#{STYLES[value]};"  if STYLES.include?(value)
43         end
44       end
45     end
46   end
47   "\e[#{background_color}#{font_style}#{foreground_color}m#{text}\e[0m"
48 end