Class: WavefrontDisplayPrinter::Long

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

Overview

Print the long indented descriptions of things

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, fields = nil, modified_data = nil, options = {}) ⇒ Long

Returns a new instance of Long.

Parameters:

  • data (Hash)

    of data to display

  • fields (Array[Symbol]) (defaults to: nil)

    requred fields

  • modified_data (Hash) (defaults to: nil)

    an override for @data

  • options (Hash) (defaults to: {})

    keys can be indent: [Integer] by how many spaces nested objects should indent padding: [Integer] number of spaces between columns separator: [Bool] whether or not to print a line of dashes

    between objects in an array of objects
    

    none [Bool] whether or not to put ‘<none>’ for empty arrays



23
24
25
26
27
28
# File 'lib/wavefront-cli/display/printer/long.rb', line 23

def initialize(data, fields = nil, modified_data = nil, options = {})
  @opts = default_opts.merge(options)
  data  = preened_data(data, fields)
  @list = make_list(modified_data || data)
  @kw   = longest_key_col(list)
end

Instance Attribute Details

#kwObject (readonly)

Returns the value of attribute kw.



10
11
12
# File 'lib/wavefront-cli/display/printer/long.rb', line 10

def kw
  @kw
end

#listObject (readonly)

Returns the value of attribute list.



10
11
12
# File 'lib/wavefront-cli/display/printer/long.rb', line 10

def list
  @list
end

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/wavefront-cli/display/printer/long.rb', line 10

def opts
  @opts
end

Instance Method Details

#default_optsObject

Default options. Can all be overridden by passing them in the initializer options hash. After sep_depth indentations we do not print separator lines



34
35
36
37
38
39
40
# File 'lib/wavefront-cli/display/printer/long.rb', line 34

def default_opts
  { indent: 2,
    padding: 2,
    separator: true,
    sep_depth: 3,
    none: true }
end

#line(key, val) ⇒ Object



121
122
123
124
125
126
127
128
129
# File 'lib/wavefront-cli/display/printer/long.rb', line 121

def line(key, val)
  line_length = key.to_s.size + val.to_s.size

  if line_length > TW && val.is_a?(String)
    val = val.value_fold(key.to_s.size)
  end

  format('%<padded_key>s%<value>s', padded_key: key, value: val).rstrip
end

#longest_key_col(data) ⇒ Integer

Works out what the width of the left-hand (key) column needs to be. This considers indentation and padding.

Parameters:

  • data (Array)

    of the form returned by #make_list

Returns:

  • (Integer)


99
100
101
# File 'lib/wavefront-cli/display/printer/long.rb', line 99

def longest_key_col(data)
  data.map { |d| d[0].size + opts[:padding] + (opts[:indent] * d[2]) }.max
end

#make_list(data, aggr = [], depth = 0, last_key = nil) ⇒ Array[Array]

A recursive function which takes a structure, most likely a hash, and turns it into an array of arrays. This output is easily formatted into nicely laid-out columns by #to_s. Most of the parameters are used by the function itself. Make an array of hashes: { key, value, depth }

Parameters:

  • data (Object)

    the thing you wish to present

  • aggr (Array) (defaults to: [])

    aggregates the output array. Don’t set this yourself

  • depth (Integer) (defaults to: 0)

    how many layers of indentation are required. Don’t set this yourself.

  • last_key (String, Nil) (defaults to: nil)

    a memo used for printing arrays. Don’t set this yourself.

Returns:

  • (Array[Array])

    where each sub-array is of the form

    key, value, depth


80
81
82
83
84
85
86
87
88
# File 'lib/wavefront-cli/display/printer/long.rb', line 80

def make_list(data, aggr = [], depth = 0, last_key = nil)
  if data.is_a?(Hash)
    append_hash(data, aggr, depth)
  elsif data.is_a?(Array)
    append_array(data, aggr, depth, last_key)
  else
    aggr << ['', preened_value(data), depth]
  end
end

#preened_data(data, fields = nil) ⇒ Hash

Parameters:

  • data (Hash)

    raw data

  • fields (Array, Nil) (defaults to: nil)

    fields to keep in @data. Nil means everything

Returns:

  • (Hash)


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

def preened_data(data, fields = nil)
  return data if fields.nil?

  data.map { |d| d.select { |k| fields.include?(k.to_sym) }.to_h }
end

#preened_value(value) ⇒ String

Remove HTML and stuff

Parameters:

Returns:

  • (String)

    value with all HTML stripped out



58
59
60
61
62
# File 'lib/wavefront-cli/display/printer/long.rb', line 58

def preened_value(value)
  return value unless value.is_a?(String) && value =~ /<.*>/

  value.gsub(%r{</?[^>]*>}, '').delete("\n")
end

#smart_value(val) ⇒ Object



90
91
92
# File 'lib/wavefront-cli/display/printer/long.rb', line 90

def smart_value(val)
  val.to_s.empty? && opts[:none] ? '<none>' : preened_value(val)
end

#stringify_line(item) ⇒ Object

Parameters:

  • item (Array)

    of the form [key, value, indent_level]



112
113
114
115
116
117
118
119
# File 'lib/wavefront-cli/display/printer/long.rb', line 112

def stringify_line(item)
  key_str = format('%<indent>s%<key>s%<gutter>s',
                   indent: padding(item[2]),
                   key: item[0].to_s,
                   gutter: '  ' * kw)[0..kw]
  line(key_str,
       item[1] == :separator ? separator_line(key_str.size) : item[1])
end

#to_sString

Turn the list made by #make_list into user output

Returns:



106
107
108
# File 'lib/wavefront-cli/display/printer/long.rb', line 106

def to_s
  list.map { |item| stringify_line(item) }.join("\n")
end