mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Reading the output of ls
is hard, tablify it
ls now shows items more like how the unix ls works
This commit is contained in:
parent
83d3ceeb5f
commit
cd5f0ae3be
5 changed files with 46 additions and 33 deletions
|
@ -248,3 +248,4 @@ require 'pry/pry_class'
|
|||
require 'pry/pry_instance'
|
||||
require 'pry/cli'
|
||||
require 'pry/pager'
|
||||
require 'pry/terminal_info'
|
||||
|
|
|
@ -288,7 +288,18 @@ 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?
|
||||
output.puts "#{text.bold(color(:heading, heading))}: #{body.compact.join(Pry.config.ls.separator)}"
|
||||
output.puts "#{text.bold(color(:heading, heading))}: #{tablify(body)}"
|
||||
end
|
||||
|
||||
def tablify things
|
||||
things = things.compact
|
||||
screen_width = TerminalInfo.screen_size[1]
|
||||
maximum_width = things.map{|t| t.size}.max + 1
|
||||
columns = screen_width.div(maximum_width)
|
||||
|
||||
"\n" + things.each_slice(columns).map do |slice|
|
||||
slice.map {|s| s.ljust(maximum_width)}.join("")
|
||||
end.join("\n")
|
||||
end
|
||||
|
||||
# Color output based on config.ls.*_color
|
||||
|
|
|
@ -389,7 +389,7 @@ class Pry
|
|||
full_line = prompt + code
|
||||
whitespace = ' ' * overhang
|
||||
|
||||
_, cols = screen_size
|
||||
_, cols = TerminalInfo.screen_size
|
||||
|
||||
cols = cols.to_i
|
||||
lines = cols != 0 ? (full_line.length / cols + 1) : 1
|
||||
|
@ -404,30 +404,5 @@ class Pry
|
|||
|
||||
"#{move_up}#{prompt}#{colorize_code(code)}#{whitespace}#{move_down}"
|
||||
end
|
||||
|
||||
# Return a pair of [rows, columns] which gives the size of the window.
|
||||
#
|
||||
# If the window size cannot be determined, return nil.
|
||||
def screen_size
|
||||
[
|
||||
# Some readlines also provides get_screen_size.
|
||||
# Readline comes before IO#winsize because jruby sometimes defaults winsize to [25, 80]
|
||||
Readline.respond_to?(:get_screen_size) && Readline.get_screen_size,
|
||||
|
||||
# io/console adds a winsize method to IO streams.
|
||||
# rescue nil for jruby 1.7.0 [jruby/jruby#354]
|
||||
$stdout.tty? && $stdout.respond_to?(:winsize) && ($stdout.winsize rescue nil),
|
||||
|
||||
# Otherwise try to use the environment (this may be out of date due
|
||||
# to window resizing, but it's better than nothing).
|
||||
[ENV["ROWS"], ENV["COLUMNS"]],
|
||||
|
||||
# If the user is running within ansicon, then use the screen size
|
||||
# that it reports (same caveats apply as with ROWS and COLUMNS)
|
||||
ENV['ANSICON'] =~ /\((.*)x(.*)\)/ && [$2, $1]
|
||||
].detect do |(_, cols)|
|
||||
cols.to_i > 0
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
26
lib/pry/terminal_info.rb
Normal file
26
lib/pry/terminal_info.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
class Pry::TerminalInfo
|
||||
# Return a pair of [rows, columns] which gives the size of the window.
|
||||
#
|
||||
# If the window size cannot be determined, return nil.
|
||||
def self.screen_size
|
||||
[
|
||||
# Some readlines also provides get_screen_size.
|
||||
# Readline comes before IO#winsize because jruby sometimes defaults winsize to [25, 80]
|
||||
Readline.respond_to?(:get_screen_size) && Readline.get_screen_size,
|
||||
|
||||
# io/console adds a winsize method to IO streams.
|
||||
# rescue nil for jruby 1.7.0 [jruby/jruby#354]
|
||||
$stdout.tty? && $stdout.respond_to?(:winsize) && ($stdout.winsize rescue nil),
|
||||
|
||||
# Otherwise try to use the environment (this may be out of date due
|
||||
# to window resizing, but it's better than nothing).
|
||||
[ENV["ROWS"], ENV["COLUMNS"]],
|
||||
|
||||
# If the user is running within ansicon, then use the screen size
|
||||
# that it reports (same caveats apply as with ROWS and COLUMNS)
|
||||
ENV['ANSICON'] =~ /\((.*)x(.*)\)/ && [$2, $1]
|
||||
].detect do |(_, cols)|
|
||||
cols.to_i > 0
|
||||
end
|
||||
end
|
||||
end
|
|
@ -37,21 +37,21 @@ 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: goo/
|
||||
output.should =~ /methods: \ngoo/
|
||||
end
|
||||
|
||||
it "should not show protected/private by default" do
|
||||
pry_eval("ls -M Class.new{ def goo; end; private :goo }").should.not =~ /methods: goo/
|
||||
pry_eval("ls Class.new{ def goo; end; protected :goo }.new").should.not =~ /methods: goo/
|
||||
pry_eval("ls -M Class.new{ def goo; end; private :goo }").should.not =~ /goo/
|
||||
pry_eval("ls Class.new{ def goo; end; protected :goo }.new").should.not =~ /goo/
|
||||
end
|
||||
|
||||
it "should show public methods with -p" do
|
||||
pry_eval("ls -p Class.new{ def goo; end }.new").should =~ /methods: goo/
|
||||
pry_eval("ls -p Class.new{ def goo; end }.new").should =~ /methods: \ngoo/
|
||||
end
|
||||
|
||||
it "should show protected/private methods with -p" do
|
||||
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/
|
||||
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/
|
||||
end
|
||||
|
||||
it "should work for objects with an overridden method method" do
|
||||
|
|
Loading…
Add table
Reference in a new issue