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

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.)
This commit is contained in:
☈king 2012-10-15 08:20:18 +00:00 committed by rking@sharpsaw.org
parent e062a00837
commit 2195ada664
4 changed files with 40 additions and 12 deletions

View file

@ -15,6 +15,10 @@ class Pry
# The default print # The default print
DEFAULT_PRINT = proc do |output, value| DEFAULT_PRINT = proc do |output, value|
format_for_output(output, value, :hashrocket => true)
end
def self.format_for_output(output, value, options = {})
stringified = begin stringified = begin
value.pretty_inspect value.pretty_inspect
rescue RescuableException rescue RescuableException
@ -22,19 +26,23 @@ class Pry
end end
unless String === stringified 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 klass = (class << value; self; end).ancestors.first
stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>" stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>"
end end
nonce = rand(0x100000000).to_s(16) # whatever 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 # avoid colour-leak from CodeRay and any of the users' previous output
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color 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 end
# may be convenient when working with enormous objects and # may be convenient when working with enormous objects and

View file

@ -268,16 +268,27 @@ class Pry
name_value_pairs.sort_by do |name, value| name_value_pairs.sort_by do |name, value|
value.to_s.size value.to_s.size
end.reverse.map do |name, value| end.reverse.map do |name, value|
accumulator = StringIO.new colorized_assignment_style(name, format_value_without_hashrocket(value))
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
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. # 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

@ -63,7 +63,10 @@ describe "ls" do
describe 'with -l' do describe 'with -l' do
it 'should find locals and sort by descending size' 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 end
it 'should not list pry noise' do it 'should not list pry noise' do
pry_eval('ls -l').should.not =~ /_(?:dir|file|ex|pry|out|in)_/ pry_eval('ls -l').should.not =~ /_(?:dir|file|ex|pry|out|in)_/

View file

@ -31,7 +31,13 @@ describe Pry do
describe "DEFAULT_PRINT" do describe "DEFAULT_PRINT" do
it "should output the right thing" 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 end
it "should not be phased by un-inspectable things" do it "should not be phased by un-inspectable things" do