Move hashrocket out of Pry.config.print

This simplifies the contract of Pry.config.print which makes it easier to use
in other places that we want to output a value, like ls -l.

If desired you can stil remove the => by setting Pry.config.output_prefix = ''
in your ~/.pryrc
This commit is contained in:
Conrad Irwin 2012-11-07 23:58:18 -08:00
parent c0a33a0829
commit 5246700e2b
6 changed files with 17 additions and 20 deletions

View File

@ -40,8 +40,7 @@ class Pry
# avoid colour-leak from CodeRay and any of the users' previous output
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color
prefix = if false != options[:hashrocket] then '=> ' else '' end
result = prefix + colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')
result = colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')
Helpers::BaseHelpers.stagger_output(result, output)
end
@ -49,15 +48,15 @@ class Pry
# pretty_print is too slow
SIMPLE_PRINT = proc do |output, value|
begin
output.puts "=> #{value.inspect}"
output.puts value.inspect
rescue RescuableException
output.puts "=> unknown"
output.puts "unknown"
end
end
# useful when playing with truly enormous objects
CLIPPED_PRINT = proc do |output, value|
output.puts "=> #{Pry.view_clip(value)}"
output.puts Pry.view_clip(value)
end
# Will only show the first line of the backtrace

View File

@ -268,7 +268,7 @@ class Pry
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))
colorized_assignment_style(name, format_value(value))
end
end
@ -279,13 +279,9 @@ class Pry
"%-#{pad}s = %s" % [color(:local_var, colorized_lhs), rhs]
end
def format_value_without_hashrocket(value)
def format_value(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
Pry.print.call(accumulator, value)
accumulator.string
end

View File

@ -272,6 +272,7 @@ class Pry
config.auto_indent = Helpers::BaseHelpers.use_ansi_codes?
config.correct_indent = true
config.collision_warning = false
config.output_prefix = "=> "
if defined?(Bond) && Readline::VERSION !~ /editline/i
config.completer = Pry::BondCompleter

View File

@ -334,6 +334,7 @@ class Pry
if last_result_is_exception?
exception_handler.call(output, result, self)
elsif should_print?
output.write(Pry.config.output_prefix)
print.call(output, result)
else
# nothin'

View File

@ -92,22 +92,22 @@ describe "test Pry defaults" do
end
it "should set the print default, and the default should be overridable" do
new_print = proc { |out, value| out.puts value }
new_print = proc { |out, value| out.puts "LOL" }
Pry.print = new_print
Pry.new.print.should == Pry.print
Pry.new(:input => InputTester.new("\"test\""), :output => @str_output).rep
@str_output.string.should == "test\n"
@str_output.string.should == "=> LOL\n"
@str_output = StringIO.new
Pry.new(:input => InputTester.new("\"test\""), :output => @str_output,
:print => proc { |out, value| out.puts value.reverse }).rep
@str_output.string.should == "tset\n"
@str_output.string.should == "=> tset\n"
Pry.new.print.should == Pry.print
@str_output = StringIO.new
Pry.new(:input => InputTester.new("\"test\""), :output => @str_output).rep
@str_output.string.should == "test\n"
@str_output.string.should == "=> LOL\n"
end
describe "pry return values" do

View File

@ -34,10 +34,10 @@ describe Pry do
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 =~ /^=>/
it "should not include the =>" do
accumulator = StringIO.new
Pry.config.print.call(accumulator, :a => 1)
accumulator.string.should == "{:a=>1}\n"
end
it "should not be phased by un-inspectable things" do