From e062a00837bee6b66ab0b1dff8763356fabb3ef2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=88king?= Date: Tue, 2 Oct 2012 06:37:01 -0500 Subject: [PATCH] Use `ls -l` instead of new command. --- lib/pry/commands/local_variables.rb | 21 ------------ lib/pry/commands/ls.rb | 40 ++++++++++++++++++---- lib/pry/pry_class.rb | 4 +-- test/test_commands/test_local_variables.rb | 10 ------ test/test_commands/test_ls.rb | 9 +++++ 5 files changed, 44 insertions(+), 40 deletions(-) delete mode 100644 lib/pry/commands/local_variables.rb delete mode 100644 test/test_commands/test_local_variables.rb diff --git a/lib/pry/commands/local_variables.rb b/lib/pry/commands/local_variables.rb deleted file mode 100644 index b19ff5bb..00000000 --- a/lib/pry/commands/local_variables.rb +++ /dev/null @@ -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 diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 86f540cf..31461297 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -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 + 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,20 @@ 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| + 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. def output_section(heading, body) return if body.compact.empty? diff --git a/lib/pry/pry_class.rb b/lib/pry/pry_class.rb index 6abded2c..f4be5d36 100644 --- a/lib/pry/pry_class.rb +++ b/lib/pry/pry_class.rb @@ -309,8 +309,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 diff --git a/test/test_commands/test_local_variables.rb b/test/test_commands/test_local_variables.rb deleted file mode 100644 index 712ca52e..00000000 --- a/test/test_commands/test_local_variables.rb +++ /dev/null @@ -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 diff --git a/test/test_commands/test_ls.rb b/test/test_commands/test_ls.rb index 551b108b..a4cd4ef0 100644 --- a/test/test_commands/test_ls.rb +++ b/test/test_commands/test_ls.rb @@ -61,6 +61,15 @@ describe "ls" do 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 it "should still work" do pry_eval(