Module: RequestLogAnalyzer::Output::FixedWidth::Color

Defined in:
lib/request_log_analyzer/output/fixed_width.rb

Overview

Colorize module

Constant Summary collapse

STYLES =
{ normal: 0, bold: 1, underscore: 4, blink: 5, inverse: 7, concealed: 8 }
COLORS =
{ black: 0, blue: 4, green: 2, cyan: 6, red: 1, purple: 5, brown: 3, white: 7 }

Instance Method Summary collapse

Instance Method Details

#colorize(text, *options) ⇒ Object

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



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/request_log_analyzer/output/fixed_width.rb', line 27

def colorize(text, *options)
  font_style       = ''
  foreground_color = '0'
  background_color = ''

  options.each do |option|
    if option.is_a?(Symbol)
      foreground_color = "3#{COLORS[option]}" if COLORS.include?(option)
      font_style       = "#{STYLES[option]};" if STYLES.include?(option)
    elsif option.is_a?(Hash)
      option.each do |key, value|
        case key
        when :color then      foreground_color = "3#{COLORS[value]}"  if COLORS.include?(value)
        when :background then background_color = "4#{COLORS[value]};" if COLORS.include?(value)
        when :on then         background_color = "4#{COLORS[value]};" if COLORS.include?(value)
        when :style then      font_style       = "#{STYLES[value]};"  if STYLES.include?(value)
        end
      end
    end
  end
  "\e[#{background_color}#{font_style}#{foreground_color}m#{text}\e[0m"
end