summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorDaniel Pittman <daniel@puppetlabs.com>2011-05-04 10:30:52 -0700
committerDaniel Pittman <daniel@puppetlabs.com>2011-05-04 12:28:25 -0700
commita4e735e133898a376747491d5bbc1ca5692ac122 (patch)
tree1d479a95d91dbfb2cc38f80d8f76140ebbc53a99 /lib
parentd203853bc8b40732c2ba88a4e5396f00a1e3a4ec (diff)
downloadpuppet-a4e735e133898a376747491d5bbc1ca5692ac122.tar.gz
puppet-a4e735e133898a376747491d5bbc1ca5692ac122.tar.xz
puppet-a4e735e133898a376747491d5bbc1ca5692ac122.zip
(#7353) Add 'console' format to FormatHandler
This adds a console rendering format to the Network FormatHandler subsystem; it provides the same human-friendly textual rendering as the Faces application did, except it uses JSON rather than PP as the fall-back rendering mode. This paves the path for unification of all formatting into the same subsystem, rather than the half-measures we used to have. Reviewed-By: Jacob Helwig <jacob@puppetlabs.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/network/formats.rb36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/puppet/network/formats.rb b/lib/puppet/network/formats.rb
index 4ca3240d4..082c83ee3 100644
--- a/lib/puppet/network/formats.rb
+++ b/lib/puppet/network/formats.rb
@@ -160,3 +160,39 @@ end
# This is really only ever going to be used for Catalogs.
Puppet::Network::FormatHandler.create_serialized_formats(:dot, :required_methods => [:render_method])
+
+
+Puppet::Network::FormatHandler.create(:console,
+ :mime => 'text/x-console-text',
+ :weight => 0) do
+ def json
+ @json ||= Puppet::Network::FormatHandler.format(:pson)
+ end
+
+ def render(datum)
+ # String to String
+ return datum if datum.is_a? String
+ return datum if datum.is_a? Numeric
+
+ # Simple hash to table
+ if datum.is_a? Hash and datum.keys.all? { |x| x.is_a? String or x.is_a? Numeric }
+ output = ''
+ column_a = datum.map do |k,v| k.to_s.length end.max + 2
+ column_b = 79 - column_a
+ datum.sort_by { |k,v| k.to_s } .each do |key, value|
+ output << key.to_s.ljust(column_a)
+ output << json.render(value).
+ chomp.gsub(/\n */) { |x| x + (' ' * column_a) }
+ output << "\n"
+ end
+ return output
+ end
+
+ # ...or pretty-print the inspect outcome.
+ return json.render(datum)
+ end
+
+ def render_multiple(data)
+ data.collect(&:render).join("\n")
+ end
+end