Class: WavefrontDisplayPrinter::Long
- Inherits:
-
Object
- Object
- WavefrontDisplayPrinter::Long
- Defined in:
- lib/wavefront-cli/display/printer/long.rb
Overview
Print the long indented descriptions of things
Instance Attribute Summary collapse
-
#kw ⇒ Object
readonly
Returns the value of attribute kw.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#default_opts ⇒ Object
Default options.
-
#initialize(data, fields = nil, modified_data = nil, options = {}) ⇒ Long
constructor
A new instance of Long.
- #line(key, val) ⇒ Object
-
#longest_key_col(data) ⇒ Integer
Works out what the width of the left-hand (key) column needs to be.
-
#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.
- #preened_data(data, fields = nil) ⇒ Hash
-
#preened_value(value) ⇒ String
Remove HTML and stuff.
- #smart_value(val) ⇒ Object
- #stringify_line(item) ⇒ Object
-
#to_s ⇒ String
Turn the list made by #make_list into user output.
Constructor Details
#initialize(data, fields = nil, modified_data = nil, options = {}) ⇒ Long
Returns a new instance of Long.
23 24 25 26 27 28 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 23 def initialize(data, fields = nil, modified_data = nil, = {}) @opts = default_opts.merge() data = preened_data(data, fields) @list = make_list(modified_data || data) @kw = longest_key_col(list) end |
Instance Attribute Details
#kw ⇒ Object (readonly)
Returns the value of attribute kw.
10 11 12 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 10 def kw @kw end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
10 11 12 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 10 def list @list end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
10 11 12 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 10 def opts @opts end |
Instance Method Details
#default_opts ⇒ Object
Default options. Can all be overridden by passing them in the initializer options hash. After sep_depth indentations we do not print separator lines
34 35 36 37 38 39 40 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 34 def default_opts { indent: 2, padding: 2, separator: true, sep_depth: 3, none: true } end |
#line(key, val) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 121 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('%<padded_key>s%<value>s', padded_key: key, value: 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.
99 100 101 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 99 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 }
80 81 82 83 84 85 86 87 88 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 80 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
47 48 49 50 51 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 47 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
58 59 60 61 62 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 58 def preened_value(value) return value unless value.is_a?(String) && value =~ /<.*>/ value.gsub(%r{</?[^>]*>}, '').delete("\n") end |
#smart_value(val) ⇒ Object
90 91 92 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 90 def smart_value(val) val.to_s.empty? && opts[:none] ? '<none>' : preened_value(val) end |
#stringify_line(item) ⇒ Object
112 113 114 115 116 117 118 119 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 112 def stringify_line(item) key_str = format('%<indent>s%<key>s%<gutter>s', indent: padding(item[2]), key: item[0].to_s, gutter: ' ' * kw)[0..kw] line(key_str, item[1] == :separator ? separator_line(key_str.size) : item[1]) end |
#to_s ⇒ String
Turn the list made by #make_list into user output
106 107 108 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 106 def to_s list.map { |item| stringify_line(item) }.join("\n") end |