module 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 to add custom prompts.

@example

Pry::Prompt.add(
  :ipython,
  'IPython-like prompt', [':', '...:']
) do |_context, _nesting, _pry_, sep|
  sep == ':' ? "In [#{_pry_.input_ring.count}]: " : '   ...: '
end

# Produces:
# In [3]: def foo
#    ...:   puts 'foo'
#    ...: end
# => :foo
# In [4]:

@since v0.11.0 @api public

Constants

DEFAULT_NAME

@return [String]

MAP

@deprecated Use {Pry::Prompt.add} instead.

SAFE_CONTEXTS

@return [Array<Object>] the list of objects that are known to have a

1-line #inspect output suitable for prompt

Public Class Methods

[](prompt_name) click to toggle source

Retrieves a prompt.

@example

Prompt[:my_prompt][:value]

@param [Symbol] ::prompt_name The name of the prompt you want to access @return [Hash{Symbol=>Object}] @since v0.12.0

# File lib/pry/prompt.rb, line 47
def [](prompt_name)
  @prompts[prompt_name.to_s]
end
add(prompt_name, description = '', separators = %w[> *]) { |context, nesting, _pry_, sep| ... } click to toggle source

Adds a new prompt to the prompt hash.

@param [Symbol] ::prompt_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, sep] @yieldparam context [Object] the context where Pry is currently in @yieldparam nesting [Integer] whether the context is nested @yieldparam pry [Pry] the Pry instance @yieldparam separator [String] separator string @return [nil] @raise [ArgumentError] if the size of `separators` is not 2 @since v0.12.0

# File lib/pry/prompt.rb, line 73
def add(prompt_name, description = '', separators = %w[> *])
  unless separators.size == 2
    raise ArgumentError, "separators size must be 2, given #{separators.size}"
  end

  @prompts[prompt_name.to_s] = {
    description: description,
    value: separators.map do |sep|
      proc { |context, nesting, _pry_| yield(context, nesting, _pry_, sep) }
    end
  }

  nil
end
all() click to toggle source

@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 54
def all
  @prompts.dup
end

Private Class Methods

prompt_name(name) click to toggle source
# File lib/pry/prompt.rb, line 90
def prompt_name(name)
  return name unless name.is_a?(Pry::Config::Lazy)

  name.call
end