mirror of
https://github.com/pry/pry.git
synced 2022-11-09 12:35:05 -05:00
Allow customisation of the prompt objects
Fix issue #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>
This commit is contained in:
parent
7a6a6164fe
commit
6ca5f60207
4 changed files with 26 additions and 3 deletions
|
@ -90,6 +90,8 @@ class Pry
|
|||
}
|
||||
]
|
||||
|
||||
DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false]
|
||||
|
||||
# A simple prompt - doesn't display target or nesting level
|
||||
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
|
||||
|
||||
|
|
|
@ -119,6 +119,20 @@ class Pry
|
|||
# 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
|
||||
|
|
|
@ -184,7 +184,7 @@ class Pry
|
|||
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)
|
||||
|
@ -280,6 +280,7 @@ Readline version #{ver} detected - will not auto_resize! correctly.
|
|||
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
|
||||
|
|
|
@ -263,18 +263,24 @@ describe "test Pry defaults" do
|
|||
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
|
||||
|
|
Loading…
Reference in a new issue