1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

Use ls -l instead of new command.

This commit is contained in:
☈king 2012-10-02 06:37:01 -05:00 committed by rking@sharpsaw.org
parent 38598429fb
commit e062a00837
5 changed files with 44 additions and 40 deletions

View file

@ -1,21 +0,0 @@
class Pry
Pry::Commands.create_command 'local-variables' do
group 'Context'
description 'Show hash of local vars, sorted by descending size'
def process
pry_vars = [
:____, :___, :__, :_, :_dir_, :_file_, :_ex_, :_pry_, :_out_, :_in_ ]
loc_names = target.eval('local_variables').reject do |e|
pry_vars.include? e
end
name_value_pairs = loc_names.map do |name|
[name, (target.eval name.to_s)]
end
name_value_pairs.sort! do |(a,av), (b,bv)|
bv.to_s.size <=> av.to_s.size
end
Pry.print.call _pry_.output, Hash[name_value_pairs]
end
end
end

View file

@ -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 :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 :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" + 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)." " " * 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?(:globals) || opts.present?(:locals) || opts.present?(:constants) ||
opts.present?(:ivars)) 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_self_methods = (!has_opts && Module === obj)
show_constants = opts.present?(:constants) || (!has_opts && Module === obj) show_constants = opts.present?(:constants) || (!has_opts && Module === obj)
show_ivars = opts.present?(:ivars) || !has_opts show_ivars = opts.present?(:ivars) || !has_opts
show_locals = opts.present?(:locals) || (!has_opts && args.empty?) 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) }] 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)) output_section("class variables", format_variables(:class_var, kvars))
end end
if show_local_names
output_section("locals", format_local_names(
grep[target.eval("local_variables")]))
end
if show_locals 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
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
end end
@ -242,7 +254,7 @@ class Pry
end end
end end
def format_locals(locals) def format_local_names(locals)
locals.sort_by(&:downcase).map do |name| locals.sort_by(&:downcase).map do |name|
if _pry_.sticky_locals.include?(name.to_sym) if _pry_.sticky_locals.include?(name.to_sym)
color(:pry_var, name) color(:pry_var, name)
@ -252,6 +264,20 @@ class Pry
end end
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|
accumulator = StringIO.new
Pry.print.call accumulator, value
colorized_name= color(:local_var, name)
desired_width = 7
color_escape_padding = colorized_name.size - name.size
pad = desired_width + color_escape_padding
"%-#{pad}s = %s" % [color(:local_var, name), accumulator.string]
end
end
# Add a new section to the output. Outputs nothing if the section would be empty. # Add a new section to the output. Outputs nothing if the section would be empty.
def output_section(heading, body) def output_section(heading, body)
return if body.compact.empty? return if body.compact.empty?

View file

@ -309,8 +309,8 @@ class Pry
:protected_method_color => :yellow, :protected_method_color => :yellow,
:method_missing_color => :bright_red, :method_missing_color => :bright_red,
:local_var_color => :default, :local_var_color => :yellow,
:pry_var_color => :red, # e.g. _, _pry_, _file_ :pry_var_color => :default, # e.g. _, _pry_, _file_
:instance_var_color => :blue, # e.g. @foo :instance_var_color => :blue, # e.g. @foo
:class_var_color => :bright_blue, # e.g. @@foo :class_var_color => :bright_blue, # e.g. @@foo

View file

@ -1,10 +0,0 @@
require 'helper'
describe 'local-variables' do
it 'should find locals and sort by descending size' do
mock_pry("a = 'asdf'; b = 'x'\nlocal-variables").should =~ /'asdf'.*'x'/
end
it 'should not list pry noise' do
mock_pry('local-variables').should.not =~ /_(?:dir|file|ex|pry|out|in)_/
end
end

View file

@ -61,6 +61,15 @@ describe "ls" do
end end
end end
describe 'with -l' do
it 'should find locals and sort by descending size' do
pry_eval("a = 'asdf'; b = 'xyz'", 'ls -l').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 describe "when inside Modules" do
it "should still work" do it "should still work" do
pry_eval( pry_eval(