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