Class: Chef::Knife::Core::TextFormatter
- Defined in:
- lib/chef/knife/core/text_formatter.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#ui ⇒ Object
readonly
Returns the value of attribute ui.
Instance Method Summary collapse
- #formatted_data ⇒ Object
- #indent_key_value(key, value, justify_width, indent) ⇒ Object
- #indent_line(string, indent) ⇒ Object
-
#initialize(data, ui) ⇒ TextFormatter
constructor
A new instance of TextFormatter.
-
#should_enumerate?(value) ⇒ Boolean
Ruby 1.8 Strings include enumberable, which is not what we want.
- #stringify_value(data) ⇒ Object
- #text_format(data, indent = 0) ⇒ Object
Constructor Details
#initialize(data, ui) ⇒ TextFormatter
Returns a new instance of TextFormatter.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/chef/knife/core/text_formatter.rb', line 27 def initialize(data, ui) @ui = ui @data = if data.respond_to?(:display_hash) data.display_hash elsif data.kind_of?(Array) data elsif data.respond_to?(:to_hash) data.to_hash else data end end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
24 25 26 |
# File 'lib/chef/knife/core/text_formatter.rb', line 24 def data @data end |
#ui ⇒ Object (readonly)
Returns the value of attribute ui.
25 26 27 |
# File 'lib/chef/knife/core/text_formatter.rb', line 25 def ui @ui end |
Instance Method Details
#formatted_data ⇒ Object
40 41 42 |
# File 'lib/chef/knife/core/text_formatter.rb', line 40 def formatted_data @formatted_data ||= text_format(data) end |
#indent_key_value(key, value, justify_width, indent) ⇒ Object
82 83 84 85 86 87 88 89 90 |
# File 'lib/chef/knife/core/text_formatter.rb', line 82 def indent_key_value(key, value, justify_width, indent) lines = value.to_s.split("\n") if lines.size > 1 total_indent = (2 * indent) + justify_width + 1 indent_line("#{key} #{lines.shift}", indent) << lines.map {|l| (" " * total_indent) + l << "\n" }.join("") else indent_line("#{key} #{stringify_value(value)}", indent) end end |
#indent_line(string, indent) ⇒ Object
78 79 80 |
# File 'lib/chef/knife/core/text_formatter.rb', line 78 def indent_line(string, indent) (" " * indent) << "#{string}\n" end |
#should_enumerate?(value) ⇒ Boolean
Ruby 1.8 Strings include enumberable, which is not what we want. So we have this heuristic to detect hashes and arrays instead.
74 75 76 |
# File 'lib/chef/knife/core/text_formatter.rb', line 74 def should_enumerate?(value) ((value.respond_to?(:keys) && !value.empty? ) || ( value.kind_of?(Array) && (value.size > 1 || should_enumerate?(value.first) ))) end |
#stringify_value(data) ⇒ Object
92 93 94 |
# File 'lib/chef/knife/core/text_formatter.rb', line 92 def stringify_value(data) data.kind_of?(String) ? data : data.to_s end |
#text_format(data, indent = 0) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/chef/knife/core/text_formatter.rb', line 44 def text_format(data, indent=0) buffer = '' if data.respond_to?(:keys) justify_width = data.keys.map {|k| k.to_s.size }.max.to_i + 2 data.sort.each do |key, value| justified_key = ui.color("#{key}:".ljust(justify_width), :cyan) if should_enumerate?(value) buffer << indent_line(justified_key, indent) buffer << text_format(value, indent + 1) else buffer << indent_key_value(justified_key, value, justify_width, indent) end end elsif data.kind_of?(Array) data.each do |item| if should_enumerate?(data) buffer << text_format(item, indent + 1) else buffer << indent_line(item, indent) end end else buffer << indent_line(stringify_value(data), indent) end buffer end |