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
This commit is contained in:
☈king 2013-01-16 23:29:32 -06:00 committed by rking@sharpsaw.org
parent 5b346bc447
commit 14283aa51f
6 changed files with 33 additions and 16 deletions

View File

@ -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

View File

@ -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)

View File

@ -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,

View File

@ -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.

View File

@ -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

View File

@ -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