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:

  • of data to display

  • (defaults to: nil)

    requred fields

  • (defaults to: nil)

    an override for @data

  • (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



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

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.



6
7
8
# File 'lib/wavefront-cli/display/printer/long.rb', line 6

def kw
  @kw
end

#listObject (readonly)

Returns the value of attribute list.



6
7
8
# File 'lib/wavefront-cli/display/printer/long.rb', line 6

def list
  @list
end

#optsObject (readonly)

Returns the value of attribute opts.



6
7
8
# File 'lib/wavefront-cli/display/printer/long.rb', line 6

def opts
  @opts
end

Instance Method Details

#default_optsObject

Default options. Can all be overridden by passing them in the initializer options hash.



28
29
30
31
32
33
# File 'lib/wavefront-cli/display/printer/long.rb', line 28

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

#line(key, val) ⇒ Object

rubocop:enable Metrics/AbcSize



108
109
110
111
112
113
114
115
116
# File 'lib/wavefront-cli/display/printer/long.rb', line 108

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('%s%s', key, 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:

  • of the form returned by #make_list

Returns:



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

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:

  • the thing you wish to present

  • (defaults to: [])

    aggregates the output array. Don’t set this yourself

  • (defaults to: 0)

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

  • (defaults to: nil)

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

Returns:

  • where each sub-array is of the form

    key, value, depth


71
72
73
74
75
76
77
78
79
# File 'lib/wavefront-cli/display/printer/long.rb', line 71

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:

  • raw data

  • (defaults to: nil)

    fields to keep in @data. Nil means everything

Returns:



40
41
42
43
# File 'lib/wavefront-cli/display/printer/long.rb', line 40

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:

  • value

Returns:

  • value with all HTML stripped out



50
51
52
53
# File 'lib/wavefront-cli/display/printer/long.rb', line 50

def preened_value(value)
  return value unless value.is_a?(String) && value =~ /<.*>/
  value.gsub(%r{<\/?[^>]*>}, '').delete("\n")
end

#smart_value(val) ⇒ Object



81
82
83
# File 'lib/wavefront-cli/display/printer/long.rb', line 81

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

#to_sString

Turn the list made by #make_list into user output rubocop:disable Metrics/AbcSize

Returns:



98
99
100
101
102
103
104
105
# File 'lib/wavefront-cli/display/printer/long.rb', line 98

def to_s
  list.map do |e|
    indent = ' ' * opts[:indent] * e.last
    key_str = (indent + e.first.to_s + '  ' * kw)[0..kw]
    val = e[1] == :separator ? '-' * (TW - key_str.length) : e[1]
    line(key_str, val)
  end.join("\n")
end