2011-10-08 07:57:42 -04:00
|
|
|
require "pry/indent"
|
2011-04-07 02:09:03 -04:00
|
|
|
|
2012-06-19 11:08:59 -04:00
|
|
|
##
|
|
|
|
# Pry is a powerful alternative to the standard IRB shell for Ruby. It
|
|
|
|
# features syntax highlighting, a flexible plugin architecture, runtime
|
|
|
|
# invocation and source and documentation browsing.
|
|
|
|
#
|
|
|
|
# Pry can be started similar to other command line utilities by simply running
|
|
|
|
# the following command:
|
|
|
|
#
|
|
|
|
# pry
|
|
|
|
#
|
|
|
|
# Once inside Pry you can invoke the help message:
|
|
|
|
#
|
|
|
|
# help
|
|
|
|
#
|
|
|
|
# This will show a list of available commands and their usage. For more
|
|
|
|
# information about Pry you can refer to the following resources:
|
|
|
|
#
|
|
|
|
# * http://pry.github.com/
|
|
|
|
# * https://github.com/pry/pry
|
|
|
|
# * the IRC channel, which is #pry on the Freenode network
|
|
|
|
#
|
2010-12-25 08:59:37 -05:00
|
|
|
class Pry
|
|
|
|
|
2011-05-19 11:53:44 -04:00
|
|
|
attr_accessor :input
|
|
|
|
attr_accessor :output
|
|
|
|
attr_accessor :commands
|
|
|
|
attr_accessor :print
|
|
|
|
attr_accessor :exception_handler
|
2011-09-15 05:08:05 -04:00
|
|
|
attr_accessor :input_stack
|
2012-03-31 17:18:03 -04:00
|
|
|
attr_accessor :quiet
|
|
|
|
alias :quiet? :quiet
|
2011-09-15 05:08:05 -04:00
|
|
|
|
2011-05-19 11:53:44 -04:00
|
|
|
attr_accessor :custom_completions
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-08-21 02:22:45 -04:00
|
|
|
attr_accessor :binding_stack
|
2011-03-16 03:02:15 -04:00
|
|
|
|
2011-08-31 13:05:21 -04:00
|
|
|
attr_accessor :last_result
|
|
|
|
attr_accessor :last_file
|
|
|
|
attr_accessor :last_dir
|
|
|
|
|
2012-01-08 01:01:15 -05:00
|
|
|
attr_reader :last_exception
|
|
|
|
|
2011-09-02 12:09:17 -04:00
|
|
|
attr_reader :input_array
|
|
|
|
attr_reader :output_array
|
|
|
|
|
2011-11-27 01:20:41 -05:00
|
|
|
attr_accessor :backtrace
|
|
|
|
|
2012-03-13 22:33:51 -04:00
|
|
|
attr_accessor :extra_sticky_locals
|
|
|
|
|
2012-06-15 08:36:03 -04:00
|
|
|
# This is exposed via Pry::Command#state.
|
|
|
|
attr_reader :command_state
|
|
|
|
|
2012-01-23 07:10:51 -05:00
|
|
|
# Special treatment for hooks as we want to alert people of the
|
|
|
|
# changed API
|
|
|
|
attr_reader :hooks
|
|
|
|
|
|
|
|
# FIXME:
|
|
|
|
# This is a hack to alert people of the new API.
|
|
|
|
# @param [Pry::Hooks] v Only accept `Pry::Hooks` now!
|
|
|
|
def hooks=(v)
|
|
|
|
if v.is_a?(Hash)
|
2012-01-24 03:44:21 -05:00
|
|
|
warn "Hash-based hooks are now deprecated! Use a `Pry::Hooks` object instead! http://rubydoc.info/github/pry/pry/master/Pry/Hooks"
|
|
|
|
@hooks = Pry::Hooks.from_hash(v)
|
|
|
|
else
|
|
|
|
@hooks = v
|
2012-01-23 07:10:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-29 04:14:12 -05:00
|
|
|
# Create a new `Pry` object.
|
|
|
|
# @param [Hash] options The optional configuration parameters.
|
2011-04-16 17:17:30 -04:00
|
|
|
# @option options [#readline] :input The object to use for input.
|
|
|
|
# @option options [#puts] :output The object to use for output.
|
2011-05-31 11:12:29 -04:00
|
|
|
# @option options [Pry::CommandBase] :commands The object to use for commands.
|
|
|
|
# @option options [Hash] :hooks The defined hook Procs
|
|
|
|
# @option options [Array<Proc>] :prompt The array of Procs to use for the prompts.
|
2010-12-30 10:01:11 -05:00
|
|
|
# @option options [Proc] :print The Proc to use for the 'print'
|
2012-03-31 17:18:03 -04:00
|
|
|
# @option options [Boolean] :quiet If true, omit the whereami banner when starting.
|
2010-12-30 10:01:11 -05:00
|
|
|
# component of the REPL. (see print.rb)
|
2010-12-25 08:59:37 -05:00
|
|
|
def initialize(options={})
|
2011-05-31 11:12:29 -04:00
|
|
|
refresh(options)
|
2011-06-05 12:07:13 -04:00
|
|
|
|
2012-06-15 19:10:56 -04:00
|
|
|
@binding_stack = []
|
|
|
|
@indent = Pry::Indent.new
|
|
|
|
@command_state = {}
|
2011-05-31 11:12:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Refresh the Pry instance settings from the Pry class.
|
|
|
|
# Allows options to be specified to override settings from Pry class.
|
|
|
|
# @param [Hash] options The options to override Pry class settings
|
|
|
|
# for this instance.
|
|
|
|
def refresh(options={})
|
2011-05-19 11:53:44 -04:00
|
|
|
defaults = {}
|
2011-05-24 04:16:46 -04:00
|
|
|
attributes = [
|
2012-03-31 17:18:03 -04:00
|
|
|
:input, :output, :commands, :print, :quiet,
|
2011-05-19 11:53:44 -04:00
|
|
|
:exception_handler, :hooks, :custom_completions,
|
2012-03-13 22:33:51 -04:00
|
|
|
:prompt, :memory_size, :input_stack, :extra_sticky_locals
|
2011-05-19 11:53:44 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
attributes.each do |attribute|
|
|
|
|
defaults[attribute] = Pry.send attribute
|
2010-12-27 05:56:55 -05:00
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
|
2011-05-31 11:12:29 -04:00
|
|
|
defaults.merge!(options).each do |key, value|
|
2012-01-02 14:23:22 -05:00
|
|
|
send("#{key}=", value) if respond_to?("#{key}=")
|
2010-12-27 05:56:55 -05:00
|
|
|
end
|
2011-04-07 02:09:03 -04:00
|
|
|
|
2011-05-31 11:12:29 -04:00
|
|
|
true
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2012-02-28 07:36:50 -05:00
|
|
|
# The currently active `Binding`.
|
|
|
|
# @return [Binding] The currently active `Binding` for the session.
|
|
|
|
def current_context
|
|
|
|
binding_stack.last
|
|
|
|
end
|
|
|
|
|
2011-05-24 04:16:46 -04:00
|
|
|
# The current prompt.
|
2011-05-19 11:53:44 -04:00
|
|
|
# This is the prompt at the top of the prompt stack.
|
2011-05-24 04:16:46 -04:00
|
|
|
#
|
2011-05-19 11:53:44 -04:00
|
|
|
# @example
|
|
|
|
# self.prompt = Pry::SIMPLE_PROMPT
|
|
|
|
# self.prompt # => Pry::SIMPLE_PROMPT
|
|
|
|
#
|
|
|
|
# @return [Array<Proc>] Current prompt.
|
|
|
|
def prompt
|
|
|
|
prompt_stack.last
|
|
|
|
end
|
|
|
|
|
|
|
|
def prompt=(new_prompt)
|
|
|
|
if prompt_stack.empty?
|
|
|
|
push_prompt new_prompt
|
|
|
|
else
|
|
|
|
prompt_stack[-1] = new_prompt
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-08-30 13:35:41 -04:00
|
|
|
# Injects a local variable into the provided binding.
|
|
|
|
# @param [String] name The name of the local to inject.
|
|
|
|
# @param [Object] value The value to set the local to.
|
|
|
|
# @param [Binding] b The binding to set the local on.
|
|
|
|
# @return [Object] The value the local was set to.
|
|
|
|
def inject_local(name, value, b)
|
2012-03-03 06:51:11 -05:00
|
|
|
Thread.current[:__pry_local__] = value.is_a?(Proc) ? value.call : value
|
2011-08-30 13:35:41 -04:00
|
|
|
b.eval("#{name} = Thread.current[:__pry_local__]")
|
|
|
|
ensure
|
|
|
|
Thread.current[:__pry_local__] = nil
|
|
|
|
end
|
|
|
|
|
2011-06-07 08:57:01 -04:00
|
|
|
# @return [Integer] The maximum amount of objects remembered by the inp and
|
|
|
|
# out arrays. Defaults to 100.
|
2011-06-06 11:14:16 -04:00
|
|
|
def memory_size
|
|
|
|
@output_array.max_size
|
|
|
|
end
|
|
|
|
|
|
|
|
def memory_size=(size)
|
|
|
|
@input_array = Pry::HistoryArray.new(size)
|
|
|
|
@output_array = Pry::HistoryArray.new(size)
|
|
|
|
end
|
|
|
|
|
2012-03-03 06:51:11 -05:00
|
|
|
# Inject all the sticky locals into the `target` binding.
|
|
|
|
# @param [Binding] target
|
|
|
|
def inject_sticky_locals(target)
|
|
|
|
sticky_locals.each_pair do |name, value|
|
2011-10-01 20:41:53 -04:00
|
|
|
inject_local(name, value, target)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-03 06:51:11 -05:00
|
|
|
# Add a sticky local to this Pry instance.
|
|
|
|
# A sticky local is a local that persists between all bindings in a session.
|
|
|
|
# @param [Symbol] name The name of the sticky local.
|
|
|
|
# @yield The block that defines the content of the local. The local
|
|
|
|
# will be refreshed at each tick of the repl loop.
|
|
|
|
def add_sticky_local(name, &block)
|
|
|
|
sticky_locals[name] = block
|
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Hash] The currently defined sticky locals.
|
|
|
|
def sticky_locals
|
|
|
|
@sticky_locals ||= {
|
|
|
|
:_in_ => proc { @input_array },
|
|
|
|
:_out_ => proc { @output_array },
|
2012-01-11 22:53:34 -05:00
|
|
|
:_pry_ => self,
|
2012-03-03 06:51:11 -05:00
|
|
|
:_ex_ => proc { last_exception },
|
|
|
|
:_file_ => proc { last_file },
|
|
|
|
:_dir_ => proc { last_dir },
|
|
|
|
:_ => proc { last_result }
|
2012-03-13 22:33:51 -04:00
|
|
|
}.merge(extra_sticky_locals)
|
2011-08-31 13:05:21 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Initialize the repl session.
|
|
|
|
# @param [Binding] target The target binding for the session.
|
|
|
|
def repl_prologue(target)
|
2012-01-14 03:33:36 -05:00
|
|
|
exec_hook :before_session, output, target, self
|
2012-03-03 06:51:11 -05:00
|
|
|
set_last_result(nil, target)
|
|
|
|
|
2011-05-15 06:03:16 -04:00
|
|
|
|
2011-09-15 05:08:05 -04:00
|
|
|
@input_array << nil # add empty input so _in_ and _out_ match
|
2011-06-05 12:07:13 -04:00
|
|
|
|
2011-08-21 02:22:45 -04:00
|
|
|
binding_stack.push target
|
2011-03-16 03:02:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Clean-up after the repl session.
|
|
|
|
# @param [Binding] target The target binding for the session.
|
2011-09-17 18:34:58 -04:00
|
|
|
def repl_epilogue(target)
|
2012-01-14 03:33:36 -05:00
|
|
|
exec_hook :after_session, output, target, self
|
2011-03-16 03:02:15 -04:00
|
|
|
|
2011-08-21 02:22:45 -04:00
|
|
|
binding_stack.pop
|
2012-03-18 18:09:04 -04:00
|
|
|
Pry.save_history if Pry.config.history.should_save
|
2011-03-16 03:02:15 -04:00
|
|
|
end
|
2011-04-06 12:59:09 -04:00
|
|
|
|
2010-12-29 04:14:12 -05:00
|
|
|
# Start a read-eval-print-loop.
|
2010-12-27 22:56:23 -05:00
|
|
|
# If no parameter is given, default to top-level (main).
|
2010-12-29 04:14:12 -05:00
|
|
|
# @param [Object, Binding] target The receiver of the Pry session
|
2011-02-25 20:00:55 -05:00
|
|
|
# @return [Object] The target of the Pry session or an explictly given
|
|
|
|
# return value. If given return value is `nil` or no return value
|
|
|
|
# is specified then `target` will be returned.
|
2010-12-27 22:56:23 -05:00
|
|
|
# @example
|
|
|
|
# Pry.new.repl(Object.new)
|
2010-12-25 08:59:37 -05:00
|
|
|
def repl(target=TOPLEVEL_BINDING)
|
2011-02-16 11:27:55 -05:00
|
|
|
target = Pry.binding_for(target)
|
2010-12-25 08:59:37 -05:00
|
|
|
|
2011-03-16 03:02:15 -04:00
|
|
|
repl_prologue(target)
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2012-03-08 00:54:19 -05:00
|
|
|
break_data = nil
|
|
|
|
exception = catch(:raise_up) do
|
|
|
|
break_data = catch(:breakout) do
|
|
|
|
loop do
|
|
|
|
rep(binding_stack.last)
|
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
2012-03-08 00:54:19 -05:00
|
|
|
exception = false
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2012-03-08 00:54:19 -05:00
|
|
|
raise exception if exception
|
|
|
|
|
|
|
|
break_data
|
2012-01-13 08:22:30 -05:00
|
|
|
ensure
|
|
|
|
repl_epilogue(target)
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
2011-01-13 09:35:46 -05:00
|
|
|
|
2010-12-29 04:14:12 -05:00
|
|
|
# Perform a read-eval-print.
|
2010-12-27 22:56:23 -05:00
|
|
|
# If no parameter is given, default to top-level (main).
|
2010-12-29 04:14:12 -05:00
|
|
|
# @param [Object, Binding] target The receiver of the read-eval-print
|
2010-12-27 22:56:23 -05:00
|
|
|
# @example
|
|
|
|
# Pry.new.rep(Object.new)
|
2010-12-25 08:59:37 -05:00
|
|
|
def rep(target=TOPLEVEL_BINDING)
|
2011-02-16 11:27:55 -05:00
|
|
|
target = Pry.binding_for(target)
|
2011-04-07 10:13:16 -04:00
|
|
|
result = re(target)
|
2011-04-07 21:06:39 -04:00
|
|
|
|
2011-04-18 00:47:35 -04:00
|
|
|
show_result(result) if should_print?
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2010-12-27 22:56:23 -05:00
|
|
|
# Perform a read-eval
|
|
|
|
# If no parameter is given, default to top-level (main).
|
2010-12-29 04:14:12 -05:00
|
|
|
# @param [Object, Binding] target The receiver of the read-eval-print
|
2011-04-16 17:17:30 -04:00
|
|
|
# @return [Object] The result of the eval or an `Exception` object in case of
|
2011-04-16 17:27:48 -04:00
|
|
|
# error. In the latter case, you can check whether the exception was raised
|
|
|
|
# or is just the result of the expression using #last_result_is_exception?
|
2010-12-27 22:56:23 -05:00
|
|
|
# @example
|
|
|
|
# Pry.new.re(Object.new)
|
2010-12-25 08:59:37 -05:00
|
|
|
def re(target=TOPLEVEL_BINDING)
|
2011-02-16 11:27:55 -05:00
|
|
|
target = Pry.binding_for(target)
|
2011-01-07 07:18:09 -05:00
|
|
|
|
2011-08-31 13:05:21 -04:00
|
|
|
# It's not actually redundant to inject them continually as we may have
|
|
|
|
# moved into the scope of a new Binding (e.g the user typed `cd`)
|
2012-03-03 06:51:11 -05:00
|
|
|
inject_sticky_locals(target)
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2011-05-15 06:08:59 -04:00
|
|
|
code = r(target)
|
|
|
|
|
2012-01-13 02:43:06 -05:00
|
|
|
result = target.eval(code, Pry.eval_path, Pry.current_line)
|
|
|
|
set_last_result(result, target, code)
|
|
|
|
|
2011-08-31 13:05:21 -04:00
|
|
|
result
|
2011-08-26 20:44:13 -04:00
|
|
|
rescue RescuableException => e
|
2012-01-08 01:01:15 -05:00
|
|
|
self.last_exception = e
|
|
|
|
e
|
2011-05-24 08:34:55 -04:00
|
|
|
ensure
|
2011-08-07 04:19:28 -04:00
|
|
|
update_input_history(code)
|
2012-01-14 03:33:36 -05:00
|
|
|
exec_hook :after_eval, result, self
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2010-12-27 22:56:23 -05:00
|
|
|
# Perform a read.
|
|
|
|
# If no parameter is given, default to top-level (main).
|
|
|
|
# This is a multi-line read; so the read continues until a valid
|
|
|
|
# Ruby expression is received.
|
|
|
|
# Pry commands are also accepted here and operate on the target.
|
2010-12-29 04:14:12 -05:00
|
|
|
# @param [Object, Binding] target The receiver of the read.
|
2011-05-16 13:08:40 -04:00
|
|
|
# @param [String] eval_string Optionally Prime `eval_string` with a start value.
|
2010-12-27 22:56:23 -05:00
|
|
|
# @return [String] The Ruby expression.
|
|
|
|
# @example
|
|
|
|
# Pry.new.r(Object.new)
|
2011-05-11 03:32:29 -04:00
|
|
|
def r(target=TOPLEVEL_BINDING, eval_string="")
|
2011-02-16 11:27:55 -05:00
|
|
|
target = Pry.binding_for(target)
|
2011-04-07 10:13:16 -04:00
|
|
|
@suppress_output = false
|
2011-01-09 06:51:45 -05:00
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
loop do
|
2011-11-05 02:10:10 -04:00
|
|
|
begin
|
2011-11-19 21:16:23 -05:00
|
|
|
# eval_string will probably be mutated by this method
|
|
|
|
retrieve_line(eval_string, target)
|
2012-06-03 04:55:07 -04:00
|
|
|
rescue CommandError, Slop::InvalidOptionError, MethodSource::SourceNotFoundError => e
|
2011-11-05 02:10:10 -04:00
|
|
|
output.puts "Error: #{e.message}"
|
|
|
|
end
|
2011-05-24 08:34:55 -04:00
|
|
|
|
2012-01-13 02:21:21 -05:00
|
|
|
begin
|
2012-04-10 09:39:29 -04:00
|
|
|
break if Pry::Code.complete_expression?(eval_string)
|
2012-01-13 02:21:21 -05:00
|
|
|
rescue SyntaxError => e
|
|
|
|
output.puts "SyntaxError: #{e.message.sub(/.*syntax error, */m, '')}"
|
|
|
|
eval_string = ""
|
|
|
|
end
|
2011-03-31 09:14:04 -04:00
|
|
|
end
|
2011-04-07 10:13:16 -04:00
|
|
|
|
2011-09-05 00:12:56 -04:00
|
|
|
@suppress_output = true if eval_string =~ /;\Z/ || eval_string.empty?
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2012-01-14 03:33:36 -05:00
|
|
|
exec_hook :after_read, eval_string, self
|
2011-04-07 10:13:16 -04:00
|
|
|
eval_string
|
2011-04-14 23:58:29 -04:00
|
|
|
end
|
|
|
|
|
2011-05-29 11:46:25 -04:00
|
|
|
# Output the result or pass to an exception handler (if result is an exception).
|
2011-04-16 00:47:48 -04:00
|
|
|
def show_result(result)
|
2011-04-16 17:17:30 -04:00
|
|
|
if last_result_is_exception?
|
2011-09-10 12:39:51 -04:00
|
|
|
exception_handler.call output, result, self
|
2011-04-16 17:17:30 -04:00
|
|
|
else
|
|
|
|
print.call output, result
|
|
|
|
end
|
2011-08-27 04:58:53 -04:00
|
|
|
rescue RescuableException => e
|
2011-08-23 03:54:58 -04:00
|
|
|
# Being uber-paranoid here, given that this exception arose because we couldn't
|
|
|
|
# serialize something in the user's program, let's not assume we can serialize
|
|
|
|
# the exception either.
|
|
|
|
begin
|
2011-09-14 18:48:20 -04:00
|
|
|
output.puts "(pry) output error: #{e.inspect}"
|
2011-08-27 04:58:53 -04:00
|
|
|
rescue RescuableException => e
|
2011-08-23 03:54:58 -04:00
|
|
|
if last_result_is_exception?
|
2011-09-14 18:48:20 -04:00
|
|
|
output.puts "(pry) output error: failed to show exception"
|
2011-08-23 03:54:58 -04:00
|
|
|
else
|
2011-09-14 18:48:20 -04:00
|
|
|
output.puts "(pry) output error: failed to show result"
|
2011-08-23 03:54:58 -04:00
|
|
|
end
|
|
|
|
end
|
2011-04-14 23:58:29 -04:00
|
|
|
end
|
2011-04-07 10:13:16 -04:00
|
|
|
|
2011-11-22 09:28:45 -05:00
|
|
|
def should_force_encoding?(eval_string, val)
|
|
|
|
eval_string.empty? && val.respond_to?(:encoding) && val.encoding != eval_string.encoding
|
|
|
|
end
|
|
|
|
private :should_force_encoding?
|
|
|
|
|
2011-11-19 21:16:23 -05:00
|
|
|
# Read and process a line of input -- check for ^D, determine which prompt to
|
|
|
|
# use, rewrite the indentation if `Pry.config.auto_indent` is enabled, and,
|
|
|
|
# if the line is a command, process it and alter the eval_string accordingly.
|
|
|
|
# This method should not need to be invoked directly.
|
2011-10-05 13:04:44 -04:00
|
|
|
#
|
2011-03-31 09:14:04 -04:00
|
|
|
# @param [String] eval_string The cumulative lines of input.
|
|
|
|
# @param [Binding] target The target of the session.
|
|
|
|
# @return [String] The line received.
|
|
|
|
def retrieve_line(eval_string, target)
|
2011-10-08 13:49:01 -04:00
|
|
|
@indent.reset if eval_string.empty?
|
|
|
|
|
2012-04-29 01:23:55 -04:00
|
|
|
current_prompt = select_prompt(eval_string, target)
|
2012-05-08 02:39:50 -04:00
|
|
|
completion_proc = Pry::InputCompleter.build_completion_proc(target,
|
|
|
|
instance_eval(&custom_completions))
|
2012-04-14 03:29:33 -04:00
|
|
|
|
2011-10-08 13:49:01 -04:00
|
|
|
|
2012-05-08 02:39:50 -04:00
|
|
|
indentation = Pry.config.auto_indent ? @indent.current_prefix : ''
|
2012-04-29 01:23:55 -04:00
|
|
|
|
2012-04-01 15:13:34 -04:00
|
|
|
begin
|
2012-05-08 02:39:50 -04:00
|
|
|
val = readline("#{current_prompt}#{indentation}", completion_proc)
|
2012-04-01 15:13:34 -04:00
|
|
|
|
2012-05-08 02:39:50 -04:00
|
|
|
# Handle <Ctrl+C> like Bash, empty the current input buffer but do not quit.
|
|
|
|
# This is only for ruby-1.9; other versions of ruby do not let you send Interrupt
|
|
|
|
# from within Readline.
|
2012-05-28 19:37:02 -04:00
|
|
|
rescue Interrupt
|
2012-04-01 15:13:34 -04:00
|
|
|
output.puts ""
|
2012-06-16 15:59:14 -04:00
|
|
|
eval_string.replace("")
|
2012-04-01 15:13:34 -04:00
|
|
|
return
|
|
|
|
end
|
2011-03-31 09:14:04 -04:00
|
|
|
|
2011-10-27 07:10:12 -04:00
|
|
|
# invoke handler if we receive EOF character (^D)
|
2011-03-31 09:14:04 -04:00
|
|
|
if !val
|
2011-08-29 10:21:51 -04:00
|
|
|
output.puts ""
|
|
|
|
Pry.config.control_d_handler.call(eval_string, self)
|
2011-11-19 21:16:23 -05:00
|
|
|
return
|
|
|
|
end
|
2011-10-01 16:10:59 -04:00
|
|
|
|
2011-11-19 21:16:23 -05:00
|
|
|
# Change the eval_string into the input encoding (Issue 284)
|
|
|
|
# TODO: This wouldn't be necessary if the eval_string was constructed from
|
|
|
|
# input strings only.
|
2011-11-22 09:28:45 -05:00
|
|
|
if should_force_encoding?(eval_string, val)
|
2011-11-19 21:16:23 -05:00
|
|
|
eval_string.force_encoding(val.encoding)
|
|
|
|
end
|
2011-10-09 15:03:24 -04:00
|
|
|
|
2011-11-19 21:16:23 -05:00
|
|
|
if Pry.config.auto_indent && !input.is_a?(StringIO)
|
|
|
|
original_val = "#{indentation}#{val}"
|
|
|
|
indented_val = @indent.indent(val)
|
|
|
|
|
2012-04-02 03:21:35 -04:00
|
|
|
if output.tty? && Pry::Helpers::BaseHelpers.use_ansi_codes? && Pry.config.correct_indent
|
2012-04-02 03:31:22 -04:00
|
|
|
output.print @indent.correct_indentation(current_prompt, indented_val, original_val.length - indented_val.length)
|
2012-01-15 15:25:19 -05:00
|
|
|
output.flush
|
2011-10-05 13:04:44 -04:00
|
|
|
end
|
2011-11-19 21:16:23 -05:00
|
|
|
else
|
|
|
|
indented_val = val
|
|
|
|
end
|
2011-10-05 13:04:44 -04:00
|
|
|
|
2011-11-27 02:17:04 -05:00
|
|
|
begin
|
|
|
|
if !process_command(val, eval_string, target)
|
|
|
|
eval_string << "#{indented_val.rstrip}\n" unless val.empty?
|
|
|
|
end
|
|
|
|
ensure
|
|
|
|
Pry.history << indented_val unless input.is_a?(StringIO)
|
2011-03-31 09:14:04 -04:00
|
|
|
end
|
|
|
|
end
|
2011-01-13 09:35:46 -05:00
|
|
|
|
2011-11-19 21:16:23 -05:00
|
|
|
# If the given line is a valid command, process it in the context of the
|
|
|
|
# current `eval_string` and context.
|
2011-03-31 09:14:04 -04:00
|
|
|
# This method should not need to be invoked directly.
|
|
|
|
# @param [String] val The line to process.
|
|
|
|
# @param [String] eval_string The cumulative lines of input.
|
2011-06-16 09:45:33 -04:00
|
|
|
# @param [Binding] target The target of the Pry session.
|
2011-11-19 21:16:23 -05:00
|
|
|
# @return [Boolean] `true` if `val` is a command, `false` otherwise
|
|
|
|
def process_command(val, eval_string, target)
|
2012-01-02 19:04:47 -05:00
|
|
|
result = commands.process_line(val, {
|
|
|
|
:target => target,
|
|
|
|
:output => output,
|
|
|
|
:eval_string => eval_string,
|
|
|
|
:pry_instance => self
|
|
|
|
})
|
2011-09-05 12:12:53 -04:00
|
|
|
|
|
|
|
# set a temporary (just so we can inject the value we want into eval_string)
|
2011-09-04 09:50:27 -04:00
|
|
|
Thread.current[:__pry_cmd_result__] = result
|
|
|
|
|
|
|
|
# note that `result` wraps the result of command processing; if a
|
|
|
|
# command was matched and invoked then `result.command?` returns true,
|
|
|
|
# otherwise it returns false.
|
2011-11-19 21:16:23 -05:00
|
|
|
if result.command?
|
|
|
|
if !result.void_command?
|
|
|
|
# the command that was invoked was non-void (had a return value) and so we make
|
|
|
|
# the value of the current expression equal to the return value
|
|
|
|
# of the command.
|
|
|
|
eval_string.replace "Thread.current[:__pry_cmd_result__].retval\n"
|
|
|
|
end
|
|
|
|
true
|
2011-03-31 09:14:04 -04:00
|
|
|
else
|
2011-11-19 21:16:23 -05:00
|
|
|
false
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
end
|
2011-01-13 09:35:46 -05:00
|
|
|
|
2011-09-10 12:41:51 -04:00
|
|
|
# Run the specified command.
|
2011-10-27 07:10:12 -04:00
|
|
|
# @param [String] val The command (and its params) to execute.
|
|
|
|
# @param [String] eval_string The current input buffer.
|
|
|
|
# @param [Binding] target The binding to use..
|
2011-12-31 06:50:04 -05:00
|
|
|
# @return [Pry::Command::VOID_VALUE]
|
2011-09-10 12:41:51 -04:00
|
|
|
# @example
|
|
|
|
# pry_instance.run_command("ls -m")
|
2011-10-27 07:10:12 -04:00
|
|
|
def run_command(val, eval_string = "", target = binding_stack.last)
|
2012-01-02 19:04:47 -05:00
|
|
|
commands.process_line(val,
|
|
|
|
:eval_string => eval_string,
|
|
|
|
:target => target,
|
|
|
|
:pry_instance => self,
|
|
|
|
:output => output
|
|
|
|
)
|
2011-12-31 06:50:04 -05:00
|
|
|
Pry::Command::VOID_VALUE
|
2011-09-10 12:41:51 -04:00
|
|
|
end
|
|
|
|
|
2012-01-14 03:33:36 -05:00
|
|
|
# Execute the specified hook.
|
|
|
|
# @param [Symbol] name The hook name to execute
|
|
|
|
# @param [*Object] args The arguments to pass to the hook
|
|
|
|
# @return [Object, Exception] The return value of the hook or the exception raised
|
|
|
|
#
|
|
|
|
# If executing a hook raises an exception, we log that and then continue sucessfully.
|
|
|
|
# To debug such errors, use the global variable $pry_hook_error, which is set as a
|
|
|
|
# result.
|
|
|
|
def exec_hook(name, *args, &block)
|
|
|
|
e_before = hooks.errors.size
|
2012-01-16 20:42:21 -05:00
|
|
|
hooks.exec_hook(name, *args, &block).tap do
|
|
|
|
hooks.errors[e_before..-1].each do |e|
|
|
|
|
output.puts "#{name} hook failed: #{e.class}: #{e.message}"
|
|
|
|
output.puts "#{e.backtrace.first}"
|
|
|
|
output.puts "(see _pry_.hooks.errors to debug)"
|
|
|
|
end
|
2012-01-14 03:33:36 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-14 06:55:22 -04:00
|
|
|
# Set the last result of an eval.
|
2011-03-31 09:14:04 -04:00
|
|
|
# This method should not need to be invoked directly.
|
2011-03-14 06:55:22 -04:00
|
|
|
# @param [Object] result The result.
|
|
|
|
# @param [Binding] target The binding to set `_` on.
|
2012-01-13 02:43:06 -05:00
|
|
|
# @param [String] code The code that was run.
|
|
|
|
def set_last_result(result, target, code="")
|
2011-08-07 04:19:28 -04:00
|
|
|
@last_result_is_exception = false
|
2011-05-15 06:03:16 -04:00
|
|
|
@output_array << result
|
2011-08-07 04:19:28 -04:00
|
|
|
|
2012-01-13 02:43:06 -05:00
|
|
|
self.last_result = result unless code =~ /\A\s*\z/
|
2011-03-14 06:55:22 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set the last exception for a session.
|
2012-01-08 01:01:15 -05:00
|
|
|
# @param [Exception] ex
|
|
|
|
def last_exception=(ex)
|
2011-06-16 09:45:33 -04:00
|
|
|
class << ex
|
2011-09-13 13:39:48 -04:00
|
|
|
attr_accessor :file, :line, :bt_index
|
2011-09-12 13:07:05 -04:00
|
|
|
def bt_source_location_for(index)
|
|
|
|
backtrace[index] =~ /(.*):(\d+)/
|
|
|
|
[$1, $2.to_i]
|
|
|
|
end
|
2012-01-08 01:01:15 -05:00
|
|
|
|
|
|
|
def inc_bt_index
|
|
|
|
@bt_index = (@bt_index + 1) % backtrace.size
|
|
|
|
end
|
2011-06-16 09:45:33 -04:00
|
|
|
end
|
|
|
|
|
2011-09-13 13:39:48 -04:00
|
|
|
ex.bt_index = 0
|
|
|
|
ex.file, ex.line = ex.bt_source_location_for(0)
|
2011-06-16 09:45:33 -04:00
|
|
|
|
2011-08-07 04:19:28 -04:00
|
|
|
@last_result_is_exception = true
|
|
|
|
@output_array << ex
|
2012-01-08 01:01:15 -05:00
|
|
|
@last_exception = ex
|
2011-03-14 06:55:22 -04:00
|
|
|
end
|
|
|
|
|
2011-08-07 04:19:28 -04:00
|
|
|
# Update Pry's internal state after evalling code.
|
|
|
|
# This method should not need to be invoked directly.
|
2011-08-11 07:35:04 -04:00
|
|
|
# @param [String] code The code we just eval'd
|
2011-08-07 04:19:28 -04:00
|
|
|
def update_input_history(code)
|
|
|
|
# Always push to the @input_array as the @output_array is always pushed to.
|
|
|
|
@input_array << code
|
|
|
|
if code
|
|
|
|
Pry.line_buffer.push(*code.each_line)
|
|
|
|
Pry.current_line += code.each_line.count
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-16 17:17:30 -04:00
|
|
|
# @return [Boolean] True if the last result is an exception that was raised,
|
|
|
|
# as opposed to simply an instance of Exception (like the result of
|
|
|
|
# Exception.new)
|
|
|
|
def last_result_is_exception?
|
|
|
|
@last_result_is_exception
|
|
|
|
end
|
|
|
|
|
2011-09-16 02:14:47 -04:00
|
|
|
# Manage switching of input objects on encountering EOFErrors
|
|
|
|
def handle_read_errors
|
|
|
|
should_retry = true
|
|
|
|
begin
|
|
|
|
yield
|
|
|
|
rescue EOFError
|
|
|
|
if input_stack.empty?
|
|
|
|
self.input = Pry.config.input
|
|
|
|
if !should_retry
|
|
|
|
output.puts "Error: Pry ran out of things to read from! Attempting to break out of REPL."
|
|
|
|
throw(:breakout)
|
|
|
|
end
|
|
|
|
should_retry = false
|
|
|
|
else
|
|
|
|
self.input = input_stack.pop
|
|
|
|
end
|
2012-06-17 06:50:18 -04:00
|
|
|
|
2011-09-16 02:14:47 -04:00
|
|
|
retry
|
2012-04-01 15:13:34 -04:00
|
|
|
|
|
|
|
# Interrupts are handled in r() because they need to tweak eval_string
|
|
|
|
# TODO: Refactor this baby.
|
|
|
|
rescue Interrupt
|
|
|
|
raise
|
|
|
|
|
|
|
|
# If we get a random error when trying to read a line we don't want to automatically
|
|
|
|
# retry, as the user will see a lot of error messages scroll past and be unable to do
|
|
|
|
# anything about it.
|
2012-03-24 15:32:28 -04:00
|
|
|
rescue RescuableException => e
|
2012-04-01 15:13:34 -04:00
|
|
|
puts "Error: #{e.message}"
|
2012-04-14 03:15:29 -04:00
|
|
|
puts "FATAL: Pry failed to get user input using `#{input}`."
|
2012-04-01 15:13:34 -04:00
|
|
|
puts "To fix this you may be able to pass input and output file descriptors to pry directly. e.g."
|
|
|
|
puts " Pry.config.input = STDIN"
|
|
|
|
puts " Pry.config.output = STDOUT"
|
|
|
|
puts " binding.pry"
|
|
|
|
throw(:breakout)
|
2011-09-16 02:14:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
private :handle_read_errors
|
|
|
|
|
2011-01-10 01:29:26 -05:00
|
|
|
# Returns the next line of input to be used by the pry instance.
|
|
|
|
# This method should not need to be invoked directly.
|
|
|
|
# @param [String] current_prompt The prompt to use for input.
|
|
|
|
# @return [String] The next line of input.
|
2012-04-14 03:29:33 -04:00
|
|
|
def readline(current_prompt="> ", completion_proc=nil)
|
2011-09-16 02:14:47 -04:00
|
|
|
handle_read_errors do
|
2012-04-14 03:29:33 -04:00
|
|
|
|
|
|
|
if defined? Coolline and input.is_a? Coolline
|
|
|
|
input.completion_proc = proc do |cool|
|
|
|
|
completion_proc.call cool.completed_word
|
|
|
|
end
|
|
|
|
elsif input.respond_to? :completion_proc=
|
|
|
|
input.completion_proc = completion_proc
|
|
|
|
end
|
|
|
|
|
2011-09-16 00:19:42 -04:00
|
|
|
if input == Readline
|
2011-10-09 04:41:42 -04:00
|
|
|
input.readline(current_prompt, false) # false since we'll add it manually
|
2011-11-02 06:09:29 -04:00
|
|
|
elsif defined? Coolline and input.is_a? Coolline
|
|
|
|
input.readline(current_prompt)
|
2011-09-16 00:19:42 -04:00
|
|
|
else
|
2011-04-14 15:51:00 -04:00
|
|
|
if input.method(:readline).arity == 1
|
|
|
|
input.readline(current_prompt)
|
|
|
|
else
|
|
|
|
input.readline
|
|
|
|
end
|
2011-09-16 00:19:42 -04:00
|
|
|
end
|
2011-01-10 01:29:26 -05:00
|
|
|
end
|
2011-01-13 09:35:46 -05:00
|
|
|
end
|
2011-01-10 01:29:26 -05:00
|
|
|
|
2011-04-10 19:10:05 -04:00
|
|
|
# Whether the print proc should be invoked.
|
2011-04-18 00:47:35 -04:00
|
|
|
# Currently only invoked if the output is not suppressed OR the last result
|
2011-04-10 19:10:05 -04:00
|
|
|
# is an exception regardless of suppression.
|
|
|
|
# @return [Boolean] Whether the print proc should be invoked.
|
2011-04-18 00:47:35 -04:00
|
|
|
def should_print?
|
|
|
|
!@suppress_output || last_result_is_exception?
|
2011-04-10 19:10:05 -04:00
|
|
|
end
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2010-12-27 22:56:23 -05:00
|
|
|
# Returns the appropriate prompt to use.
|
|
|
|
# This method should not need to be invoked directly.
|
2011-11-22 09:29:53 -05:00
|
|
|
# @param [String] eval_string The current input buffer.
|
|
|
|
# @param [Binding] target The target Binding of the Pry session.
|
2010-12-27 22:56:23 -05:00
|
|
|
# @return [String] The prompt.
|
2011-11-22 09:29:53 -05:00
|
|
|
def select_prompt(eval_string, target)
|
|
|
|
target_self = target.eval('self')
|
2011-01-13 09:35:46 -05:00
|
|
|
|
2011-11-22 09:29:53 -05:00
|
|
|
# If input buffer is empty then use normal prompt
|
|
|
|
if eval_string.empty?
|
2011-09-02 05:12:05 -04:00
|
|
|
Array(prompt).first.call(target_self, binding_stack.size - 1, self)
|
2011-11-22 09:29:53 -05:00
|
|
|
|
|
|
|
# Otherwise use the wait prompt (indicating multi-line expression)
|
2010-12-25 08:59:37 -05:00
|
|
|
else
|
2011-09-02 05:12:05 -04:00
|
|
|
Array(prompt).last.call(target_self, binding_stack.size - 1, self)
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-30 17:16:20 -04:00
|
|
|
# the array that the prompt stack is stored in
|
|
|
|
def prompt_stack
|
|
|
|
@prompt_stack ||= Array.new
|
|
|
|
end
|
|
|
|
private :prompt_stack
|
|
|
|
|
|
|
|
# Pushes the current prompt onto a stack that it can be restored from later.
|
|
|
|
# Use this if you wish to temporarily change the prompt.
|
|
|
|
# @param [Array<Proc>] new_prompt
|
|
|
|
# @return [Array<Proc>] new_prompt
|
|
|
|
# @example
|
|
|
|
# new_prompt = [ proc { '>' }, proc { '>>' } ]
|
|
|
|
# push_prompt(new_prompt) # => new_prompt
|
|
|
|
def push_prompt(new_prompt)
|
|
|
|
prompt_stack.push new_prompt
|
|
|
|
end
|
|
|
|
|
|
|
|
# Pops the current prompt off of the prompt stack.
|
|
|
|
# If the prompt you are popping is the last prompt, it will not be popped.
|
|
|
|
# Use this to restore the previous prompt.
|
|
|
|
# @return [Array<Proc>] Prompt being popped.
|
|
|
|
# @example
|
|
|
|
# prompt1 = [ proc { '>' }, proc { '>>' } ]
|
|
|
|
# prompt2 = [ proc { '$' }, proc { '>' } ]
|
|
|
|
# pry = Pry.new :prompt => prompt1
|
|
|
|
# pry.push_prompt(prompt2)
|
|
|
|
# pry.pop_prompt # => prompt2
|
|
|
|
# pry.pop_prompt # => prompt1
|
|
|
|
# pry.pop_prompt # => prompt1
|
|
|
|
def pop_prompt
|
2011-05-19 13:18:21 -04:00
|
|
|
prompt_stack.size > 1 ? prompt_stack.pop : prompt
|
2011-04-30 17:16:20 -04:00
|
|
|
end
|
|
|
|
|
2012-03-18 02:45:42 -04:00
|
|
|
# Raise an exception out of Pry.
|
|
|
|
#
|
|
|
|
# See Kernel#raise for documentation of parameters.
|
|
|
|
# See rb_make_exception for the inbuilt implementation.
|
|
|
|
#
|
|
|
|
# This is necessary so that the raise-up command can tell the
|
|
|
|
# difference between an exception the user has decided to raise,
|
|
|
|
# and a mistake in specifying that exception.
|
|
|
|
#
|
|
|
|
# (i.e. raise-up RunThymeError.new should not be the same as
|
|
|
|
# raise-up NameError, "unititialized constant RunThymeError")
|
|
|
|
#
|
|
|
|
def raise_up_common(force, *args)
|
|
|
|
exception = if args == []
|
|
|
|
last_exception || RuntimeError.new
|
|
|
|
elsif args.length == 1 && args.first.is_a?(String)
|
|
|
|
RuntimeError.new(args.first)
|
|
|
|
elsif args.length > 3
|
|
|
|
raise ArgumentError, "wrong number of arguments"
|
|
|
|
elsif !args.first.respond_to?(:exception)
|
|
|
|
raise TypeError, "exception class/object expected"
|
|
|
|
elsif args.length === 1
|
|
|
|
args.first.exception
|
|
|
|
else
|
|
|
|
args.first.exception(args[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
raise TypeError, "exception object expected" unless exception.is_a? Exception
|
|
|
|
|
|
|
|
exception.set_backtrace(args.length === 3 ? args[2] : caller(1))
|
|
|
|
|
|
|
|
if force || binding_stack.one?
|
|
|
|
binding_stack.clear
|
|
|
|
throw :raise_up, exception
|
|
|
|
else
|
|
|
|
binding_stack.pop
|
|
|
|
raise exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def raise_up(*args); raise_up_common(false, *args); end
|
|
|
|
def raise_up!(*args); raise_up_common(true, *args); end
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|