updated Pry.view_clip() to call #inspect on Numerics, Strings and Symbols, added tests for this. Also added new CLIPPED_PRINT printer (using Pry.view_clip), perhaps useful for enormous objects)

This commit is contained in:
John Mair 2011-09-08 15:36:24 +12:00
parent af11439d55
commit 375dfbe380
3 changed files with 53 additions and 31 deletions

View File

@ -45,6 +45,11 @@ class Pry
end
end
# useful when playing with truly enormous objects
CLIPPED_PRINT = proc do |output, value|
output.puts "=> #{Pry.view_clip(value)}"
end
# Will only show the first line of the backtrace
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception|
output.puts "#{exception.class}: #{exception.message}"
@ -53,22 +58,23 @@ class Pry
# The default prompt; includes the target and nesting level
DEFAULT_PROMPT = [
proc { |target_self, nest_level, _|
if nest_level == 0
"pry(#{Pry.view_clip(target_self)})> "
else
"pry(#{Pry.view_clip(target_self)}):#{nest_level}> "
end
},
proc { |target_self, nest_level, _|
if nest_level == 0
"pry(#{Pry.view_clip(target_self)})> "
else
"pry(#{Pry.view_clip(target_self)}):#{nest_level}> "
end
},
proc { |target_self, nest_level, _|
if nest_level == 0
"pry(#{Pry.view_clip(target_self)})* "
else
"pry(#{Pry.view_clip(target_self)}):#{nest_level}* "
end
}
proc { |target_self, nest_level, _|
if nest_level == 0
"pry(#{Pry.view_clip(target_self)})* "
else
"pry(#{Pry.view_clip(target_self)}):#{nest_level}* "
end
}
]
# Deal with the ^D key being pressed, different behaviour in
# different cases:
# 1) In an expression - behave like `!` command (clear input buffer)
@ -92,22 +98,22 @@ class Pry
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
SHELL_PROMPT = [
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
]
# A prompt that includes the full object path as well as
# input/output (_in_ and _out_) information. Good for navigation.
NAV_PROMPT = [
proc do |_, level, pry|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{pry.input_array.size}] (pry) #{tree}: #{level}> "
end,
proc do |_, level, pry|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{pry.input_array.size}] (pry) #{tree}: #{level}* "
end,
]
proc do |_, level, pry|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{pry.input_array.size}] (pry) #{tree}: #{level}> "
end,
proc do |_, level, pry|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{pry.input_array.size}] (pry) #{tree}: #{level}* "
end,
]
# As a REPL, we often want to catch any unexpected exceptions that may have
@ -116,15 +122,15 @@ class Pry
module RescuableException
def self.===(exception)
case exception
# Catch when the user hits ^C (Interrupt < SignalException), and assume
# that they just wanted to stop the in-progress command (just like bash etc.)
# Catch when the user hits ^C (Interrupt < SignalException), and assume
# that they just wanted to stop the in-progress command (just like bash etc.)
when Interrupt
true
# Don't catch signals (particularly not SIGTERM) as these are unlikely to be
# intended for pry itself. We should also make sure that Kernel#exit works.
# Don't catch signals (particularly not SIGTERM) as these are unlikely to be
# intended for pry itself. We should also make sure that Kernel#exit works.
when SystemExit, SignalException
false
# All other exceptions will be caught.
# All other exceptions will be caught.
else
true
end

View File

@ -106,11 +106,13 @@ class Pry
elsif TOPLEVEL_BINDING.eval('self') == obj
# special case for 'main' object :)
obj.inspect
elsif [String, Numeric, Symbol].any? { |v| v === obj } && obj.inspect.length <= max_length
obj.inspect
else
"#<#{obj.class}:%#x>" % (obj.object_id << 1)
end
rescue
rescue RescuableException
"unknown"
end

View File

@ -1142,6 +1142,20 @@ describe Pry do
end
end
describe "given the a Numeric, String or Symbol object" do
[1, 2.0, -5, "hello", :test].each do |o|
it "returns the #inspect of the special-cased immediate object: #{o}" do
Pry.view_clip(o, VC_MAX_LENGTH).should == o.inspect
end
end
# only testing with String here :)
it "returns #<> format of the special-cased immediate object if #inspect is longer than maximum" do
o = "o" * (VC_MAX_LENGTH + 1)
Pry.view_clip(o, VC_MAX_LENGTH).should =~ /String:0x.*?/
end
end
describe "given an object with an #inspect string as long as the maximum specified" do
it "returns the #<> format of the object (never use inspect)" do
o = Object.new