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, kw = 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(kw) ⇒ 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 = '', tw = 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, 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.
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
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
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
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.
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.
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.
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.
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
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 |