mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Merge remote-tracking branch 'rking/feature/ls-l'
This commit is contained in:
commit
c0a33a0829
5 changed files with 76 additions and 13 deletions
14
lib/pry.rb
14
lib/pry.rb
|
@ -15,6 +15,10 @@ class Pry
|
|||
|
||||
# The default print
|
||||
DEFAULT_PRINT = proc do |output, value|
|
||||
output_with_default_format(output, value, :hashrocket => true)
|
||||
end
|
||||
|
||||
def self.output_with_default_format(output, value, options = {})
|
||||
stringified = begin
|
||||
value.pretty_inspect
|
||||
rescue RescuableException
|
||||
|
@ -22,19 +26,23 @@ class Pry
|
|||
end
|
||||
|
||||
unless String === stringified
|
||||
# Read the class name off of the singleton class to provide a default inspect.
|
||||
# Read the class name off of the singleton class to provide a default
|
||||
# inspect.
|
||||
klass = (class << value; self; end).ancestors.first
|
||||
stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>"
|
||||
end
|
||||
|
||||
nonce = rand(0x100000000).to_s(16) # whatever
|
||||
|
||||
colorized = Helpers::BaseHelpers.colorize_code(stringified.gsub(/#</, "%<#{nonce}"))
|
||||
stringified.gsub!(/#</, "%<#{nonce}")
|
||||
colorized = Helpers::BaseHelpers.colorize_code(stringified)
|
||||
|
||||
# avoid colour-leak from CodeRay and any of the users' previous output
|
||||
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color
|
||||
|
||||
Helpers::BaseHelpers.stagger_output("=> #{colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')}", output)
|
||||
prefix = if false != options[:hashrocket] then '=> ' else '' end
|
||||
result = prefix + colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')
|
||||
Helpers::BaseHelpers.stagger_output(result, output)
|
||||
end
|
||||
|
||||
# may be convenient when working with enormous objects and
|
||||
|
|
|
@ -24,7 +24,7 @@ class Pry
|
|||
opt.on :v, "verbose", "Show methods and constants on all super-classes (ignores Pry.config.ls.ceiling)"
|
||||
|
||||
opt.on :g, "globals", "Show global variables, including those builtin to Ruby (in cyan)"
|
||||
opt.on :l, "locals", "Show locals, including those provided by Pry (in red)"
|
||||
opt.on :l, "locals", "Show hash of local vars, sorted by descending size"
|
||||
|
||||
opt.on :c, "constants", "Show constants, highlighting classes (in blue), and exceptions (in purple).\n" +
|
||||
" " * 32 + "Constants that are pending autoload? are also shown (in yellow)."
|
||||
|
@ -45,11 +45,12 @@ class Pry
|
|||
opts.present?(:globals) || opts.present?(:locals) || opts.present?(:constants) ||
|
||||
opts.present?(:ivars))
|
||||
|
||||
show_methods = opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) || !has_opts
|
||||
show_methods = opts.present?(:methods) || opts.present?(:'instance-methods') || opts.present?(:ppp) || !has_opts
|
||||
show_self_methods = (!has_opts && Module === obj)
|
||||
show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
|
||||
show_ivars = opts.present?(:ivars) || !has_opts
|
||||
show_locals = opts.present?(:locals) || (!has_opts && args.empty?)
|
||||
show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
|
||||
show_ivars = opts.present?(:ivars) || !has_opts
|
||||
show_locals = opts.present?(:locals)
|
||||
show_local_names = !has_opts && args.empty?
|
||||
|
||||
grep_regex, grep = [Regexp.new(opts[:G] || "."), lambda{ |x| x.grep(grep_regex) }]
|
||||
|
||||
|
@ -95,8 +96,19 @@ class Pry
|
|||
output_section("class variables", format_variables(:class_var, kvars))
|
||||
end
|
||||
|
||||
if show_local_names
|
||||
output_section("locals", format_local_names(
|
||||
grep[target.eval("local_variables")]))
|
||||
end
|
||||
|
||||
if show_locals
|
||||
output_section("locals", format_locals(grep[target.eval("local_variables")]))
|
||||
loc_names = target.eval('local_variables').reject do |e|
|
||||
_pry_.sticky_locals.keys.include? e.to_sym
|
||||
end
|
||||
name_value_pairs = loc_names.map do |name|
|
||||
[name, (target.eval name.to_s)]
|
||||
end
|
||||
output.puts format_locals(name_value_pairs)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -242,7 +254,7 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
def format_locals(locals)
|
||||
def format_local_names(locals)
|
||||
locals.sort_by(&:downcase).map do |name|
|
||||
if _pry_.sticky_locals.include?(name.to_sym)
|
||||
color(:pry_var, name)
|
||||
|
@ -252,6 +264,31 @@ class Pry
|
|||
end
|
||||
end
|
||||
|
||||
def format_locals(name_value_pairs)
|
||||
name_value_pairs.sort_by do |name, value|
|
||||
value.to_s.size
|
||||
end.reverse.map do |name, value|
|
||||
colorized_assignment_style(name, format_value_without_hashrocket(value))
|
||||
end
|
||||
end
|
||||
|
||||
def colorized_assignment_style(lhs, rhs, desired_width = 7)
|
||||
colorized_lhs = color(:local_var, lhs)
|
||||
color_escape_padding = colorized_lhs.size - lhs.size
|
||||
pad = desired_width + color_escape_padding
|
||||
"%-#{pad}s = %s" % [color(:local_var, colorized_lhs), rhs]
|
||||
end
|
||||
|
||||
def format_value_without_hashrocket(value)
|
||||
accumulator = StringIO.new
|
||||
if Pry::DEFAULT_PRINT.source_location == Pry.print.source_location
|
||||
Pry.output_with_default_format(accumulator, value, :hashrocket => false)
|
||||
else
|
||||
Pry.print.call(accumulator, value)
|
||||
end
|
||||
accumulator.string
|
||||
end
|
||||
|
||||
# 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?
|
||||
|
|
|
@ -312,8 +312,8 @@ class Pry
|
|||
:protected_method_color => :yellow,
|
||||
:method_missing_color => :bright_red,
|
||||
|
||||
:local_var_color => :default,
|
||||
:pry_var_color => :red, # e.g. _, _pry_, _file_
|
||||
:local_var_color => :yellow,
|
||||
:pry_var_color => :default, # e.g. _, _pry_, _file_
|
||||
|
||||
:instance_var_color => :blue, # e.g. @foo
|
||||
:class_var_color => :bright_blue, # e.g. @@foo
|
||||
|
|
|
@ -61,6 +61,18 @@ describe "ls" do
|
|||
end
|
||||
end
|
||||
|
||||
describe 'with -l' do
|
||||
it 'should find locals and sort by descending size' do
|
||||
result = pry_eval("aa = 'asdf'; bb = 'xyz'", 'ls -l')
|
||||
result.should.not =~ /=>/
|
||||
result.should.not =~ /0x\d{5}/
|
||||
result.should =~ /asdf.*xyz/m
|
||||
end
|
||||
it 'should not list pry noise' do
|
||||
pry_eval('ls -l').should.not =~ /_(?:dir|file|ex|pry|out|in)_/
|
||||
end
|
||||
end
|
||||
|
||||
describe "when inside Modules" do
|
||||
it "should still work" do
|
||||
pry_eval(
|
||||
|
|
|
@ -31,7 +31,13 @@ describe Pry do
|
|||
|
||||
describe "DEFAULT_PRINT" do
|
||||
it "should output the right thing" do
|
||||
mock_pry("{:a => 1}").should =~ /\{:a=>1\}/
|
||||
mock_pry("{:a => 1}").should =~ /^=> \{:a=>1\}/
|
||||
end
|
||||
|
||||
it 'should have a milder-mannered companion without the hashrocket' do
|
||||
s = StringIO.new
|
||||
Pry.output_with_default_format s, '2', :hashrocket => false
|
||||
s.string.should.not =~ /^=>/
|
||||
end
|
||||
|
||||
it "should not be phased by un-inspectable things" do
|
||||
|
|
Loading…
Reference in a new issue