Class: CMDB::Shell::Printer

Inherits:
Object
  • Object
show all
Defined in:
lib/cmdb/shell/printer.rb

Instance Method Summary collapse

Constructor Details

#initialize(out = $stdout, err = $stderr, text = Text.new(true)) ⇒ Printer

Returns a new instance of Printer.



3
4
5
6
7
# File 'lib/cmdb/shell/printer.rb', line 3

def initialize(out=$stdout, err=$stderr, text=Text.new(true))
  @out = out
  @err = err
  @c = text
end

Instance Method Details

#error(str) ⇒ Object

Print an error message.



16
17
18
19
# File 'lib/cmdb/shell/printer.rb', line 16

def error(str)
  @err.puts @c.bright_red(str)
  self
end

#info(str) ⇒ Object

Print an informational message.



10
11
12
13
# File 'lib/cmdb/shell/printer.rb', line 10

def info(str)
  @out.puts @c.white(str)
  self
end

#keys_values(h, prefix: nil) ⇒ Object

Display a table of keys/values.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/cmdb/shell/printer.rb', line 28

def keys_values(h, prefix:nil)
  wk = h.keys.inject(0) { |ax, e| e.size > ax ? e.size : ax }
  wv = h.values.inject(0) { |ax, e| es = e.inspect.size; es > ax ? es : ax }
  half = @c.width / 2
  wk = [wk, half].min-3
  wv = [wv, half].min
  re = (@c.width - wk - wv)
  wv += re if re > 0

  h.each do |k, v|
    @out.puts format('  %s %s', color_key(k, wk+1, prefix:prefix), color_value(v, wv))
  end

  self
end

#value(obj) ⇒ Object

Display a single CMDB value.



22
23
24
25
# File 'lib/cmdb/shell/printer.rb', line 22

def value(obj)
  @out.puts '  ' + color_value(obj, @c.width-2)
  self
end