diff options
| author | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-18 13:27:17 -0700 |
|---|---|---|
| committer | Daniel Pittman <daniel@puppetlabs.com> | 2011-04-19 10:53:31 -0700 |
| commit | be23b8423ba77a5935586e277bf543cd54b9dec7 (patch) | |
| tree | 96e2d4c93c63605c469e22d16417170216012abf /lib/puppet/application | |
| parent | e6caa2459a75cdfb015e7f4754dd5b44d166b0b5 (diff) | |
| download | puppet-be23b8423ba77a5935586e277bf543cd54b9dec7.tar.gz puppet-be23b8423ba77a5935586e277bf543cd54b9dec7.tar.xz puppet-be23b8423ba77a5935586e277bf543cd54b9dec7.zip | |
(#7013) better default rendering support for faces
We have some specific requirements around rendering, including the ability of
authors of actions to add nice, custom rendering. To support that we want
solid "basic" rendering for human-focused output.
This implements that generic rendering correctly and to spec, to give a sound
basis that we can build on for extensible rendering.
(#7013) better default rendering support for faces
We have some specific requirements around rendering, including the ability of
authors of actions to add nice, custom rendering. To support that we want
solid "basic" rendering for human-focused output.
This implements that generic rendering correctly and to spec, to give a sound
basis that we can build on for extensible rendering.
Reviewed-By: Max Martin <max@puppetlabs.com>
Diffstat (limited to 'lib/puppet/application')
| -rw-r--r-- | lib/puppet/application/face_base.rb | 35 |
1 files changed, 31 insertions, 4 deletions
diff --git a/lib/puppet/application/face_base.rb b/lib/puppet/application/face_base.rb index 7b5bffe54..947204d68 100644 --- a/lib/puppet/application/face_base.rb +++ b/lib/puppet/application/face_base.rb @@ -1,6 +1,7 @@ require 'puppet/application' require 'puppet/face' require 'optparse' +require 'pp' class Puppet::Application::FaceBase < Puppet::Application should_parse_config @@ -36,14 +37,40 @@ class Puppet::Application::FaceBase < Puppet::Application # Override this if you need custom rendering. def render(result) - render_method = Puppet::Network::FormatHandler.format(format).render_method - if render_method == "to_pson" - jj result + if format then + render_method = Puppet::Network::FormatHandler.format(format).render_method + if render_method == "to_pson" + jj result + else + result.send(render_method) + end else - result.send(render_method) + render_for_humans(result) end end + def render_for_humans(result) + # String to String + return result if result.is_a? String + + # Simple hash to table + if result.is_a? Hash and result.keys.all? { |x| x.is_a? String or x.is_a? Numeric } + output = '' + column_a = result.map do |k,v| k.to_s.length end.max + 2 + column_b = 79 - column_a + result.sort_by { |k,v| k.to_s } .each do |key, value| + output << key.to_s.ljust(column_a) + output << PP.pp(value, '', column_b). + chomp.gsub(/\n */) { |x| x + (' ' * column_a) } + output << "\n" + end + return output + end + + # ...or pretty-print the inspect outcome. + return result.pretty_inspect + end + def preinit super Signal.trap(:INT) do |
