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_|
|
2011-10-16 21:09:12 -04:00
|
|
|
# ensure we're actually in a method
|
|
|
|
file = target.eval('__FILE__')
|
2011-05-20 10:50:55 -04:00
|
|
|
|
2011-10-16 21:09:12 -04:00
|
|
|
# /unknown/ for rbx
|
|
|
|
if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e"
|
2011-11-19 21:16:23 -05:00
|
|
|
_pry_.run_command("whereami 5", "", target)
|
2011-05-20 10:50:55 -04:00
|
|
|
end
|
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|
|
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
|
|
|
|
# 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-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
|
|
|
|
|
|
|
|
colorized = Helpers::BaseHelpers.colorize_code(stringified.gsub(/#</, "%<#{nonce}"))
|
|
|
|
|
2011-11-27 01:43:00 -05: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 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
|
|
|
|
output.puts "=> #{value.inspect}"
|
|
|
|
rescue RescuableException
|
|
|
|
output.puts "=> unknown"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-07 23:36:24 -04:00
|
|
|
# useful when playing with truly enormous objects
|
|
|
|
CLIPPED_PRINT = proc do |output, value|
|
|
|
|
output.puts "=> #{Pry.view_clip(value)}"
|
|
|
|
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, _|
|
2011-05-20 10:50:55 -04:00
|
|
|
output.puts "#{exception.class}: #{exception.message}"
|
|
|
|
output.puts "from #{exception.backtrace.first}"
|
|
|
|
end
|
|
|
|
|
2011-09-17 21:45:46 -04:00
|
|
|
# Don't catch these exceptions
|
|
|
|
DEFAULT_EXCEPTION_WHITELIST = [SystemExit, SignalException]
|
|
|
|
|
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|
|
2011-10-27 23:38:39 -04:00
|
|
|
"[#{pry.input_array.size}] pry(#{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|
|
2011-10-27 23:38:39 -04:00
|
|
|
"[#{pry.input_array.size}] pry(#{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 = [
|
2011-09-07 23:36:24 -04: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 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 = [
|
2011-09-07 23:36:24 -04:00
|
|
|
proc do |_, level, pry|
|
|
|
|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
|
|
|
"[#{pry.input_array.size}] (pry) #{tree}: #{level}> "
|
|
|
|
end,
|
|
|
|
proc do |_, level, pry|
|
|
|
|
tree = pry.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
|
|
|
|
"[#{pry.input_array.size}] (pry) #{tree}: #{level}* "
|
|
|
|
end,
|
|
|
|
]
|
2011-09-07 03:56:23 -04:00
|
|
|
|
2011-09-27 10:25:09 -04: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?
|
|
|
|
# clear input buffer
|
|
|
|
eval_string.replace("")
|
|
|
|
elsif _pry_.binding_stack.one?
|
|
|
|
# ^D at top-level breaks out of loop
|
|
|
|
_pry_.binding_stack.clear
|
|
|
|
throw(:breakout)
|
|
|
|
else
|
|
|
|
# otherwise just pops a binding
|
|
|
|
_pry_.binding_stack.pop
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
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.
|
|
|
|
class CommandError < StandardError
|
|
|
|
end
|
2011-05-20 10:50:55 -04:00
|
|
|
end
|
|
|
|
|
2010-12-18 06:21:58 -05:00
|
|
|
require "method_source"
|
2011-03-02 06:18:26 -05:00
|
|
|
require 'shellwords'
|
2011-01-10 08:54:17 -05:00
|
|
|
require "readline"
|
2011-02-27 18:00:51 -05:00
|
|
|
require "stringio"
|
2011-03-02 06:18:26 -05:00
|
|
|
require "coderay"
|
2011-04-25 16:58:06 -04:00
|
|
|
require "optparse"
|
|
|
|
require "slop"
|
2011-03-02 06:18:26 -05:00
|
|
|
|
2011-09-11 00:28:31 -04:00
|
|
|
if RUBY_PLATFORM =~ /jruby/
|
|
|
|
begin
|
|
|
|
require 'ffi'
|
|
|
|
rescue LoadError
|
|
|
|
$stderr.puts "Need to `gem install ffi`"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-03-02 06:18:26 -05:00
|
|
|
if RUBY_PLATFORM =~ /mswin/ || RUBY_PLATFORM =~ /mingw/
|
|
|
|
begin
|
|
|
|
require 'win32console'
|
|
|
|
rescue LoadError
|
2011-04-18 17:31:39 -04:00
|
|
|
$stderr.puts "Need to `gem install win32console`"
|
2011-03-02 06:18:26 -05:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-24 07:56:47 -04:00
|
|
|
require "pry/version"
|
2011-09-19 02:42:31 -04:00
|
|
|
require "pry/rbx_method"
|
2011-09-19 04:18:57 -04:00
|
|
|
require "pry/rbx_path"
|
2011-09-19 02:42:31 -04:00
|
|
|
require "pry/method"
|
2011-12-02 01:55:48 -05:00
|
|
|
require "pry/wrapped_module"
|
2011-05-15 05:29:14 -04:00
|
|
|
require "pry/history_array"
|
2011-04-25 16:58:06 -04:00
|
|
|
require "pry/helpers"
|
2011-09-05 05:00:14 -04:00
|
|
|
require "pry/history"
|
2011-04-24 10:25:07 -04:00
|
|
|
require "pry/command_set"
|
2011-04-24 07:56:47 -04:00
|
|
|
require "pry/commands"
|
2011-04-25 07:26:25 -04:00
|
|
|
require "pry/command_context"
|
2011-04-24 07:56:47 -04:00
|
|
|
require "pry/custom_completions"
|
|
|
|
require "pry/completion"
|
2011-05-15 05:58:27 -04:00
|
|
|
require "pry/plugins"
|
2011-04-24 07:56:47 -04:00
|
|
|
require "pry/core_extensions"
|
|
|
|
require "pry/pry_class"
|
|
|
|
require "pry/pry_instance"
|
2011-12-06 21:07:02 -05:00
|
|
|
require "pry/cli"
|