From 14283aa51f0d7aae16ff28d6814bc10c64ebbdab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=88king?= Date: Wed, 16 Jan 2013 23:29:32 -0600 Subject: [PATCH] Adjusting ls output for voluminous output The previous table output was geared mostly for `ls _pry_`, which isn't a common of hierarchy. After feedback from users such as @envygeeks, we found a few tweaks that would help the really-small layers such as those found in Rails or in small classes, namely: - Rolling it up onto one line, if possible - Highlighting the heading in the colors familiar to users of GNU ls for "directory" style Additionally, I took the opportunity for toning down the private/protected method colors, because before they were green and yellow, now they're both "muted terminal blue" Without the ability to really get in and really distinguish colors (e.g. using 256 colors), giving "protected" such a loud color seems wrong. Before recoloring: https://github.com/pry/pry/issues/813#issuecomment-12355179 After: https://github.com/pry/pry/issues/813#issuecomment-12355941 --- lib/pry/commands/ls.rb | 4 ++-- lib/pry/helpers/table.rb | 23 ++++++++++++++++------- lib/pry/pry_class.rb | 6 +++--- lib/pry/terminal_info.rb | 5 +++++ spec/commands/ls_spec.rb | 8 ++++---- spec/helpers/table_spec.rb | 3 +++ 6 files changed, 33 insertions(+), 16 deletions(-) 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