]> git.r.bdr.sh - rbdr/cologne/blob - lib/cobalt.rb
Buffering is not consoles responsibility.
[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 notice(*objects)
47 color(:cyan) { log(*objects) }
48 end
49
50 def warn(*objects)
51 color(:yellow) { log(*objects) }
52 end
53
54 def error(*objects)
55 color(:red) { log(*objects) }
56 end
57
58 def separator(type = '-')
59 log((type * (@separator_length - @indent)))
60 end
61
62 def space(lines = 1)
63 lines.times { self.log('') }
64 self
65 end
66
67 def indent
68 if block_given?
69 @indent = @indent + 2
70 yield
71 @indent = @indent - 2
72 else
73 @indent = @indent + 2
74 end
75 self
76 end
77
78 def outdent
79 @indent = @indent - 2
80 self
81 end
82
83 def color(symbol)
84 if block_given?
85 old = @color
86 @color = symbol
87 yield
88 @color = old
89 else
90 @color = symbol
91 end
92 self
93 end
94
95 end
96
97 end