Class: WavefrontDisplayPrinter::Long
- Defined in:
- lib/wavefront-cli/display/printer/long.rb
Overview
Print the long indented descriptions of things
Instance Attribute Summary collapse
-
#hide_blank ⇒ Object
readonly
Returns the value of attribute hide_blank.
-
#indent ⇒ Object
readonly
Returns the value of attribute indent.
-
#indent_step ⇒ Object
readonly
Returns the value of attribute indent_step.
-
#indent_str ⇒ Object
readonly
Returns the value of attribute indent_str.
-
#kw ⇒ Object
readonly
Returns the value of attribute kw.
Attributes inherited from Base
Instance Method Summary collapse
-
#_two_columns(data, key_col_width = nil, fields = nil) ⇒ Object
A recursive function which displays a key-value hash in two columns.
-
#add_array(key, value_arr) ⇒ Nil
Add a key-value pair to the output when value is an array.
-
#add_hash(key, value, arr_size = 0, arr_index = 0) ⇒ Nil
Add a hash to the output.
-
#add_line(*args) ⇒ Object
Add a line, prepped by #mk_line() to the out array.
-
#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.
-
#blank?(value) ⇒ Boolean
Return true if this line is blank and we don’t want to print blank lines.
-
#initialize(data, fields = nil, modified_data = nil) ⇒ Long
constructor
A new instance of Long.
-
#mk_indent(indent) ⇒ Object
Make the string which is prepended to each line.
-
#mk_line(key, value = '', term_width = TW) ⇒ Object
Print a single line of output, handling the necessary indentation and tabulation.
-
#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.
-
#preen_fields(item, fields = nil) ⇒ Hash, Map
Drop any fields not required.
-
#preen_value(value) ⇒ String
Remove HTML and stuff.
Methods inherited from Base
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_blank ⇒ Object (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 |
#indent ⇒ Object (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_step ⇒ Object (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_str ⇒ Object (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 |
#kw ⇒ Object (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
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
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
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
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.
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
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.
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.
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 |