class WavefrontDisplayPrinter::Terse

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

Attributes

data[R]

Public Class Methods

new(data, keys) click to toggle source

@param data [Hash] data to display, from a response object @param keys [Array] keys to display, in order

# File lib/wavefront-cli/display/printer/terse.rb, line 16
def initialize(data, keys)
  @data = stringify(data, keys)
  @fmt  = format_string(data, keys)
end

Public Instance Methods

format_string(data, keys) click to toggle source

@return [String] used to format output

# File lib/wavefront-cli/display/printer/terse.rb, line 23
def format_string(data, keys)
  keys.map { |k| "%-#{data.longest_value_of(k)}<#{k}>s" }.join('  ')
end
map_to_string(value) click to toggle source

If we get a hash as a value (tags, for instance) we squash it down to a “key1=val1;key2=val2” kind of string. Note that this doesn't handle nested hashes. It shouldn't have to.

@param value [Map,Hash] { k1: 'v1', k2: 'v2' } @return [String] 'k1=v1;k2=v2'

# File lib/wavefront-cli/display/printer/terse.rb, line 55
def map_to_string(value)
  value.map { |k, v| "#{k}=#{v}" }.join(';')
end
stringify(data, keys) click to toggle source

Flatten nested data. @param data [Map,Hash] data to flatten @param keys [Array] keys of interest. We don't bother working on

things we'll only throw away
# File lib/wavefront-cli/display/printer/terse.rb, line 32
def stringify(data, keys)
  data.map { |e| e.tap { keys.each { |k| e[k] = value_as_string(e[k]) } } }
end
to_s() click to toggle source

Format every element according to the format string @fmt

# File lib/wavefront-cli/display/printer/terse.rb, line 61
def to_s
  data.map { |e| format(@fmt, e).rstrip }.join("\n")
rescue KeyError
  raise WavefrontCli::Exception::UserError, 'field not found'
end
value_as_string(value) click to toggle source

Turn a (potentially) more complicated structure into a string @param value [Object] @return [String]

# File lib/wavefront-cli/display/printer/terse.rb, line 40
def value_as_string(value)
  return value.join(', ') if value.is_a?(Array)

  return map_to_string(value) if value.is_a?(Map)

  value
end