Class: ColorTail::Colorize

Inherits:
Object
  • Object
show all
Defined in:
lib/colortail.rb

Constant Summary collapse

COLORS =
{
    :none => "0",
    :black => "30",
    :red => "31",
    :green => "32",
    :yellow => "33",
    :blue => "34",
    :magenta => "35",
    :cyan => "36",
    :white => "37"
}
ATTRIBUTES =
{
    :bright => 1,
    :dim => 2,
    :underscore => 4,
    :blink => 5,
    :reverse => 7,
    :hidden => 8
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeColorize

Returns a new instance of Colorize.



35
36
37
38
39
# File 'lib/colortail.rb', line 35

def initialize()
    # This is an instance variable in case we tail
    # multiple files with different groupings
    @color_matchers = Array.new
end

Instance Attribute Details

#color_matchersObject

Returns the value of attribute color_matchers.



33
34
35
# File 'lib/colortail.rb', line 33

def color_matchers
  @color_matchers
end

Instance Method Details

#add_color_matcher(options) ⇒ Object



75
76
77
# File 'lib/colortail.rb', line 75

def add_color_matcher( options )
    @color_matchers.push( options )
end

#colorit(message, color, attribute, nl = "\n") ⇒ Object



70
71
72
73
# File 'lib/colortail.rb', line 70

def colorit(message, color, attribute, nl = "\n")
    attribute = "#{attribute};" if attribute
    "\e[#{attribute}#{color}m" + message.strip + "\e[0m#{nl}"
end

#log(filename, message, line_prefix = nil) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/colortail.rb', line 41

def log(filename, message, line_prefix=nil)
    # Add the filename to the message
    message = "#{message}"

    color = :none
    attribute = nil

    @color_matchers.each do |filter|
        if message =~ filter[:match]
                color = filter[:color]
                attribute = filter[:attribute]
                message = filter[:prepend] + message unless filter[:prepend].nil?
                break
        end
    end

    if color != :hide
        current_color = COLORS[color]
        current_attribute = ATTRIBUTES[attribute]

        line_prefix = colorit(line_prefix.to_s, current_color, current_attribute, nil) unless line_prefix.nil?
        show(colorit(message, current_color, current_attribute), line_prefix)
    end
end

#show(message, prefix) ⇒ Object



66
67
68
# File 'lib/colortail.rb', line 66

def show(message,prefix)
    puts "#{prefix}#{message}"
end