From 2195ada66491950d91866fb629e96359719e0fbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=88king?= Date: Mon, 15 Oct 2012 08:20:18 +0000 Subject: [PATCH] Eliminate spurious hashrockets. These were coming from a pretty simple source: the DEFAULT_PRINT prepends them so you can get output like: [1] pry(main)> 2+3 => 5 But since we're formatting these differently, obviously we don't want that prefix. So this patch extracts a Pry.format_for_output method then calls that iff the user hasn't changed the default. There is some hackitude involved, but the test pass (and are of decent coverage, I think, so feel fry to try to diff this down if you have a good idea.) --- lib/pry.rb | 14 +++++++++++--- lib/pry/commands/ls.rb | 25 ++++++++++++++++++------- test/test_commands/test_ls.rb | 5 ++++- test/test_pry_output.rb | 8 +++++++- 4 files changed, 40 insertions(+), 12 deletions(-) diff --git a/lib/pry.rb b/lib/pry.rb index 986d4a07..ab4efd84 100644 --- a/lib/pry.rb +++ b/lib/pry.rb @@ -15,6 +15,10 @@ class Pry # The default print DEFAULT_PRINT = proc do |output, value| + format_for_output(output, value, :hashrocket => true) + end + + def self.format_for_output(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(/# #{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 diff --git a/lib/pry/commands/ls.rb b/lib/pry/commands/ls.rb index 31461297..8508902c 100644 --- a/lib/pry/commands/ls.rb +++ b/lib/pry/commands/ls.rb @@ -268,16 +268,27 @@ class Pry 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] + 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.format_for_output(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? diff --git a/test/test_commands/test_ls.rb b/test/test_commands/test_ls.rb index a4cd4ef0..84e7ed55 100644 --- a/test/test_commands/test_ls.rb +++ b/test/test_commands/test_ls.rb @@ -63,7 +63,10 @@ describe "ls" do 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 + result = pry_eval("aa = 'asdf'; bb = 'xyz'", 'ls -l') + result.should !~ /=>/ + result.should !~ /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)_/ diff --git a/test/test_pry_output.rb b/test/test_pry_output.rb index a13a9d93..120c7de4 100644 --- a/test/test_pry_output.rb +++ b/test/test_pry_output.rb @@ -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.format_for_output s, '2', :hashrocket => false + s.string.should !~ /^=>/ end it "should not be phased by un-inspectable things" do