class Pry::Prompt
Prompt
represents the Pry
prompt, which can be used with Readline-like libraries. It defines a few default prompts (default prompt, simple prompt, etc) and also provides an API for adding and implementing custom prompts.
@example Registering a new Pry
prompt
Pry::Prompt.add( :ipython, 'IPython-like prompt', [':', '...:'] ) do |_context, _nesting, pry_instance, sep| sep == ':' ? "In [#{pry_instance.input_ring.count}]: " : ' ...: ' end # Produces: # In [3]: def foo # ...: puts 'foo' # ...: end # => :foo # In [4]:
@example Manually instantiating the Prompt
class
prompt_procs = [ proc { '#{rand(1)}>" }, proc { "#{('a'..'z').to_a.sample}*" } ] prompt = Pry::Prompt.new( :random, 'Random number or letter prompt.', prompt_procs ) prompt.wait_proc.call(...) #=> prompt.incomplete_proc.call(...)
@since v0.11.0 @api public
Attributes
@return [String]
@return [String]
@return [Array<Proc>] the array of procs that hold
`[wait_proc, incomplete_proc]`
Public Class Methods
Retrieves a prompt.
@example
Prompt[:my_prompt]
@param [Symbol] name The name of the prompt you want to access @return [Hash{Symbol=>Object}] @since v0.12.0
# File lib/pry/prompt.rb, line 52 def [](name) @prompts[name.to_s] end
Adds a new prompt to the prompt hash.
@param [Symbol] name @param [String] description @param [Array<String>] separators The separators to differentiate
between prompt modes (default mode and class/method definition mode). The Array *must* have a size of 2.
@yield [context, nesting, pry_instance, sep] @yieldparam context [Object] the context where Pry
is currently in @yieldparam nesting [Integer] whether the context is nested @yieldparam pry_instance [Pry] the Pry
instance @yieldparam separator [String] separator string @return [nil] @raise [ArgumentError] if the size of `separators` is not 2 @raise [ArgumentError] if `prompt_name` is already occupied @since v0.12.0
# File lib/pry/prompt.rb, line 79 def add(name, description = '', separators = %w[> *]) name = name.to_s unless separators.size == 2 raise ArgumentError, "separators size must be 2, given #{separators.size}" end if @prompts.key?(name) raise ArgumentError, "the '#{name}' prompt was already added" end @prompts[name] = new( name, description, separators.map do |sep| proc do |context, nesting, pry_instance| yield(context, nesting, pry_instance, sep) end end ) nil end
@return [Hash{Symbol=>Hash}] the duplicate of the internal prompts hash @note Use this for read-only operations @since v0.12.0
# File lib/pry/prompt.rb, line 59 def all @prompts.dup end
@param [String] name @param [String] description @param [Array<Proc>] prompt_procs
# File lib/pry/prompt.rb, line 117 def initialize(name, description, prompt_procs) @name = name @description = description @prompt_procs = prompt_procs end
Public Instance Methods
@deprecated Use a `Pry::Prompt` instance directly
# File lib/pry/prompt.rb, line 135 def [](key) key = key.to_s if %w[name description].include?(key) Pry::Warning.warn( "`Pry::Prompt[:#{@name}][:#{key}]` is deprecated. " \ "Use `#{self.class}##{key}` instead" ) public_send(key) elsif key.to_s == 'value' Pry::Warning.warn( "`#{self.class}[:#{@name}][:value]` is deprecated. Use " \ "`#{self.class}#prompt_procs` instead or an instance of " \ "`#{self.class}` directly" ) @prompt_procs end end
@return [Proc] the proc which builds the prompt when in the middle of an
expression such as open method, etc. (`*`)
# File lib/pry/prompt.rb, line 130 def incomplete_proc @prompt_procs.last end
@return [Proc] the proc which builds the wait prompt (`>`)
# File lib/pry/prompt.rb, line 124 def wait_proc @prompt_procs.first end