diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 6d8f24e5..9c1d2cc1 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -325,8 +325,8 @@ class Pry # Add a new section to the output. Outputs nothing if the section would be empty. def output_section(heading, body) return "" if body.compact.empty? - table = Pry::Helpers.tablify_to_screen_width(body) - "#{text.bold(color(:heading, heading))}: \n#{table}\n" + fancy_heading = text.bold(color(:heading, heading)) + Pry::Helpers.tablify_or_one_line(fancy_heading, body) end # Color output based on config.ls.*_color diff --git a/lib/pry/helpers/table.rb b/lib/pry/helpers/table.rb index 7064b97c..533fb3ad 100644 --- a/lib/pry/helpers/table.rb +++ b/lib/pry/helpers/table.rb @@ -1,14 +1,23 @@ class Pry module Helpers - def self.tablify_to_screen_width(things) - things = things.compact - - if TerminalInfo.screen_size.nil? - return things.join(Pry.config.ls.separator) + def self.tablify_or_one_line(heading, things) + plain_heading = Pry::Helpers::Text.strip_color(heading) + attempt = Table.new(things, :column_count => things.size) + if attempt.fits_on_line?(TerminalInfo.width! - plain_heading.size - 2) + "#{heading}: #{attempt}\n" + else + "#{heading}: \n#{tablify_to_screen_width(things, :indent => ' ')}\n" end + end - screen_width = (TerminalInfo.screen_size || [25, 80])[1] - tablify(things, screen_width) + def self.tablify_to_screen_width(things, options = {}) + things = things.compact + if indent = options[:indent] + usable_width = TerminalInfo.width! - indent.size + tablify(things, usable_width).to_s.gsub(/^/, indent) + else + tablify(things, TerminalInfo.width!).to_s + end end def self.tablify(things, line_length) diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index b16d0a20..112744ff 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -323,11 +323,11 @@ class Pry config.extra_sticky_locals = {} config.ls ||= OpenStruct.new({ - :heading_color => :default, + :heading_color => :bright_blue, :public_method_color => :default, - :private_method_color => :green, - :protected_method_color => :yellow, + :private_method_color => :blue, + :protected_method_color => :blue, :method_missing_color => :bright_red, :local_var_color => :yellow, diff --git a/lib/pry/terminal_info.rb b/lib/pry/terminal_info.rb index f23e122a..5fed220a 100644 --- a/lib/pry/terminal_info.rb +++ b/lib/pry/terminal_info.rb @@ -11,6 +11,11 @@ class Pry::TerminalInfo end end + # Return a screen width or a default if it fails. + def self.width! default = 80 + (screen_size || [nil, default])[1] + end + def self.actual_screen_size [ # Some readlines also provides get_screen_size. diff --git a/spec/commands/ls_spec.rb b/spec/commands/ls_spec.rb index 55607552..d4faf32a 100644 --- a/spec/commands/ls_spec.rb +++ b/spec/commands/ls_spec.rb @@ -37,7 +37,7 @@ describe "ls" do describe "methods" do it "should show public methods by default" do output = pry_eval("ls Class.new{ def goo; end; public :goo }.new") - output.should =~ /methods: \ngoo/ + output.should =~ /methods: goo/ end it "should not show protected/private by default" do @@ -46,12 +46,12 @@ describe "ls" do end it "should show public methods with -p" do - pry_eval("ls -p Class.new{ def goo; end }.new").should =~ /methods: \ngoo/ + pry_eval("ls -p Class.new{ def goo; end }.new").should =~ /methods: goo/ end it "should show protected/private methods with -p" do - pry_eval("ls -pM Class.new{ def goo; end; protected :goo }").should =~ /methods: \ngoo/ - pry_eval("ls -p Class.new{ def goo; end; private :goo }.new").should =~ /methods: \ngoo/ + pry_eval("ls -pM Class.new{ def goo; end; protected :goo }").should =~ /methods: goo/ + pry_eval("ls -p Class.new{ def goo; end; private :goo }.new").should =~ /methods: goo/ end it "should work for objects with an overridden method method" do diff --git a/spec/helpers/table_spec.rb b/spec/helpers/table_spec.rb index 297336c0..9eac1ee3 100644 --- a/spec/helpers/table_spec.rb +++ b/spec/helpers/table_spec.rb @@ -80,4 +80,7 @@ asfadsssaaad fasfaafdssd s end end + describe 'decide between one-line or indented output' do + Pry::Helpers.tablify_or_one_line('head', %w(ing)).should == 'head: ing' + end end