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, kw = 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.

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.



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

def _two_columns(data, kw = nil, fields = nil)
  [data].flatten.each do |item|
    preen_fields(item, fields)
    kw = key_width(item) unless kw
    @kw = kw unless @kw
    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)


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

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)


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

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.



182
183
184
# File 'lib/wavefront-cli/display/printer/long.rb', line 182

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

#add_rule(kw) ⇒ Object

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



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

def add_rule(kw)
  add_line(nil, '-' * (TW - kw - 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)


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

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.



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

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

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

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

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) (defaults to: TW)

    terminal width



173
174
175
176
177
178
# File 'lib/wavefront-cli/display/printer/long.rb', line 173

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

  indent_str + format("%-#{kw}s%s", key, value)
               .fold(tw, 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)


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

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)


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

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



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

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