2011-02-19 05:21:13 +00:00
|
|
|
# (C) John Mair (banisterfiend) 2011
|
2010-12-08 13:49:28 +00:00
|
|
|
# MIT License
|
|
|
|
|
2011-06-08 14:49:35 +00:00
|
|
|
require 'pp'
|
2011-06-11 10:44:30 +00:00
|
|
|
require 'pry/helpers/base_helpers'
|
2011-05-20 14:50:55 +00:00
|
|
|
class Pry
|
|
|
|
# The default hooks - display messages when beginning and ending Pry sessions.
|
|
|
|
DEFAULT_HOOKS = {
|
2011-09-07 15:13:27 +00:00
|
|
|
:before_session => proc do |out, target, _pry_|
|
2011-05-20 14:50:55 +00:00
|
|
|
# ensure we're actually in a method
|
|
|
|
file = target.eval('__FILE__')
|
|
|
|
|
|
|
|
# /unknown/ for rbx
|
|
|
|
if file !~ /(\(.*\))|<.*>/ && file !~ /__unknown__/ && file != "" && file != "-e"
|
2011-09-07 15:13:27 +00:00
|
|
|
_pry_.process_line("whereami 5", "", target)
|
2011-05-20 14:50:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
2011-09-08 02:17:41 +00:00
|
|
|
# The default print
|
2011-05-20 14:50:55 +00:00
|
|
|
DEFAULT_PRINT = proc do |output, value|
|
2011-08-24 08:07:52 +00:00
|
|
|
stringified = begin
|
|
|
|
value.pretty_inspect
|
2011-08-27 08:58:53 +00:00
|
|
|
rescue RescuableException => ex
|
2011-08-24 08:07:52 +00: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-08-27 17:49:30 +00:00
|
|
|
Helpers::BaseHelpers.stagger_output("output error: #{ex.inspect}", output) if ex
|
2011-07-25 13:06:57 +00:00
|
|
|
end
|
2011-08-24 08:07:52 +00:00
|
|
|
|
|
|
|
Helpers::BaseHelpers.stagger_output("=> #{Helpers::BaseHelpers.colorize_code(stringified)}", output)
|
2011-05-20 14:50:55 +00:00
|
|
|
end
|
|
|
|
|
2011-09-07 07:56:23 +00: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-05-20 14:50:55 +00:00
|
|
|
# Will only show the first line of the backtrace
|
|
|
|
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception|
|
|
|
|
output.puts "#{exception.class}: #{exception.message}"
|
|
|
|
output.puts "from #{exception.backtrace.first}"
|
|
|
|
end
|
|
|
|
|
|
|
|
# The default prompt; includes the target and nesting level
|
|
|
|
DEFAULT_PROMPT = [
|
2011-09-02 09:58:22 +00:00
|
|
|
proc { |target_self, nest_level, _|
|
2011-05-20 14:50:55 +00:00
|
|
|
if nest_level == 0
|
|
|
|
"pry(#{Pry.view_clip(target_self)})> "
|
|
|
|
else
|
2011-09-08 02:17:41 +00:00
|
|
|
"pry(#{Pry.view_clip(target_self)}):#{nest_level}> "
|
2011-05-20 14:50:55 +00:00
|
|
|
end
|
|
|
|
},
|
|
|
|
|
2011-09-02 09:58:22 +00:00
|
|
|
proc { |target_self, nest_level, _|
|
2011-05-20 14:50:55 +00:00
|
|
|
if nest_level == 0
|
|
|
|
"pry(#{Pry.view_clip(target_self)})* "
|
|
|
|
else
|
2011-09-08 02:17:41 +00:00
|
|
|
"pry(#{Pry.view_clip(target_self)}):#{nest_level}* "
|
2011-05-20 14:50:55 +00:00
|
|
|
end
|
|
|
|
}
|
2011-08-29 14:21:51 +00: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-05-20 14:50:55 +00:00
|
|
|
|
|
|
|
# A simple prompt - doesn't display target or nesting level
|
2011-07-26 17:16:25 +00:00
|
|
|
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
|
2011-05-20 14:50:55 +00:00
|
|
|
|
|
|
|
SHELL_PROMPT = [
|
2011-09-02 09:58:22 +00: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 07:56:23 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
# A prompt that includes the full object path as well as
|
|
|
|
# input/output (_in_ and _out_) information. Good for navigation.
|
|
|
|
NAV_PROMPT = [
|
|
|
|
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-05-20 14:50:55 +00:00
|
|
|
|
2011-08-27 00:44:13 +00: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
|
|
|
|
# Catch when the user hits ^C (Interrupt < SignalException), and assume
|
|
|
|
# that they just wanted to stop the in-progress command (just like bash etc.)
|
|
|
|
when Interrupt
|
|
|
|
true
|
|
|
|
# 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.
|
|
|
|
when SystemExit, SignalException
|
|
|
|
false
|
|
|
|
# All other exceptions will be caught.
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-20 14:50:55 +00:00
|
|
|
end
|
|
|
|
|
2010-12-18 11:21:58 +00:00
|
|
|
require "method_source"
|
2011-03-02 11:18:26 +00:00
|
|
|
require 'shellwords'
|
2011-01-10 13:54:17 +00:00
|
|
|
require "readline"
|
2011-02-27 23:00:51 +00:00
|
|
|
require "stringio"
|
2011-03-02 11:18:26 +00:00
|
|
|
require "coderay"
|
2011-04-25 20:58:06 +00:00
|
|
|
require "optparse"
|
|
|
|
require "slop"
|
|
|
|
require "rubygems/dependency_installer"
|
2011-03-02 11:18:26 +00:00
|
|
|
|
|
|
|
if RUBY_PLATFORM =~ /mswin/ || RUBY_PLATFORM =~ /mingw/
|
|
|
|
begin
|
|
|
|
require 'win32console'
|
|
|
|
rescue LoadError
|
2011-04-18 21:31:39 +00:00
|
|
|
$stderr.puts "Need to `gem install win32console`"
|
2011-03-02 11:18:26 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-04-24 11:56:47 +00:00
|
|
|
require "pry/version"
|
2011-05-15 09:29:14 +00:00
|
|
|
require "pry/history_array"
|
2011-04-25 20:58:06 +00:00
|
|
|
require "pry/helpers"
|
2011-09-05 09:00:14 +00:00
|
|
|
require "pry/history"
|
2011-04-24 14:25:07 +00:00
|
|
|
require "pry/command_set"
|
2011-04-24 11:56:47 +00:00
|
|
|
require "pry/commands"
|
2011-04-25 11:26:25 +00:00
|
|
|
require "pry/command_context"
|
2011-04-24 11:56:47 +00:00
|
|
|
require "pry/custom_completions"
|
|
|
|
require "pry/completion"
|
2011-05-15 09:58:27 +00:00
|
|
|
require "pry/plugins"
|
2011-04-24 11:56:47 +00:00
|
|
|
require "pry/core_extensions"
|
|
|
|
require "pry/pry_class"
|
|
|
|
require "pry/pry_instance"
|