2011-02-19 00:21:13 -05:00
|
|
|
# (C) John Mair (banisterfiend) 2011
|
2010-12-08 08:49:28 -05:00
|
|
|
# MIT License
|
2011-10-08 17:12:25 -04:00
|
|
|
#
|
2010-12-08 08:49:28 -05:00
|
|
|
|
2011-06-08 10:49:35 -04:00
|
|
|
require 'pp'
|
2011-06-11 06:44:30 -04:00
|
|
|
require 'pry/helpers/base_helpers'
|
2011-10-16 21:09:12 -04:00
|
|
|
require 'pry/hooks'
|
|
|
|
|
2011-05-20 10:50:55 -04:00
|
|
|
class Pry
|
|
|
|
# The default hooks - display messages when beginning and ending Pry sessions.
|
2011-11-15 08:49:11 -05:00
|
|
|
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_|
|
2012-03-31 17:18:03 -04:00
|
|
|
next if _pry_.quiet?
|
2012-06-11 02:37:47 -04:00
|
|
|
_pry_.run_command("whereami --quiet", "", target)
|
2011-10-16 21:09:12 -04:00
|
|
|
end
|
2011-05-20 10:50:55 -04:00
|
|
|
|
2011-09-07 22:17:41 -04:00
|
|
|
# The default print
|
2011-05-20 10:50:55 -04:00
|
|
|
DEFAULT_PRINT = proc do |output, value|
|
2012-11-05 02:30:45 -05:00
|
|
|
output_with_default_format(output, value, :hashrocket => true)
|
2012-10-15 04:20:18 -04:00
|
|
|
end
|
|
|
|
|
2012-11-05 02:30:45 -05:00
|
|
|
def self.output_with_default_format(output, value, options = {})
|
2011-08-24 04:07:52 -04:00
|
|
|
stringified = begin
|
|
|
|
value.pretty_inspect
|
2011-09-15 05:43:58 -04:00
|
|
|
rescue RescuableException
|
2011-08-24 04:07:52 -04:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
unless String === stringified
|
2012-10-15 04:20:18 -04:00
|
|
|
# Read the class name off of the singleton class to provide a default
|
|
|
|
# inspect.
|
2011-08-24 04:07:52 -04:00
|
|
|
klass = (class << value; self; end).ancestors.first
|
|
|
|
stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>"
|
2011-07-25 09:06:57 -04:00
|
|
|
end
|
2011-08-24 04:07:52 -04:00
|
|
|
|
2011-10-19 02:24:37 -04:00
|
|
|
nonce = rand(0x100000000).to_s(16) # whatever
|
|
|
|
|
2012-10-15 04:20:18 -04:00
|
|
|
stringified.gsub!(/#</, "%<#{nonce}")
|
2012-11-28 18:21:14 -05:00
|
|
|
# Don't recolorize output with color (for cucumber, looksee, etc.) [Issue #751]
|
|
|
|
colorized = if stringified =~ /\e\[/
|
|
|
|
stringified
|
|
|
|
else
|
|
|
|
Helpers::BaseHelpers.colorize_code(stringified)
|
|
|
|
end
|
2011-10-19 02:24:37 -04:00
|
|
|
|
2011-11-27 01:43:00 -05:00
|
|
|
# avoid colour-leak from CodeRay and any of the users' previous output
|
2012-09-25 08:18:16 -04:00
|
|
|
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color
|
2011-11-27 01:43:00 -05:00
|
|
|
|
2012-11-08 02:58:18 -05:00
|
|
|
result = colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')
|
2013-01-14 12:31:48 -05:00
|
|
|
result = "=> #{result}"if options[:hashrocket]
|
2012-10-15 04:20:18 -04:00
|
|
|
Helpers::BaseHelpers.stagger_output(result, output)
|
2011-05-20 10:50:55 -04:00
|
|
|
end
|
|
|
|
|
2011-09-07 03:56:23 -04:00
|
|
|
# may be convenient when working with enormous objects and
|
|
|
|
# pretty_print is too slow
|
|
|
|
SIMPLE_PRINT = proc do |output, value|
|
|
|
|
begin
|
2012-11-08 02:58:18 -05:00
|
|
|
output.puts value.inspect
|
2011-09-07 03:56:23 -04:00
|
|
|
rescue RescuableException
|
2012-11-08 02:58:18 -05:00
|
|
|
output.puts "unknown"
|
2011-09-07 03:56:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-07 23:36:24 -04:00
|
|
|
# useful when playing with truly enormous objects
|
|
|
|
CLIPPED_PRINT = proc do |output, value|
|
2012-11-08 02:58:18 -05:00
|
|
|
output.puts Pry.view_clip(value)
|
2011-09-07 23:36:24 -04:00
|
|
|
end
|
|
|
|
|
2011-05-20 10:50:55 -04:00
|
|
|
# Will only show the first line of the backtrace
|
2011-09-10 12:39:51 -04:00
|
|
|
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception, _|
|
2013-01-12 14:26:02 -05:00
|
|
|
if UserError === exception && SyntaxError === exception
|
|
|
|
output.puts "SyntaxError: #{exception.message.sub(/.*syntax error, */m, '')}"
|
|
|
|
else
|
|
|
|
output.puts "#{exception.class}: #{exception.message}"
|
|
|
|
output.puts "from #{exception.backtrace.first}"
|
|
|
|
end
|
2011-05-20 10:50:55 -04:00
|
|
|
end
|
|
|
|
|
2012-08-22 15:12:31 -04:00
|
|
|
DEFAULT_PROMPT_NAME = 'pry'
|
|
|
|
|
2011-05-20 10:50:55 -04:00
|
|
|
# The default prompt; includes the target and nesting level
|
|
|
|
DEFAULT_PROMPT = [
|
2011-09-27 10:25:09 -04:00
|
|
|
proc { |target_self, nest_level, pry|
|
2012-08-22 15:12:31 -04:00
|
|
|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> "
|
2011-09-07 23:36:24 -04:00
|
|
|
},
|
|
|
|
|
2011-09-27 10:25:09 -04:00
|
|
|
proc { |target_self, nest_level, pry|
|
2012-08-22 15:12:31 -04:00
|
|
|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* "
|
2011-09-07 23:36:24 -04:00
|
|
|
}
|
2011-08-29 10:21:51 -04:00
|
|
|
]
|
2011-09-07 23:36:24 -04:00
|
|
|
|
2011-05-20 10:50:55 -04:00
|
|
|
# A simple prompt - doesn't display target or nesting level
|
2011-07-26 13:16:25 -04:00
|
|
|
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
|
2011-05-20 10:50:55 -04:00
|
|
|
|
|
|
|
SHELL_PROMPT = [
|
2012-08-22 15:12:31 -04:00
|
|
|
proc { |target_self, _, _| "#{Pry.config.prompt_name} #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
|
|
|
|
proc { |target_self, _, _| "#{Pry.config.prompt_name} #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
|
2011-09-07 03:56:23 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
# A prompt that includes the full object path as well as
|
|
|
|
# input/output (_in_ and _out_) information. Good for navigation.
|
|
|
|
NAV_PROMPT = [
|
2012-07-12 07:13:13 -04:00
|
|
|
proc do |conf|
|
|
|
|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
2012-08-22 15:12:31 -04:00
|
|
|
"[#{conf.expr_number}] (#{Pry.config.prompt_name}) #{tree}: #{conf.nesting_level}> "
|
2011-09-07 23:36:24 -04:00
|
|
|
end,
|
2012-07-12 07:13:13 -04:00
|
|
|
proc do |conf|
|
|
|
|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
2012-08-22 15:12:31 -04:00
|
|
|
"[#{conf.expr_number}] (#{ Pry.config.prompt_name}) #{tree}: #{conf.nesting_level}* "
|
2011-09-07 23:36:24 -04:00
|
|
|
end,
|
|
|
|
]
|
2011-09-07 03:56:23 -04:00
|
|
|
|
2012-12-14 20:23:24 -05:00
|
|
|
# Deal with the ^D key being pressed. Different behaviour in different cases:
|
|
|
|
# 1. In an expression behave like `!` command.
|
|
|
|
# 2. At top-level session behave like `exit` command.
|
|
|
|
# 3. In a nested session behave like `cd ..`.
|
2011-09-27 10:25:09 -04:00
|
|
|
DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_|
|
|
|
|
if !eval_string.empty?
|
2012-12-14 20:23:24 -05:00
|
|
|
eval_string.replace('') # Clear input buffer.
|
2011-09-27 10:25:09 -04:00
|
|
|
elsif _pry_.binding_stack.one?
|
|
|
|
_pry_.binding_stack.clear
|
|
|
|
throw(:breakout)
|
|
|
|
else
|
2012-12-14 20:23:24 -05:00
|
|
|
# Store the entire binding stack before popping. Useful for `cd -`.
|
|
|
|
if _pry_.command_state['cd'].nil?
|
|
|
|
_pry_.command_state['cd'] = OpenStruct.new
|
|
|
|
end
|
|
|
|
_pry_.command_state['cd'].old_stack = _pry_.binding_stack.dup
|
2012-06-27 07:54:07 -04:00
|
|
|
_pry_.binding_stack.pop
|
2011-09-27 10:25:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-17 08:42:29 -04:00
|
|
|
DEFAULT_SYSTEM = proc do |output, cmd, _|
|
2011-09-17 02:35:17 -04:00
|
|
|
if !system(cmd)
|
|
|
|
output.puts "Error: there was a problem executing system command: #{cmd}"
|
|
|
|
end
|
|
|
|
end
|
2011-05-20 10:50:55 -04:00
|
|
|
|
2012-08-13 01:41:28 -04:00
|
|
|
# Store the current working directory. This allows show-source etc. to work if
|
|
|
|
# your process has changed directory since boot. [Issue #675]
|
|
|
|
INITIAL_PWD = Dir.pwd
|
|
|
|
|
2011-08-26 20:44:13 -04:00
|
|
|
# As a REPL, we often want to catch any unexpected exceptions that may have
|
|
|
|
# been raised; however we don't want to go overboard and prevent the user
|
|
|
|
# from exiting Pry when they want to.
|
|
|
|
module RescuableException
|
|
|
|
def self.===(exception)
|
|
|
|
case exception
|
2011-09-07 23:36:24 -04:00
|
|
|
# Catch when the user hits ^C (Interrupt < SignalException), and assume
|
|
|
|
# that they just wanted to stop the in-progress command (just like bash etc.)
|
2011-08-26 20:44:13 -04:00
|
|
|
when Interrupt
|
|
|
|
true
|
2011-09-07 23:36:24 -04:00
|
|
|
# Don't catch signals (particularly not SIGTERM) as these are unlikely to be
|
|
|
|
# intended for pry itself. We should also make sure that Kernel#exit works.
|
2011-09-17 21:45:46 -04:00
|
|
|
when *Pry.config.exception_whitelist
|
2011-08-26 20:44:13 -04:00
|
|
|
false
|
2011-09-07 23:36:24 -04:00
|
|
|
# All other exceptions will be caught.
|
2011-08-26 20:44:13 -04:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-12 14:26:02 -05:00
|
|
|
# An Exception Tag (cf. Exceptional Ruby) that instructs Pry to show the error in
|
|
|
|
# a more user-friendly manner. This should be used when the exception happens within
|
|
|
|
# Pry itself as a direct consequence of the user typing something wrong.
|
|
|
|
#
|
|
|
|
# This allows us to distinguish between the user typing:
|
|
|
|
#
|
|
|
|
# pry(main)> def )
|
|
|
|
# SyntaxError: unexpected )
|
|
|
|
#
|
|
|
|
# pry(main)> method_that_evals("def )")
|
|
|
|
# SyntaxError: (eval):1: syntax error, unexpected ')'
|
|
|
|
# from ./a.rb:2 in `eval'
|
|
|
|
module UserError; end
|
|
|
|
|
2012-11-08 02:21:52 -05:00
|
|
|
# Catches SecurityErrors if $SAFE is set
|
|
|
|
module TooSafeException
|
|
|
|
def self.===(exception)
|
|
|
|
$SAFE > 0 && SecurityError === exception
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Don't catch these exceptions
|
|
|
|
DEFAULT_EXCEPTION_WHITELIST = [SystemExit, SignalException, Pry::TooSafeException]
|
|
|
|
|
2011-10-01 19:57:39 -04:00
|
|
|
# CommandErrors are caught by the REPL loop and displayed to the user. They
|
|
|
|
# indicate an exceptional condition that's fatal to the current command.
|
2012-01-16 02:45:08 -05:00
|
|
|
class CommandError < StandardError; end
|
2012-07-17 21:55:18 -04:00
|
|
|
class MethodNotFound < CommandError; end
|
2012-01-23 01:33:34 -05:00
|
|
|
|
2012-01-23 07:10:35 -05:00
|
|
|
# indicates obsolete API
|
|
|
|
class ObsoleteError < StandardError; end
|
|
|
|
|
2012-01-23 01:33:34 -05:00
|
|
|
# This is to keep from breaking under Rails 3.2 for people who are doing that
|
|
|
|
# IRB = Pry thing.
|
|
|
|
module ExtendCommandBundle
|
|
|
|
end
|
2011-05-20 10:50:55 -04:00
|
|
|
end
|
|
|
|
|
2012-04-18 02:18:33 -04:00
|
|
|
if Pry::Helpers::BaseHelpers.mri_18?
|
|
|
|
begin
|
|
|
|
require 'ruby18_source_location'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-18 17:18:54 -04:00
|
|
|
require 'method_source'
|
2011-03-02 06:18:26 -05:00
|
|
|
require 'shellwords'
|
2012-08-18 17:18:54 -04:00
|
|
|
require 'stringio'
|
|
|
|
require 'coderay'
|
|
|
|
require 'slop'
|
|
|
|
require 'rbconfig'
|
2012-08-11 20:22:29 -04:00
|
|
|
require 'tempfile'
|
2011-03-02 06:18:26 -05:00
|
|
|
|
2012-06-05 02:26:38 -04:00
|
|
|
begin
|
|
|
|
require 'readline'
|
|
|
|
rescue LoadError
|
|
|
|
warn "You're running a version of ruby with no Readline support"
|
|
|
|
warn "Please `gem install rb-readline` or recompile ruby --with-readline."
|
|
|
|
exit!
|
|
|
|
end
|
|
|
|
|
2011-12-26 19:48:59 -05:00
|
|
|
if Pry::Helpers::BaseHelpers.jruby?
|
2011-09-11 00:28:31 -04:00
|
|
|
begin
|
|
|
|
require 'ffi'
|
|
|
|
rescue LoadError
|
2012-01-22 22:40:40 -05:00
|
|
|
warn "Need to `gem install ffi`"
|
2011-09-11 00:28:31 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-23 18:06:32 -04:00
|
|
|
if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
|
2011-03-02 06:18:26 -05:00
|
|
|
begin
|
|
|
|
require 'win32console'
|
2012-06-23 18:06:32 -04:00
|
|
|
# The mswin and mingw versions of pry require win32console, so this should
|
|
|
|
# only fail on jruby (where win32console doesn't work).
|
|
|
|
# Instead we'll recommend ansicon, which does.
|
2011-03-02 06:18:26 -05:00
|
|
|
rescue LoadError
|
2012-06-23 18:06:32 -04:00
|
|
|
warn "For a better pry experience, please use ansicon: http://adoxa.3eeweb.com/ansicon/"
|
2011-03-02 06:18:26 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-06 02:14:53 -04:00
|
|
|
begin
|
|
|
|
require 'bond'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2012-08-18 17:18:54 -04:00
|
|
|
require 'pry/version'
|
|
|
|
require 'pry/rbx_method'
|
|
|
|
require 'pry/rbx_path'
|
|
|
|
require 'pry/code'
|
|
|
|
require 'pry/history_array'
|
|
|
|
require 'pry/helpers'
|
2012-12-25 07:47:33 -05:00
|
|
|
require 'pry/code_object'
|
|
|
|
require 'pry/method'
|
|
|
|
require 'pry/wrapped_module'
|
2012-08-18 17:18:54 -04:00
|
|
|
require 'pry/history'
|
|
|
|
require 'pry/command'
|
|
|
|
require 'pry/command_set'
|
|
|
|
require 'pry/commands'
|
|
|
|
require 'pry/custom_completions'
|
|
|
|
require 'pry/completion'
|
|
|
|
require 'pry/plugins'
|
|
|
|
require 'pry/core_extensions'
|
|
|
|
require 'pry/pry_class'
|
|
|
|
require 'pry/pry_instance'
|
|
|
|
require 'pry/cli'
|
|
|
|
require 'pry/pager'
|
2012-11-29 05:51:08 -05:00
|
|
|
require 'pry/terminal_info'
|
2012-12-29 17:34:59 -05:00
|
|
|
require 'pry/editor'
|
2013-01-08 17:24:53 -05:00
|
|
|
require 'pry/rubygem'
|