Class: WavefrontDisplayPrinter::Terse

Inherits:
Base
  • Object
show all
Defined in:
lib/wavefront-cli/display/printer/terse.rb

Overview

Print things which are per-row. The terse listings, primarily

Instance Attribute Summary collapse

Attributes inherited from Base

#out

Instance Method Summary collapse

Methods inherited from Base

#key_width, #to_s

Constructor Details

#initialize(data, *keys) ⇒ Terse

Returns a new instance of Terse.



10
11
12
13
14
15
# File 'lib/wavefront-cli/display/printer/terse.rb', line 10

def initialize(data, *keys)
  @data = data
  @keys = keys
  @fmt_string = format_string.rstrip
  @out = prep_output
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



8
9
10
# File 'lib/wavefront-cli/display/printer/terse.rb', line 8

def data
  @data
end

#fmt_stringObject (readonly)

Returns the value of attribute fmt_string.



8
9
10
# File 'lib/wavefront-cli/display/printer/terse.rb', line 8

def fmt_string
  @fmt_string
end

#keysObject (readonly)

Returns the value of attribute keys.



8
9
10
# File 'lib/wavefront-cli/display/printer/terse.rb', line 8

def keys
  @keys
end

Instance Method Details

#format_stringString

Returns a Ruby format string for each line.

Returns:

  • (String)

    a Ruby format string for each line



19
20
21
22
# File 'lib/wavefront-cli/display/printer/terse.rb', line 19

def format_string
  lk = longest_keys
  keys.each_with_object('') { |k, out| out.<< "%-#{lk[k]}s  " }
end

#longest_keysHash

Find the length of the longest value for each member of @keys, in @data.

Returns:

  • (Hash)

    with the same keys as :keys and Integer values



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/wavefront-cli/display/printer/terse.rb', line 29

def longest_keys
  keys.each_with_object(Hash[*keys.map { |k| [k, 0] }.flatten]) \
  do |k, aggr|
    data.each do |obj|
      next unless obj.key?(k)
      val = obj[k]
      val = val.join(', ') if val.is_a?(Array)
      aggr[k] = val.size if val.size > aggr[k]
    end
  end
end

#prep_outputObject

Print multiple column output. This method does no word wrapping.

Parameters:

  • keys (Symbol)

    the keys you want in the output. They will be printed in the order given.



47
48
49
50
51
52
# File 'lib/wavefront-cli/display/printer/terse.rb', line 47

def prep_output
  data.each_with_object([]) do |o, aggr|
    args = keys.map { |k| o[k].is_a?(Array) ? o[k].join(', ') : o[k] }
    aggr.<< format(fmt_string, *args).rstrip
  end
end