Class: WavefrontDisplayPrinter::Terse
- Defined in:
- lib/wavefront-cli/display/printer/terse.rb
Overview
Print things which are per-row. The terse listings, primarily
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#fmt_string ⇒ Object
readonly
Returns the value of attribute fmt_string.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Attributes inherited from Base
Instance Method Summary collapse
-
#format_string ⇒ String
A Ruby format string for each line.
-
#initialize(data, *keys) ⇒ Terse
constructor
A new instance of Terse.
-
#longest_keys ⇒ Hash
Find the length of the longest value for each member of @keys, in @data.
-
#prep_output ⇒ Object
Print multiple column output.
Methods inherited from Base
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
#data ⇒ Object (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_string ⇒ Object (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 |
#keys ⇒ Object (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_string ⇒ String
Returns 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_keys ⇒ Hash
Find the length of the longest value for each member of @keys, in @data.
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_output ⇒ Object
Print multiple column output. This method does no word wrapping.
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 |