2013-04-06 18:50:17 +03:00
|
|
|
# (C) John Mair (banisterfiend) 2013
|
2010-12-09 02:49:28 +13:00
|
|
|
# MIT License
|
2011-10-08 14:12:25 -07:00
|
|
|
#
|
2011-06-09 02:49:35 +12:00
|
|
|
require 'pp'
|
2013-09-03 11:04:25 +03:00
|
|
|
|
|
|
|
require 'pry/input_lock'
|
|
|
|
require 'pry/exceptions'
|
|
|
|
require 'pry/helpers/base_helpers'
|
|
|
|
require 'pry/hooks'
|
|
|
|
|
2013-10-24 02:17:51 +03:00
|
|
|
require 'securerandom'
|
2013-12-12 12:01:04 +09:00
|
|
|
require 'forwardable'
|
2013-10-24 02:17:51 +03:00
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
class Pry
|
2013-08-31 20:54:52 +02:00
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
# 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-12-18 00:26:51 -08:00
|
|
|
_pry_.run_command("whereami --quiet")
|
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|
|
2013-11-03 16:35:33 -08:00
|
|
|
Pry::Pager.with_pager(output) do |pager|
|
|
|
|
pager.print "=> "
|
2013-11-09 12:53:39 -08:00
|
|
|
Pry::ColorPrinter.pp(value, pager, Pry::Terminal.width! - 1)
|
2013-11-03 16:35:33 -08:00
|
|
|
end
|
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
|
2012-11-07 23:58:18 -08:00
|
|
|
output.puts value.inspect
|
2011-09-07 19:56:23 +12:00
|
|
|
rescue RescuableException
|
2012-11-07 23:58:18 -08:00
|
|
|
output.puts "unknown"
|
2011-09-07 19:56:23 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-09-08 15:36:24 +12:00
|
|
|
# useful when playing with truly enormous objects
|
|
|
|
CLIPPED_PRINT = proc do |output, value|
|
2012-11-07 23:58:18 -08:00
|
|
|
output.puts Pry.view_clip(value)
|
2011-09-08 15:36:24 +12:00
|
|
|
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, _|
|
2013-01-12 11:26:02 -08: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 15:50:55 +01:00
|
|
|
end
|
|
|
|
|
2012-08-22 14:12:31 -05:00
|
|
|
DEFAULT_PROMPT_NAME = 'pry'
|
|
|
|
|
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|
|
2012-08-22 14:12:31 -05:00
|
|
|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{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|
|
2012-08-22 14:12:31 -05:00
|
|
|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{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
|
|
|
|
2013-05-04 11:21:16 +03:00
|
|
|
DEFAULT_PROMPT_SAFE_OBJECTS = [String, Numeric, Symbol, nil, true, false]
|
|
|
|
|
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
|
|
|
|
2013-12-13 00:17:21 +02:00
|
|
|
NO_PROMPT = [proc { '' }, proc { '' }]
|
|
|
|
|
2011-05-20 15:50:55 +01:00
|
|
|
SHELL_PROMPT = [
|
2012-08-22 14:12:31 -05: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 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-08-22 14:12:31 -05:00
|
|
|
"[#{conf.expr_number}] (#{Pry.config.prompt_name}) #{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-08-22 14:12:31 -05:00
|
|
|
"[#{conf.expr_number}] (#{ Pry.config.prompt_name}) #{tree}: #{conf.nesting_level}* "
|
2011-09-08 15:36:24 +12:00
|
|
|
end,
|
|
|
|
]
|
2011-09-07 19:56:23 +12:00
|
|
|
|
2012-12-15 03:23:24 +02: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-28 03:25:09 +13:00
|
|
|
DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_|
|
|
|
|
if !eval_string.empty?
|
2012-12-15 03:23:24 +02:00
|
|
|
eval_string.replace('') # Clear input buffer.
|
2011-09-28 03:25:09 +13:00
|
|
|
elsif _pry_.binding_stack.one?
|
|
|
|
_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).
|
2012-12-23 00:12:07 -08:00
|
|
|
_pry_.command_state["cd"] ||= OpenStruct.new # FIXME
|
2012-12-15 03:23:24 +02:00
|
|
|
_pry_.command_state['cd'].old_stack = _pry_.binding_stack.dup
|
2012-06-27 14:54:07 +03:00
|
|
|
_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
|
|
|
|
|
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.
|
2013-09-03 11:04:25 +03:00
|
|
|
module ExtendCommandBundle; end
|
|
|
|
|
2013-12-10 12:41:31 +09:00
|
|
|
# class accessors
|
|
|
|
# define class attributes before pry library is required
|
|
|
|
# fix initialize step of Pry (#1037)
|
|
|
|
class << self
|
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
# convenience method
|
|
|
|
def self.delegate_accessors(delagatee, *names)
|
|
|
|
def_delegators delagatee, *names
|
|
|
|
def_delegators delagatee, *names.map { |v| "#{v}=" }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Get/Set the Proc that defines extra Readline completions (on top
|
|
|
|
# of the ones defined for IRB).
|
|
|
|
# @return [Proc] The Proc that defines extra Readline completions (on top
|
|
|
|
# @example Add file names to completion list
|
|
|
|
# Pry.custom_completions = proc { Dir.entries('.') }
|
|
|
|
attr_accessor :custom_completions
|
|
|
|
|
|
|
|
# @return [Fixnum] The current input line.
|
|
|
|
attr_accessor :current_line
|
|
|
|
|
|
|
|
# @return [Array] The Array of evaluated expressions.
|
|
|
|
attr_accessor :line_buffer
|
|
|
|
|
|
|
|
# @return [String] The __FILE__ for the `eval()`. Should be "(pry)"
|
|
|
|
# by default.
|
|
|
|
attr_accessor :eval_path
|
|
|
|
|
|
|
|
# @return [OpenStruct] Return Pry's config object.
|
|
|
|
attr_accessor :config
|
|
|
|
|
|
|
|
# @return [History] Return Pry's line history object.
|
|
|
|
attr_accessor :history
|
|
|
|
|
|
|
|
# @return [Boolean] Whether Pry was activated from the command line.
|
|
|
|
attr_accessor :cli
|
|
|
|
|
|
|
|
# @return [Boolean] Whether Pry sessions are quiet by default.
|
|
|
|
attr_accessor :quiet
|
|
|
|
|
|
|
|
# @return [Exception, nil] The last pry internal error.
|
|
|
|
# (a CommandError in most cases)
|
|
|
|
attr_accessor :last_internal_error
|
|
|
|
|
|
|
|
# plugin forwardables
|
|
|
|
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
|
|
|
|
|
|
|
|
delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
|
|
|
|
:hooks, :color, :pager, :editor, :memory_size, :extra_sticky_locals
|
|
|
|
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
|
2013-09-03 11:04:25 +03:00
|
|
|
require 'ruby18_source_location'
|
2012-04-18 18:18:33 +12:00
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-08-18 21:18:54 +00:00
|
|
|
require 'method_source'
|
2011-03-03 00:18:26 +13:00
|
|
|
require 'shellwords'
|
2012-08-18 21:18:54 +00:00
|
|
|
require 'stringio'
|
|
|
|
require 'coderay'
|
|
|
|
require 'slop'
|
|
|
|
require 'rbconfig'
|
2012-08-11 17:22:29 -07:00
|
|
|
require 'tempfile'
|
2013-02-17 20:43:07 -08:00
|
|
|
require 'pathname'
|
2011-03-03 00:18:26 +13:00
|
|
|
|
2012-06-04 23:26:38 -07:00
|
|
|
begin
|
2013-12-19 21:29:07 +09:00
|
|
|
require 'readline'
|
2012-06-04 23:26:38 -07:00
|
|
|
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'
|
2013-09-03 11:04:25 +03: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
|
2014-01-09 12:44:04 +09:00
|
|
|
warn "For a better pry experience, please use ansicon: https://github.com/adoxa/ansicon"
|
2011-03-03 00:18:26 +13:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-05 23:14:53 -07:00
|
|
|
begin
|
|
|
|
require 'bond'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2012-08-18 21:18:54 +00:00
|
|
|
require 'pry/version'
|
2012-12-21 00:02:13 -08:00
|
|
|
require 'pry/repl'
|
2012-08-18 21:18:54 +00:00
|
|
|
require 'pry/rbx_path'
|
|
|
|
require 'pry/code'
|
|
|
|
require 'pry/history_array'
|
|
|
|
require 'pry/helpers'
|
2012-12-25 13:47:33 +01:00
|
|
|
require 'pry/code_object'
|
|
|
|
require 'pry/method'
|
|
|
|
require 'pry/wrapped_module'
|
2012-08-18 21:18:54 +00: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'
|
2013-10-28 21:36:00 -07:00
|
|
|
require 'pry/color_printer'
|
2012-08-18 21:18:54 +00:00
|
|
|
require 'pry/pager'
|
2013-01-30 21:14:06 -06:00
|
|
|
require 'pry/terminal'
|
2012-12-29 23:34:59 +01:00
|
|
|
require 'pry/editor'
|
2013-01-09 00:24:53 +02:00
|
|
|
require 'pry/rubygem'
|