Class: Cobalt::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/cobalt.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Console

Returns a new instance of Console.



11
12
13
14
15
16
17
# File 'lib/cobalt.rb', line 11

def initialize( options = {} )
  @indent           = 0
  @loggers          = options[:loggers] || [::Logger.new(STDOUT)]
  @separator_length = 120
  @color            = :white
  @level            = :info
end

Instance Attribute Details

#separator_lengthObject

Returns the value of attribute separator_length.



9
10
11
# File 'lib/cobalt.rb', line 9

def separator_length
  @separator_length
end

Instance Method Details

#add_logger(logger) ⇒ Object



19
20
21
# File 'lib/cobalt.rb', line 19

def add_logger logger
  @loggers << logger
end

#color(symbol) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/cobalt.rb', line 93

def color(symbol)
  if block_given?
    old = @color
    @color = symbol
    yield
    @color = old
  else
    @color = symbol
  end
  self
end

#error(*objects) ⇒ Object



62
63
64
65
66
# File 'lib/cobalt.rb', line 62

def error(*objects)
  @level = :error
  color(:red) { log(*objects) }
  @level = :info
end

#indentObject



77
78
79
80
81
82
83
84
85
86
# File 'lib/cobalt.rb', line 77

def indent
  if block_given?
    @indent = @indent + 2
    yield
    @indent = @indent - 2
  else
    @indent = @indent + 2
  end
  self
end

#info(*objects) ⇒ Object



47
48
49
# File 'lib/cobalt.rb', line 47

def info(*objects)
  notice(*objects)
end

#log(*objects) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/cobalt.rb', line 27

def log(*objects)
  objects.each do |object|
    the_string = object.to_s
    the_string = the_string.to_ansi.send(@color).to_s
    the_string = the_string.gsub(/^/, ' ' * @indent)
    @loggers.each { |logger| logger.send(@level, the_string) }
  end
  self
end

#notice(*objects) ⇒ Object



51
52
53
54
# File 'lib/cobalt.rb', line 51

def notice(*objects)
  @level = :info
  color(:cyan) { log(*objects) }
end

#outdentObject



88
89
90
91
# File 'lib/cobalt.rb', line 88

def outdent
  @indent = @indent - 2
  self
end

#pp(*objects) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/cobalt.rb', line 37

def pp(*objects)
  dump = ""
  if objects.size > 1
    PP.pp(objects, dump)
  else
    PP.pp(objects.first, dump)
  end
  log(dump)
end

#remove_logger(logger) ⇒ Object



23
24
25
# File 'lib/cobalt.rb', line 23

def remove_logger logger
  @loggers = @loggers - [logger]
end

#separator(type = '-') ⇒ Object



68
69
70
# File 'lib/cobalt.rb', line 68

def separator(type = '-')
  log((type * (@separator_length - @indent)))
end

#space(lines = 1) ⇒ Object



72
73
74
75
# File 'lib/cobalt.rb', line 72

def space(lines = 1)
  lines.times { self.log('') }
  self
end

#warn(*objects) ⇒ Object



56
57
58
59
60
# File 'lib/cobalt.rb', line 56

def warn(*objects)
  @level = :warn
  color(:yellow) { log(*objects) }
  @level = :info
end