]> git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.rb
bcd683c020b7c91eb16601e9584ca85945113b73
[rbdr/cologne] / lib / cobalt.rb
1 require 'rubygems'
2 require 'isna'
3 require 'logger'
4
5 module Cobalt
6
7 class Console
8
9 attr_accessor :separator_length
10
11 def initialize( options = {} )
12 @indent = 0
13 @loggers = options[:loggers] || [::Logger.new(STDOUT)]
14 @separator_length = 120
15 @color = :white
16 end
17
18 def add_logger logger
19 @loggers << logger
20 end
21
22 def remove_logger logger
23 @loggers = @loggers - [logger]
24 end
25
26 def log(*objects)
27 objects.each do |object|
28 the_string = object.to_s
29 the_string = the_string.to_ansi.send(@color).to_s
30 the_string = the_string.gsub(/^/, ' ' * @indent)
31 @loggers.each { |logger| logger.info(the_string) }
32 end
33 self
34 end
35
36 def pp(*objects)
37 dump = ""
38 if objects.size > 1
39 PP.pp(objects, dump)
40 else
41 PP.pp(objects.first, dump)
42 end
43 log(dump)
44 end
45
46 def info(*objects)
47 notice(*objects)
48 end
49
50 def notice(*objects)
51 color(:cyan) { log(*objects) }
52 end
53
54 def warn(*objects)
55 color(:yellow) { log(*objects) }
56 end
57
58 def error(*objects)
59 color(:red) { log(*objects) }
60 end
61
62 def separator(type = '-')
63 log((type * (@separator_length - @indent)))
64 end
65
66 def space(lines = 1)
67 lines.times { self.log('') }
68 self
69 end
70
71 def indent
72 if block_given?
73 @indent = @indent + 2
74 yield
75 @indent = @indent - 2
76 else
77 @indent = @indent + 2
78 end
79 self
80 end
81
82 def outdent
83 @indent = @indent - 2
84 self
85 end
86
87 def color(symbol)
88 if block_given?
89 old = @color
90 @color = symbol
91 yield
92 @color = old
93 else
94 @color = symbol
95 end
96 self
97 end
98
99 end
100
101 end