Class: Jefe::ColorPrinter
- Inherits:
-
Object
- Object
- Jefe::ColorPrinter
- Defined in:
- lib/jefe/color_printer.rb
Constant Summary collapse
- Color =
Thor::Shell::Color
- COLORS =
%w{CYAN YELLOW GREEN MAGENTA RED}
Instance Method Summary collapse
- #color_for(type) ⇒ Object
- #colored(color, string) ⇒ Object
- #datetime ⇒ Object
-
#initialize ⇒ ColorPrinter
constructor
A new instance of ColorPrinter.
- #out(name, msg) ⇒ Object
- #padded(name) ⇒ Object
Constructor Details
#initialize ⇒ ColorPrinter
Returns a new instance of ColorPrinter.
6 7 8 9 |
# File 'lib/jefe/color_printer.rb', line 6 def initialize @colors = {"system" => "WHITE"} @longest_seen = 0 end |
Instance Method Details
#color_for(type) ⇒ Object
14 15 16 |
# File 'lib/jefe/color_printer.rb', line 14 def color_for type @colors[type] ||= COLORS.shift.tap {|c| COLORS.push c} end |
#colored(color, string) ⇒ Object
10 11 12 13 |
# File 'lib/jefe/color_printer.rb', line 10 def colored color, string color = Color.const_get color "#{color}#{string}#{Color::CLEAR}" end |
#datetime ⇒ Object
21 22 23 |
# File 'lib/jefe/color_printer.rb', line 21 def datetime Time.now.strftime '%H:%M:%S' end |
#out(name, msg) ⇒ Object
24 25 26 27 28 |
# File 'lib/jefe/color_printer.rb', line 24 def out name, msg type = name.match(/^([A-Za-z0-9_]+).\d+$/) ? $1 : name color = color_for type puts colored(color, "#{datetime} #{padded name} | ") + msg.chomp end |
#padded(name) ⇒ Object
17 18 19 20 |
# File 'lib/jefe/color_printer.rb', line 17 def padded name @longest_seen = name.size if name.size > @longest_seen name.ljust(@longest_seen) end |