change Pry.view_clip() to optionally drop or include hex value.

change 2nd argument of Pry.view_clip() to accept a Hash of options
with two keys: max_length (default 60) and id (default false)

the custom prompt proc's in lib/pry.rb clip the object shown to
look better visually. this change still supports that behavior but
also adds the option to include a hex representation of the object's
ID by including id: true.

@rf- suggested supporting both (with & without id) to be able to
keep printer proc's pretty but also support other code that's okay
with printing inspects that are hex-y, like the Pry::CLIPPED_PRINT.

also changes Pry::CLIPPED_PRINT to print return values with a hex
ID included since it is a bit different from prompt proc's.
This commit is contained in:
Robert Gleeson 2014-03-16 06:58:28 +01:00
parent 68b83e7b6c
commit 43b80755e6
4 changed files with 49 additions and 32 deletions

View File

@ -30,6 +30,9 @@
* add `Pry::LastException` (#1145)
#### Bug fixes, etc.
* update `Pry::CLIPPED_PRINT` to include a hex representation of object ID when printing a return value. (#1162)
* change second argument of `Pry.view_clip()` from Fixnum to Hash to support returning a string with or
without a hex representation of object ID. (#1162)
* break up `pry/completion.rb` into `pry/bond_completer.rb` and `pry/input_completer.rb`
* `Pry#last_exception=` supports exception objects who have been frozen (#1145)
* `binding.pry` inside `.pryrc` file now works, with some limitations (@richo / #1118)

View File

@ -38,7 +38,7 @@ class Pry
# 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, id: true)
end
# Will only show the first line of the backtrace

View File

@ -158,25 +158,37 @@ class Pry
REPLFileLoader.new(file_name).load
end
#
# An inspector that clips the output to `max_length` chars.
# In case of > `max_length` chars the `#<Object...> notation is used.
# @param obj The object to view.
# @param max_length The maximum number of chars before clipping occurs.
# @return [String] The string representation of `obj`.
def self.view_clip(obj, max_length = 60)
if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length
#
# @param options [Hash]
# :max_length The maximum number of chars before clipping occurs.
# default is 60.
#
# :id boolean to indicate whether or not a hex reprsentation of the object ID
# is attached to the output when the length of inspect is greater than value
# of `:max_length`. default is false.
#
# @return [String]
# The string representation of `obj`.
#
def self.view_clip(obj, options = {})
max = options.fetch :max_length, 60
id = options.fetch :id, false
if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
obj.name.to_s
elsif Pry.main == obj
# special-case to support jruby.
# fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
# we can drop in the future.
obj.to_s
elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length
elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max
obj.inspect
else
"#<#{obj.class}:0x%x>" % (obj.object_id << 1)
id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>"
end
rescue RescuableException
"unknown"
end

View File

@ -97,7 +97,7 @@ describe "test Pry defaults" do
Pry.print = new_print
Pry.new.print.should == Pry.print
Object.new.pry :input => InputTester.new("\"test\""), :output => @str_output
Object.new.pry :input => InputTester.new("\"test\""), :output => @str_output
@str_output.string.should == "=> LOL\n"
@str_output = StringIO.new
@ -244,41 +244,43 @@ describe "test Pry defaults" do
end
describe "view_clip used for displaying an object in a truncated format" do
VC_MAX_LENGTH = 60
DEFAULT_OPTIONS = {
max_length: 60
}
MAX_LENGTH = DEFAULT_OPTIONS[:max_length]
describe "given an object with an #inspect string" do
it "returns the #<> format of the object (never use inspect)" do
o = Object.new
def o.inspect; "a" * VC_MAX_LENGTH; end
def o.inspect; "a" * MAX_LENGTH; end
Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
Pry.view_clip(o, DEFAULT_OPTIONS).should =~ /#<Object/
end
end
describe "given the 'main' object" do
it "returns the #to_s of main (special case)" do
o = TOPLEVEL_BINDING.eval('self')
Pry.view_clip(o, VC_MAX_LENGTH).should == o.to_s
Pry.view_clip(o, DEFAULT_OPTIONS).should == o.to_s
end
end
describe "the list of prompt safe objects" 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
Pry.view_clip(o, DEFAULT_OPTIONS).should == o.inspect
end
end
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/
o = "o" * (MAX_LENGTH + 1)
Pry.view_clip(o, DEFAULT_OPTIONS).should =~ /#<String/
end
it "returns the #inspect of the custom prompt safe objects" do
Barbie = Class.new { def inspect; "life is plastic, it's fantastic" end }
Pry.config.prompt_safe_objects << Barbie
output = Pry.view_clip(Barbie.new, VC_MAX_LENGTH)
output = Pry.view_clip(Barbie.new, DEFAULT_OPTIONS)
output.should == "life is plastic, it's fantastic"
end
end
@ -286,9 +288,9 @@ describe "test Pry defaults" do
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
def o.inspect; "a" * VC_MAX_LENGTH; end
def o.inspect; "a" * DEFAULT_OPTIONS; end
Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
Pry.view_clip(o, DEFAULT_OPTIONS).should =~ /#<Object/
end
end
@ -297,9 +299,9 @@ describe "test Pry defaults" do
describe "when the object is a regular one" do
it "returns a string of the #<class name:object idish> format" do
o = Object.new
def o.inspect; "a" * (VC_MAX_LENGTH + 1); end
def o.inspect; "a" * (DEFAULT_OPTIONS + 1); end
Pry.view_clip(o, VC_MAX_LENGTH).should =~ /#<Object/
Pry.view_clip(o, DEFAULT_OPTIONS).should =~ /#<Object/
end
end
@ -308,8 +310,8 @@ describe "test Pry defaults" do
it "returns a string of the #<class name:object idish> format" do
c, m = Class.new, Module.new
Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
Pry.view_clip(c, DEFAULT_OPTIONS).should =~ /#<Class/
Pry.view_clip(m, DEFAULT_OPTIONS).should =~ /#<Module/
end
end
@ -318,11 +320,11 @@ describe "test Pry defaults" do
c, m = Class.new, Module.new
def c.name; "a" * (VC_MAX_LENGTH + 1); end
def m.name; "a" * (VC_MAX_LENGTH + 1); end
def c.name; "a" * (MAX_LENGTH + 1); end
def m.name; "a" * (MAX_LENGTH + 1); end
Pry.view_clip(c, VC_MAX_LENGTH).should =~ /#<Class/
Pry.view_clip(m, VC_MAX_LENGTH).should =~ /#<Module/
Pry.view_clip(c, DEFAULT_OPTIONS).should =~ /#<Class/
Pry.view_clip(m, DEFAULT_OPTIONS).should =~ /#<Module/
end
end
@ -330,11 +332,11 @@ describe "test Pry defaults" do
it "returns a string of the #<class name:object idish> format" do
c, m = Class.new, Module.new
def c.name; "a" * VC_MAX_LENGTH; end
def m.name; "a" * VC_MAX_LENGTH; end
def c.name; "a" * MAX_LENGTH; end
def m.name; "a" * MAX_LENGTH; end
Pry.view_clip(c, VC_MAX_LENGTH).should == c.name
Pry.view_clip(m, VC_MAX_LENGTH).should == m.name
Pry.view_clip(c, DEFAULT_OPTIONS).should == c.name
Pry.view_clip(m, DEFAULT_OPTIONS).should == m.name
end
end