Class: ColorTail::Colorize
- Inherits:
-
Object
- Object
- ColorTail::Colorize
- 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
-
#color_matchers ⇒ Object
Returns the value of attribute color_matchers.
Instance Method Summary collapse
- #add_color_matcher(options) ⇒ Object
- #colorit(message, color, attribute, nl = "\n") ⇒ Object
-
#initialize ⇒ Colorize
constructor
A new instance of Colorize.
- #log(filename, message, line_prefix = nil) ⇒ Object
- #show(message, prefix) ⇒ Object
Constructor Details
#initialize ⇒ Colorize
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_matchers ⇒ Object
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( ) @color_matchers.push( ) end |
#colorit(message, color, attribute, nl = "\n") ⇒ Object
70 71 72 73 |
# File 'lib/colortail.rb', line 70 def colorit(, color, attribute, nl = "\n") attribute = "#{attribute};" if attribute "\e[#{attribute}#{color}m" + .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, , line_prefix=nil) # Add the filename to the message = "#{}" color = :none attribute = nil @color_matchers.each do |filter| if =~ filter[:match] color = filter[:color] attribute = filter[:attribute] = filter[:prepend] + 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(, current_color, current_attribute), line_prefix) end end |
#show(message, prefix) ⇒ Object
66 67 68 |
# File 'lib/colortail.rb', line 66 def show(,prefix) puts "#{prefix}#{}" end |