Skip to content

Commit

Permalink
Allow customisation of the prompt objects
Browse files Browse the repository at this point in the history
Fix issue pry#885 (Add an API for extending Pry.view_clip)

    [1] pry(main)> class Barbie
                 |   def inspect
                 |     'You can brush my hair, undress me everywhere!'
                 |   end
                 | end
    => nil
    [2] pry(main)>
    [3] pry(main)> Pry.config.prompt_safe_objects << Barbie
    => [String, Numeric, Symbol, nil, true, false, Barbie]
    [4] pry(main)> cd Barbie.new
    [5] pry(You can brush my hair, undress me everywhere!):1>
  • Loading branch information
kyrylo committed May 4, 2013
1 parent 7a6a616 commit 6ca5f60
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/pry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ def self.output_with_default_format(output, value, options = {})
}
]

DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false]

# A simple prompt - doesn't display target or nesting level
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]

Expand Down
14 changes: 14 additions & 0 deletions lib/pry/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,20 @@ def hooks=(v)
# Pry.config.prompt_name = 'my_rails_project'
attr_accessor :prompt_name

# The list of safe objects, the `#inspect` method of which can be used for
# the prompt. The default safe objects are defined in
# `DEFAULT_PROMPT_SAFE_OBJECTS` (see Pry::DEFAULT_PROMPT_SAFE_OBJECTS).
# @return [Array]
# @example
# class Barbie
# def inspect
# 'You can brush my hair, undress me everywhere!'
# end
# end
#
# Pry.config.prompt_safe_objects << Barbie
attr_accessor :prompt_safe_objects

# The default editor to use. Defaults to $VISUAL, $EDITOR, or a sensible
# fallback for the platform. If `editor` is a String then that string is
# used as the shell command to invoke the editor. If `editor` is callable
Expand Down
3 changes: 2 additions & 1 deletion lib/pry/pry_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ def self.view_clip(obj, max_length = 60)
elsif TOPLEVEL_BINDING.eval('self') == obj
# special case for 'main' object :)
obj.to_s
elsif [String, Numeric, Symbol, nil, true, false].any? { |v| v === obj } && obj.inspect.length <= max_length
elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max_length
obj.inspect
else
"#<#{obj.class}>"#:%x>"# % (obj.object_id << 1)
Expand Down Expand Up @@ -280,6 +280,7 @@ def self.set_config_defaults
config.commands = Pry::Commands
config.prompt_name = DEFAULT_PROMPT_NAME
config.prompt = DEFAULT_PROMPT
config.prompt_safe_objects = DEFAULT_PROMPT_SAFE_OBJECTS
config.print = DEFAULT_PRINT
config.exception_handler = DEFAULT_EXCEPTION_HANDLER
config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST
Expand Down
10 changes: 8 additions & 2 deletions spec/pry_defaults_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -263,18 +263,24 @@ def o.inspect; "a" * VC_MAX_LENGTH; end
end
end

describe "given the a Numeric, String or Symbol object" do
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
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/
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.should == "life is plastic, it's fantastic"
end
end

describe "given an object with an #inspect string as long as the maximum specified" do
Expand Down

0 comments on commit 6ca5f60

Please sign in to comment.