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
rubocop:enable Metrics/AbcSize.
-
#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
-
#to_s ⇒ String
Turn the list made by #make_list into user output rubocop:disable Metrics/AbcSize.
Constructor Details
#initialize(data, fields = nil, modified_data = nil, options = {}) ⇒ Long
Returns a new instance of Long.
18 19 20 21 22 23 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 18 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.
6 7 8 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 6 def kw @kw end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
6 7 8 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 6 def list @list end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
6 7 8 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 6 def opts @opts end |
Instance Method Details
#default_opts ⇒ Object
Default options. Can all be overridden by passing them in the initializer options hash.
28 29 30 31 32 33 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 28 def default_opts { indent: 2, padding: 2, separator: true, none: true } end |
#line(key, val) ⇒ Object
rubocop:enable Metrics/AbcSize
108 109 110 111 112 113 114 115 116 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 108 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('%s%s', key, 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.
90 91 92 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 90 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 }
71 72 73 74 75 76 77 78 79 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 71 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
40 41 42 43 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 40 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
50 51 52 53 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 50 def preened_value(value) return value unless value.is_a?(String) && value =~ /<.*>/ value.gsub(%r{<\/?[^>]*>}, '').delete("\n") end |
#smart_value(val) ⇒ Object
81 82 83 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 81 def smart_value(val) val.to_s.empty? && opts[:none] ? '<none>' : preened_value(val) end |
#to_s ⇒ String
Turn the list made by #make_list into user output rubocop:disable Metrics/AbcSize
98 99 100 101 102 103 104 105 |
# File 'lib/wavefront-cli/display/printer/long.rb', line 98 def to_s list.map do |e| indent = ' ' * opts[:indent] * e.last key_str = (indent + e.first.to_s + ' ' * kw)[0..kw] val = e[1] == :separator ? '-' * (TW - key_str.length) : e[1] line(key_str, val) end.join("\n") end |