Class: Pry::Prompt

Inherits:
Object show all
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 for adding and implementing custom prompts.

Examples:

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]:

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, prompt_procs) ⇒ Prompt

Returns a new instance of Prompt.

Parameters:

  • name (String)
  • description (String)
  • prompt_procs (Array<Proc>)

Since:

  • v0.11.0



117
118
119
120
121
# File 'lib/pry/prompt.rb', line 117

def initialize(name, description, prompt_procs)
  @name = name
  @description = description
  @prompt_procs = prompt_procs
end

Instance Attribute Details

#descriptionString (readonly)

Returns:

  • (String)

Since:

  • v0.11.0



108
109
110
# File 'lib/pry/prompt.rb', line 108

def description
  @description
end

#nameString (readonly)

Returns:

  • (String)

Since:

  • v0.11.0



105
106
107
# File 'lib/pry/prompt.rb', line 105

def name
  @name
end

#prompt_procsArray<Proc> (readonly)

Returns the array of procs that hold ‘[wait_proc, incomplete_proc]`.

Returns:

  • (Array<Proc>)

    the array of procs that hold ‘[wait_proc, incomplete_proc]`

Since:

  • v0.11.0



112
113
114
# File 'lib/pry/prompt.rb', line 112

def prompt_procs
  @prompt_procs
end

Class Method Details

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

Retrieves a prompt.

Examples:

Prompt[:my_prompt]

Parameters:

  • name (Symbol)

    The name of the prompt you want to access

Returns:

Since:

  • v0.12.0



52
53
54
# File 'lib/pry/prompt.rb', line 52

def [](name)
  @prompts[name.to_s]
end

.add(name, description = '', separators = %w[> *])) {|context, nesting, pry_instance, sep| ... } ⇒ nil

Adds a new prompt to the prompt hash.

Parameters:

  • 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_instance, sep)

Yield Parameters:

  • context (Object)

    the context where Pry is currently in

  • nesting (Integer)

    whether the context is nested

  • pry_instance (Pry)

    the Pry instance

  • separator (String)

    separator string

Returns:

  • (nil)

Raises:

  • (ArgumentError)

    if the size of ‘separators` is not 2

  • (ArgumentError)

    if ‘prompt_name` is already occupied

Since:

  • v0.12.0



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# 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

.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



59
60
61
# File 'lib/pry/prompt.rb', line 59

def all
  @prompts.dup
end

Instance Method Details

#[](key) ⇒ Object

Deprecated.

Use a ‘Pry::Prompt` instance directly

Since:

  • v0.11.0



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# 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

#incomplete_procProc

Returns the proc which builds the prompt when in the middle of an expression such as open method, etc. (‘*`).

Returns:

  • (Proc)

    the proc which builds the prompt when in the middle of an expression such as open method, etc. (‘*`)

Since:

  • v0.11.0



130
131
132
# File 'lib/pry/prompt.rb', line 130

def incomplete_proc
  @prompt_procs.last
end

#wait_procProc

Returns the proc which builds the wait prompt (‘>`).

Returns:

  • (Proc)

    the proc which builds the wait prompt (‘>`)

Since:

  • v0.11.0



124
125
126
# File 'lib/pry/prompt.rb', line 124

def wait_proc
  @prompt_procs.first
end