diff --git a/lib/boson/runners/bin_runner.rb b/lib/boson/runners/bin_runner.rb index 5a3145d..694a839 100644 --- a/lib/boson/runners/bin_runner.rb +++ b/lib/boson/runners/bin_runner.rb @@ -20,8 +20,16 @@ module Boson # [:index] Updates index for given libraries allowing you to use them. This is useful if Boson's autodetection of # changed libraries isn't picking up your changes. Since this option has a :bool_default attribute, arguments # passed to this option need to be passed with '=' i.e. '--index=my_lib'. - # [:render] Pretty formats the results of commands without options. Handy for commands that return arrays. + # [:render] Toggles the auto-rendering done for commands that don't have views. Doesn't affect commands that already have views. + # Default is false. Also see Auto Rendering section below. # [:pager_toggle] Toggles Hirb's pager in case you'd like to pipe to another command. + # + # ==== Auto Rendering + # Commands that don't have views (defined via render_options) have their return value auto-rendered as a view as follows: + # * nil,false and true aren't rendered + # * arrays are rendered with Hirb's tables + # * non-arrays are printed with inspect() + # * Any of these cases can be toggled to render/not render with the global option :render class BinRunner < Runner GLOBAL_OPTIONS = { :verbose=>{:type=>:boolean, :desc=>"Verbose description of loading libraries or help"}, @@ -107,10 +115,8 @@ def parse_args(args) end def render_output(output) - if Scientist.global_options && !Scientist.rendered && !View.silent_object?(output) - puts output.inspect - elsif !Scientist.global_options && @options[:render] - View.render(output, :silence_booleans=>true) + if (!Scientist.rendered && !View.inspected_object?(output)) ^ @options[:render] + View.render(output, :inspect=>!output.is_a?(Array) || (Scientist.global_options || {})[:render]) end end diff --git a/lib/boson/scientist.rb b/lib/boson/scientist.rb index e3c3414..f411d76 100644 --- a/lib/boson/scientist.rb +++ b/lib/boson/scientist.rb @@ -75,7 +75,8 @@ module Scientist class Error < StandardError; end class EscapeGlobalOption < StandardError; end - attr_reader :global_options, :rendered, :option_parsers, :command_options + attr_reader :option_parsers, :command_options + attr_accessor :global_options, :rendered @no_option_commands ||= [] GLOBAL_OPTIONS = { :help=>{:type=>:boolean, :desc=>"Display a command's help"}, diff --git a/lib/boson/view.rb b/lib/boson/view.rb index 67be4ea..377fedf 100644 --- a/lib/boson/view.rb +++ b/lib/boson/view.rb @@ -12,8 +12,8 @@ def enable # Renders any object via Hirb. Options are passed directly to # {Hirb::Console.render_output}[http://tagaholic.me/hirb/doc/classes/Hirb/Console.html#M000011]. def render(object, options={}, return_obj=false) - if silent_object?(object) - puts(object.inspect) unless options[:silence_booleans] + if options[:inspect] || inspected_object?(object) + puts(object.inspect) else render_object(object, options, return_obj) end @@ -34,7 +34,7 @@ def toggle_pager Hirb::View.toggle_pager end - def silent_object?(obj) + def inspected_object?(obj) [nil,false,true].include?(obj) end diff --git a/test/bin_runner_test.rb b/test/bin_runner_test.rb index 8874aa9..3602d72 100644 --- a/test/bin_runner_test.rb +++ b/test/bin_runner_test.rb @@ -154,6 +154,42 @@ def index(options={}) end end + context "render_output" do + before(:each) { Scientist.rendered = false; BinRunner.instance_eval "@options = {}" } + + test "doesn't render when nil, false or true" do + View.expects(:render).never + [nil, false, true].each do |e| + BinRunner.render_output e + end + end + + test "doesn't render when rendered with Scientist" do + Scientist.rendered = true + View.expects(:render).never + BinRunner.render_output 'blah' + end + + test "renders with inspect when non-array" do + ['man', {:a=>true}, :ok].each do |e| + View.expects(:puts).with(e.inspect) + BinRunner.render_output e + end + end + + test "renders with inspect when Scientist rendering toggled off with :render" do + Scientist.global_options = {:render=>true} + View.expects(:puts).with([1,2].inspect) + BinRunner.render_output [1,2] + Scientist.global_options = nil + end + + test "renders with hirb when array" do + View.expects(:render_object) + BinRunner.render_output [1,2,3] + end + end + test "parse_args only translates options before command" do BinRunner.parse_args(['-v', 'com', '-v']).should == ["com", {:verbose=>true}, ['-v']] BinRunner.parse_args(['com', '-v']).should == ["com", {}, ['-v']]