Module: Pry::Prompt

Defined in:
lib/pry/prompt.rb

Overview

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.

Examples:

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

Constant Summary collapse

DEFAULT_NAME =

Returns:

  • (String)

Since:

  • v0.11.0

'pry'.freeze
SAFE_CONTEXTS =

Returns the list of objects that are known to have a 1-line #inspect output suitable for prompt.

Returns:

  • (Array<Object>)

    the list of objects that are known to have a 1-line #inspect output suitable for prompt

Since:

  • v0.11.0

[String, Numeric, Symbol, nil, true, false].freeze
MAP =
Deprecated.

Use add instead.

Since:

  • v0.11.0

{}

Class Method Summary collapse

Class Method Details

.[](prompt_name) ⇒ Hash{Symbol=>Object}

Retrieves a prompt.

Examples:

Prompt[:my_prompt][:value]

Parameters:

  • prompt_name (Symbol)

    The name of the prompt you want to access

Returns:

Since:

  • v0.12.0



47
48
49
# 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| ... } ⇒ nil

Adds a new prompt to the prompt hash.

Parameters:

  • prompt_name (Symbol)
  • description (String) (defaults to: '')
  • separators (Array<String>) (defaults to: %w[> *])

    The separators to differentiate between prompt modes (default mode and class/method definition mode). The Array must have a size of 2.

Yields:

  • (context, nesting, _pry_, sep)

Yield Parameters:

  • context (Object)

    the context where Pry is currently in

  • nesting (Integer)

    whether the context is nested

  • _pry_ (Pry)

    the Pry instance

  • separator (String)

    separator string

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if the size of ‘separators` is not 2

Since:

  • v0.12.0



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# 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

.allHash{Symbol=>Hash}

Note:

Use this for read-only operations

Returns the duplicate of the internal prompts hash.

Returns:

  • (Hash{Symbol=>Hash})

    the duplicate of the internal prompts hash

Since:

  • v0.12.0



54
55
56
# File 'lib/pry/prompt.rb', line 54

def all
  @prompts.dup
end