2011-02-19 18:21:13 +13:00
|
|
|
# (C) John Mair (banisterfiend) 2011
|
2010-12-09 02:49:28 +13:00
|
|
|
# MIT License
|
2011-10-08 14:12:25 -07:00
|
|
|
#
|
2010-12-09 02:49:28 +13:00
|
|
|
|
2011-06-09 02:49:35 +12:00
|
|
|
require 'pp'
|
2011-06-11 22:44:30 +12:00
|
|
|
require 'pry/helpers/base_helpers'
|
2011-10-17 14:09:12 +13:00
|
|
|
require 'pry/hooks'
|
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
class Pry
|
|
|
|
# The default hooks - display messages when beginning and ending Pry sessions.
|
2011-11-16 02:49:11 +13:00
|
|
|
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_|
|
2012-03-31 14:18:03 -07:00
|
|
|
next if _pry_.quiet?
|
2012-06-10 23:37:47 -07:00
|
|
|
_pry_.run_command("whereami --quiet", "", target)
|
2011-10-17 14:09:12 +13:00
|
|
|
end
|
2011-05-20 15:50:55 +01:00
|
|
|
|
2011-09-08 14:17:41 +12:00
|
|
|
# The default print
|
2011-05-20 15:50:55 +01:00
|
|
|
DEFAULT_PRINT = proc do |output, value|
|
2011-08-24 01:07:52 -07:00
|
|
|
stringified = begin
|
|
|
|
value.pretty_inspect
|
2011-09-15 21:43:58 +12:00
|
|
|
rescue RescuableException
|
2011-08-24 01:07:52 -07:00
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
unless String === stringified
|
|
|
|
# Read the class name off of the singleton class to provide a default inspect.
|
|
|
|
klass = (class << value; self; end).ancestors.first
|
|
|
|
stringified = "#<#{klass}:0x#{value.__id__.to_s(16)}>"
|
2011-07-26 01:06:57 +12:00
|
|
|
end
|
2011-08-24 01:07:52 -07:00
|
|
|
|
2011-10-18 23:24:37 -07:00
|
|
|
nonce = rand(0x100000000).to_s(16) # whatever
|
|
|
|
|
|
|
|
colorized = Helpers::BaseHelpers.colorize_code(stringified.gsub(/#</, "%<#{nonce}"))
|
|
|
|
|
2011-11-26 22:43:00 -08:00
|
|
|
# avoid colour-leak from CodeRay and any of the users' previous output
|
|
|
|
colorized = colorized.sub(/(\n*)$/, "\e[0m\\1") if Pry.color
|
|
|
|
|
|
|
|
Helpers::BaseHelpers.stagger_output("=> #{colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')}", output)
|
2011-05-20 15:50:55 +01:00
|
|
|
end
|
|
|
|
|
2011-09-07 19:56:23 +12:00
|
|
|
# may be convenient when working with enormous objects and
|
|
|
|
# pretty_print is too slow
|
|
|
|
SIMPLE_PRINT = proc do |output, value|
|
|
|
|
begin
|
|
|
|
output.puts "=> #{value.inspect}"
|
|
|
|
rescue RescuableException
|
|
|
|
output.puts "=> unknown"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-08 15:36:24 +12:00
|
|
|
# useful when playing with truly enormous objects
|
|
|
|
CLIPPED_PRINT = proc do |output, value|
|
|
|
|
output.puts "=> #{Pry.view_clip(value)}"
|
|
|
|
end
|
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
# Will only show the first line of the backtrace
|
2011-09-11 04:39:51 +12:00
|
|
|
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception, _|
|
2011-05-20 15:50:55 +01:00
|
|
|
output.puts "#{exception.class}: #{exception.message}"
|
|
|
|
output.puts "from #{exception.backtrace.first}"
|
|
|
|
end
|
|
|
|
|
2011-09-18 09:45:46 +08:00
|
|
|
# Don't catch these exceptions
|
|
|
|
DEFAULT_EXCEPTION_WHITELIST = [SystemExit, SignalException]
|
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
# The default prompt; includes the target and nesting level
|
|
|
|
DEFAULT_PROMPT = [
|
2011-09-28 03:25:09 +13:00
|
|
|
proc { |target_self, nest_level, pry|
|
2011-10-27 22:38:39 -05:00
|
|
|
"[#{pry.input_array.size}] pry(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> "
|
2011-09-08 15:36:24 +12:00
|
|
|
},
|
|
|
|
|
2011-09-28 03:25:09 +13:00
|
|
|
proc { |target_self, nest_level, pry|
|
2011-10-27 22:38:39 -05:00
|
|
|
"[#{pry.input_array.size}] pry(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* "
|
2011-09-08 15:36:24 +12:00
|
|
|
}
|
2011-08-30 02:21:51 +12:00
|
|
|
]
|
2011-09-08 15:36:24 +12:00
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
# A simple prompt - doesn't display target or nesting level
|
2011-07-27 05:16:25 +12:00
|
|
|
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
|
2011-05-20 15:50:55 +01:00
|
|
|
|
|
|
|
SHELL_PROMPT = [
|
2011-09-08 15:36:24 +12:00
|
|
|
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} $ " },
|
|
|
|
proc { |target_self, _, _| "pry #{Pry.view_clip(target_self)}:#{Dir.pwd} * " }
|
2011-09-07 19:56:23 +12: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 23:13:13 +12:00
|
|
|
proc do |conf|
|
|
|
|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
2012-07-13 02:58:25 +12:00
|
|
|
"[#{conf.expr_number}] (pry) #{tree}: #{conf.nesting_level}> "
|
2011-09-08 15:36:24 +12:00
|
|
|
end,
|
2012-07-12 23:13:13 +12:00
|
|
|
proc do |conf|
|
|
|
|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
2012-07-13 02:58:25 +12:00
|
|
|
"[#{conf.expr_number}] (pry) #{tree}: #{conf.nesting_level}* "
|
2011-09-08 15:36:24 +12:00
|
|
|
end,
|
|
|
|
]
|
2011-09-07 19:56:23 +12:00
|
|
|
|
2011-09-28 03:25:09 +13:00
|
|
|
# Deal with the ^D key being pressed, different behaviour in
|
|
|
|
# different cases:
|
|
|
|
# 1) In an expression - behave like `!` command (clear input buffer)
|
|
|
|
# 2) At top-level session - behave like `exit command (break out of repl loop)
|
|
|
|
# 3) In a nested session - behave like `cd ..` (pop a binding)
|
|
|
|
DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_|
|
|
|
|
if !eval_string.empty?
|
2012-06-27 14:54:07 +03:00
|
|
|
# Clear input buffer.
|
2012-06-16 21:59:14 +02:00
|
|
|
eval_string.replace("")
|
2011-09-28 03:25:09 +13:00
|
|
|
elsif _pry_.binding_stack.one?
|
2012-06-27 14:54:07 +03:00
|
|
|
# ^D at top-level breaks out of a REPL loop.
|
2011-09-28 03:25:09 +13:00
|
|
|
_pry_.binding_stack.clear
|
|
|
|
throw(:breakout)
|
|
|
|
else
|
2012-06-27 14:54:07 +03:00
|
|
|
# Otherwise, saves current binding stack as old stack and pops last
|
|
|
|
# binding out of binding stack (the old stack still has that binding).
|
|
|
|
_pry_.command_state["cd"].old_stack = _pry_.binding_stack.dup
|
|
|
|
_pry_.binding_stack.pop
|
2011-09-28 03:25:09 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-18 00:42:29 +12:00
|
|
|
DEFAULT_SYSTEM = proc do |output, cmd, _|
|
2011-09-17 18:35:17 +12:00
|
|
|
if !system(cmd)
|
|
|
|
output.puts "Error: there was a problem executing system command: #{cmd}"
|
|
|
|
end
|
|
|
|
end
|
2011-05-20 15:50:55 +01:00
|
|
|
|
2012-08-12 22:41:28 -07: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 17:44:13 -07: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-08 15:36:24 +12: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 17:44:13 -07:00
|
|
|
when Interrupt
|
|
|
|
true
|
2011-09-08 15:36:24 +12: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-18 09:45:46 +08:00
|
|
|
when *Pry.config.exception_whitelist
|
2011-08-26 17:44:13 -07:00
|
|
|
false
|
2011-09-08 15:36:24 +12:00
|
|
|
# All other exceptions will be caught.
|
2011-08-26 17:44:13 -07:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-01 16:57:39 -07: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-15 23:45:08 -08:00
|
|
|
class CommandError < StandardError; end
|
2012-07-17 21:55:18 -04:00
|
|
|
class MethodNotFound < CommandError; end
|
2012-01-22 22:33:34 -08:00
|
|
|
|
2012-01-24 01:10:35 +13:00
|
|
|
# indicates obsolete API
|
|
|
|
class ObsoleteError < StandardError; end
|
|
|
|
|
2012-01-22 22:33:34 -08: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 15:50:55 +01:00
|
|
|
end
|
|
|
|
|
2012-04-18 18:18:33 +12:00
|
|
|
if Pry::Helpers::BaseHelpers.mri_18?
|
|
|
|
begin
|
|
|
|
require 'ruby18_source_location'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-12-19 00:21:58 +13:00
|
|
|
require "method_source"
|
2011-03-03 00:18:26 +13:00
|
|
|
require 'shellwords'
|
2011-02-28 12:00:51 +13:00
|
|
|
require "stringio"
|
2011-03-03 00:18:26 +13:00
|
|
|
require "coderay"
|
2011-04-25 22:58:06 +02:00
|
|
|
require "optparse"
|
2012-05-30 13:33:13 -07:00
|
|
|
require "slop"
|
2011-12-26 15:57:40 -06:00
|
|
|
require "rbconfig"
|
2012-08-11 17:22:29 -07:00
|
|
|
require 'tempfile'
|
2011-03-03 00:18:26 +13:00
|
|
|
|
2012-06-04 23:26:38 -07: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 18:48:59 -06:00
|
|
|
if Pry::Helpers::BaseHelpers.jruby?
|
2011-09-10 21:28:31 -07:00
|
|
|
begin
|
|
|
|
require 'ffi'
|
|
|
|
rescue LoadError
|
2012-01-23 16:40:40 +13:00
|
|
|
warn "Need to `gem install ffi`"
|
2011-09-10 21:28:31 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-06-23 15:06:32 -07:00
|
|
|
if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
|
2011-03-03 00:18:26 +13:00
|
|
|
begin
|
|
|
|
require 'win32console'
|
2012-06-23 15:06:32 -07: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-03 00:18:26 +13:00
|
|
|
rescue LoadError
|
2012-06-23 15:06:32 -07:00
|
|
|
warn "For a better pry experience, please use ansicon: http://adoxa.3eeweb.com/ansicon/"
|
2011-03-03 00:18:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-24 12:56:47 +01:00
|
|
|
require "pry/version"
|
2011-09-18 23:42:31 -07:00
|
|
|
require "pry/rbx_method"
|
2011-09-19 01:18:57 -07:00
|
|
|
require "pry/rbx_path"
|
2011-11-25 21:05:10 -08:00
|
|
|
require "pry/code"
|
2011-09-18 23:42:31 -07:00
|
|
|
require "pry/method"
|
2011-12-01 22:55:48 -08:00
|
|
|
require "pry/wrapped_module"
|
2011-05-15 11:29:14 +02:00
|
|
|
require "pry/history_array"
|
2011-04-25 22:58:06 +02:00
|
|
|
require "pry/helpers"
|
2011-09-05 02:00:14 -07:00
|
|
|
require "pry/history"
|
2011-12-31 11:50:04 +00:00
|
|
|
require "pry/command"
|
2011-04-24 16:25:07 +02:00
|
|
|
require "pry/command_set"
|
2011-04-24 12:56:47 +01:00
|
|
|
require "pry/commands"
|
|
|
|
require "pry/custom_completions"
|
|
|
|
require "pry/completion"
|
2011-05-15 21:58:27 +12:00
|
|
|
require "pry/plugins"
|
2011-04-24 12:56:47 +01:00
|
|
|
require "pry/core_extensions"
|
|
|
|
require "pry/pry_class"
|
|
|
|
require "pry/pry_instance"
|
2011-12-07 15:07:02 +13:00
|
|
|
require "pry/cli"
|
2012-08-08 22:57:19 +01:00
|
|
|
require "pry/pager"
|