Class: Jefe::ColorPrinter

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeColorPrinter

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

#datetimeObject



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