Class: WavefrontDisplayPrinter::Long

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

Overview

Print the long indented descriptions of things

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, fields = nil, modified_data = nil) ⇒ Long

Returns a new instance of Long.



11
12
13
14
15
16
17
# File 'lib/wavefront-cli/display/printer/long.rb', line 11

def initialize(data, fields = nil, modified_data = nil)
  @out = []
  @indent = 0
  @indent_step = 2
  @hide_blank = true
  _two_columns(modified_data || data, nil, fields)
end

Instance Attribute Details

#hide_blankObject (readonly)

Returns the value of attribute hide_blank.



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

def hide_blank
  @hide_blank
end

#indentObject (readonly)

Returns the value of attribute indent.



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

def indent
  @indent
end

#indent_stepObject (readonly)

Returns the value of attribute indent_step.



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

def indent_step
  @indent_step
end

#indent_strObject (readonly)

Returns the value of attribute indent_str.



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

def indent_str
  @indent_str
end

#kwObject (readonly)

Returns the value of attribute kw.



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

def kw
  @kw
end

Instance Method Details

#_two_columns(data, key_col_width = nil, fields = nil) ⇒ Object

A recursive function which displays a key-value hash in two columns. The key column width is automatically calculated. Multiple-value ‘v’s are printed one per line. Hashes are nested.

rubocop:disable Metrics/AbcSize

Parameters:

  • data (Array)

    and array of objects to display. Each object should be a hash.

  • indent (Integer)

    how many characters to indent the current data.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/wavefront-cli/display/printer/long.rb', line 31

def _two_columns(data, key_col_width = nil, fields = nil)
  [data].flatten.each do |item|
    preen_fields(item, fields)
    key_col_width ||= key_width(item)
    @kw ||= key_col_width
    mk_indent(indent)
    item.each { |k, v| parse_line(k, v) }
    add_line(nil) if indent.zero?
  end

  @indent -= indent_step if indent > 0
  @kw += 2
  mk_indent(indent)
end

#add_array(key, value_arr) ⇒ Nil

Add a key-value pair to the output when value is an array. It will put the key and the first value element on the first line, with subsequent value elements aligned at the same offset, but with no key. If any value element is a hash, it is handled by a separate method. For instance:

key value1

value2
value3

Parameters:

  • key (String)

    the key

  • value_arr (Array)

    an array of values

Returns:

  • (Nil)


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

def add_array(key, value_arr)
  value_arr.each_with_index do |element, index|
    if element.is_a?(Hash)
      add_hash(key, element, value_arr.size, index)
    else
      add_line(index.zero? ? key : nil, element)
    end
  end
end

#add_hash(key, value, arr_size = 0, arr_index = 0) ⇒ Nil

Add a hash to the output. It will put the key on a line on its own, followed by other keys indented. All values are aligned to the same point. If this hash is a member of an array, we are able to print a horizontal rule at the end of it. We don’t do this if it is the final member of the array.

For instance:

key
  subkey1    value1
  subkey2    value2

Parameters:

  • key (String)

    the key

  • value (Hash)

    hash of values to display

  • size (Integer)

    the size of the parent array, if there is one

  • index (Integer)

    the index of this element in parent array, if there is one.

Returns:

  • (Nil)


143
144
145
146
147
148
149
# File 'lib/wavefront-cli/display/printer/long.rb', line 143

def add_hash(key, value, arr_size = 0, arr_index = 0)
  add_line(key) if arr_index.zero?
  @indent += indent_step
  @kw -= 2
  _two_columns([value], kw - indent_step)
  add_rule(kw) if arr_index + 1 < arr_size
end

#add_line(*args) ⇒ Object

Add a line, prepped by #mk_line() to the out array.



186
187
188
# File 'lib/wavefront-cli/display/printer/long.rb', line 186

def add_line(*args)
  @out.<< mk_line(*args)
end

#add_rule(key_col_width) ⇒ Object

Add a horizontal rule, from the start of the second column to just shy of the end of the terminal



154
155
156
# File 'lib/wavefront-cli/display/printer/long.rb', line 154

def add_rule(key_col_width)
  add_line(nil, '-' * (TW - key_col_width - 4))
end

#blank?(value) ⇒ Boolean

Return true if this line is blank and we don’t want to print blank lines

Parameters:

  • value (Object)

    thing to check

Returns:

  • (Boolean)


74
75
76
# File 'lib/wavefront-cli/display/printer/long.rb', line 74

def blank?(value)
  value.respond_to?(:empty?) && value.empty? && hide_blank
end

#mk_indent(indent) ⇒ Object

Make the string which is prepended to each line. Stepping is controlled by @indent_step.

Parameters:

  • indent (Integer)

    how many characters to indent by.



163
164
165
# File 'lib/wavefront-cli/display/printer/long.rb', line 163

def mk_indent(indent)
  @indent_str = ' ' * indent
end

#mk_line(key, value = '', term_width = TW) ⇒ Object

Print a single line of output, handling the necessary indentation and tabulation.

rubocop:disable Metrics/AbcSize

Parameters:

  • key (String)

    what to print in the first (key) column. Make this an empty string to print

  • val (String, Numeric)

    what to print in the second column

  • tw (Integer)

    terminal width



176
177
178
179
180
181
# File 'lib/wavefront-cli/display/printer/long.rb', line 176

def mk_line(key, value = '', term_width = TW)
  return indent_str + ' ' * kw + value if !key || key.empty?

  indent_str + format("%-#{kw}s%s", key, value)
               .fold(term_width, kw + indent_str.size, '').rstrip
end

#parse_line(key, value) ⇒ Nil

Parse a line and add it to the output or pass it on to another method which knows how to add it to the output.

Parameters:

  • key (String)

    a key

  • value (Object)

    the value: could be anything

Returns:

  • (Nil)


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/wavefront-cli/display/printer/long.rb', line 85

def parse_line(key, value)
  return if blank?(value)

  value = preen_value(value)

  if value.is_a?(Hash)
    add_hash(key, value)
  elsif value.is_a?(Array)
    add_array(key, value)
  else
    add_line(key, value)
  end
end

#preen_fields(item, fields = nil) ⇒ Hash, Map

Drop any fields not required.

Parameters:

  • item (Hash, Map)

    the raw data

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

    the fields we wish to keep

Returns:

  • (Hash, Map)


53
54
55
56
# File 'lib/wavefront-cli/display/printer/long.rb', line 53

def preen_fields(item, fields = nil)
  return item unless fields
  item.keep_if { |k, _v| fields.include?(k.to_sym) }
end

#preen_value(value) ⇒ String

Remove HTML and stuff

Parameters:

Returns:

  • (String)

    value with all HTML stripped out



63
64
65
66
# File 'lib/wavefront-cli/display/printer/long.rb', line 63

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