2011-05-28 23:58:27 +12:00
|
|
|
require 'pry/config'
|
2010-12-26 02:59:37 +13:00
|
|
|
class Pry
|
|
|
|
|
2013-04-02 22:53:05 -07:00
|
|
|
HOME_RC_FILE = ENV["PRYRC"] || "~/.pryrc"
|
2012-07-04 15:09:01 +12:00
|
|
|
LOCAL_RC_FILE = "./.pryrc"
|
2011-04-16 23:17:30 +02:00
|
|
|
|
2013-12-17 18:27:42 +01:00
|
|
|
class << self
|
|
|
|
extend Forwardable
|
|
|
|
attr_accessor :custom_completions
|
|
|
|
attr_accessor :current_line
|
|
|
|
attr_accessor :line_buffer
|
|
|
|
attr_accessor :eval_path
|
|
|
|
attr_accessor :cli
|
|
|
|
attr_accessor :quiet
|
|
|
|
attr_accessor :last_internal_error
|
2014-01-20 09:46:26 +01:00
|
|
|
attr_accessor :config
|
2014-02-02 19:51:39 -08:00
|
|
|
attr_writer :history
|
2013-12-17 18:27:42 +01:00
|
|
|
|
|
|
|
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
|
|
|
|
|
|
|
|
extend Pry::Config::Convenience
|
|
|
|
config_shortcut *Pry::Config.shortcuts
|
2014-01-21 09:13:26 +01:00
|
|
|
|
|
|
|
def prompt=(value)
|
|
|
|
config.prompt = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def prompt
|
|
|
|
config.prompt
|
|
|
|
end
|
2014-02-02 19:51:39 -08:00
|
|
|
|
|
|
|
def history
|
|
|
|
@history ||= History.new
|
|
|
|
end
|
2014-01-19 04:28:22 +01:00
|
|
|
end
|
2013-12-17 18:27:42 +01:00
|
|
|
|
2014-03-16 10:22:34 +01:00
|
|
|
#
|
|
|
|
# @return [main]
|
|
|
|
# returns the special instance of Object, "main".
|
|
|
|
#
|
2014-01-17 10:32:29 +01:00
|
|
|
def self.main
|
|
|
|
@main ||= TOPLEVEL_BINDING.eval "self"
|
2013-12-17 18:27:42 +01:00
|
|
|
end
|
|
|
|
|
2014-02-01 14:03:11 +01:00
|
|
|
#
|
|
|
|
# @return [Pry::Config]
|
|
|
|
# Returns a value store for an instance of Pry running on the current thread.
|
|
|
|
#
|
2013-01-15 23:29:23 +01:00
|
|
|
def self.current
|
2014-02-01 14:03:11 +01:00
|
|
|
Thread.current[:__pry__] ||= Pry::Config.from_hash({}, nil)
|
2012-11-02 14:27:30 +01:00
|
|
|
end
|
|
|
|
|
2012-07-04 15:09:01 +12:00
|
|
|
# Load the given file in the context of `Pry.toplevel_binding`
|
2014-03-16 23:54:45 +09:00
|
|
|
# @param [String] file The unexpanded file path.
|
2013-02-17 20:43:07 -08:00
|
|
|
def self.load_file_at_toplevel(file)
|
|
|
|
toplevel_binding.eval(File.read(file), file)
|
|
|
|
rescue RescuableException => e
|
|
|
|
puts "Error loading #{file}: #{e}\n#{e.backtrace.first}"
|
2012-07-04 15:09:01 +12:00
|
|
|
end
|
|
|
|
|
2013-02-17 20:43:07 -08:00
|
|
|
# Load HOME_RC_FILE and LOCAL_RC_FILE if appropriate
|
2011-10-19 05:55:31 +13:00
|
|
|
# This method can also be used to reload the files if they have changed.
|
2013-02-17 20:43:07 -08:00
|
|
|
def self.load_rc_files
|
|
|
|
rc_files_to_load.each do |file|
|
2014-01-31 16:51:38 -08:00
|
|
|
critical_section do
|
|
|
|
load_file_at_toplevel(file)
|
|
|
|
end
|
2013-02-17 20:43:07 -08:00
|
|
|
end
|
2010-12-26 02:59:37 +13:00
|
|
|
end
|
2011-04-16 23:17:30 +02:00
|
|
|
|
2012-07-04 15:09:01 +12:00
|
|
|
# Load the local RC file (./.pryrc)
|
2013-02-17 20:43:07 -08:00
|
|
|
def self.rc_files_to_load
|
|
|
|
files = []
|
|
|
|
files << HOME_RC_FILE if Pry.config.should_load_rc
|
|
|
|
files << LOCAL_RC_FILE if Pry.config.should_load_local_rc
|
|
|
|
files.map { |file| real_path_to(file) }.compact.uniq
|
|
|
|
end
|
|
|
|
|
|
|
|
# Expand a file to its canonical name (following symlinks as appropriate)
|
|
|
|
def self.real_path_to(file)
|
2013-02-17 23:21:54 -08:00
|
|
|
expanded = Pathname.new(File.expand_path(file)).realpath.to_s
|
|
|
|
# For rbx 1.9 mode [see rubinius issue #2165]
|
|
|
|
File.exist?(expanded) ? expanded : nil
|
2013-02-17 20:43:07 -08:00
|
|
|
rescue Errno::ENOENT
|
|
|
|
nil
|
2012-07-04 15:09:01 +12:00
|
|
|
end
|
|
|
|
|
2011-07-24 20:04:44 -07:00
|
|
|
# Load any Ruby files specified with the -r flag on the command line.
|
|
|
|
def self.load_requires
|
|
|
|
Pry.config.requires.each do |file|
|
|
|
|
require file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-11-22 09:06:27 -08:00
|
|
|
# Trap interrupts on jruby, and make them behave like MRI so we can
|
|
|
|
# catch them.
|
|
|
|
def self.load_traps
|
|
|
|
trap('INT'){ raise Interrupt }
|
|
|
|
end
|
|
|
|
|
2011-11-13 04:31:43 +13:00
|
|
|
# Do basic setup for initial session.
|
|
|
|
# Including: loading .pryrc, loading plugins, loading requires, and
|
|
|
|
# loading history.
|
|
|
|
def self.initial_session_setup
|
2013-03-24 23:01:44 -07:00
|
|
|
return unless initial_session?
|
|
|
|
@initial_session = false
|
2011-11-15 03:16:38 +13:00
|
|
|
|
2011-11-13 04:31:43 +13:00
|
|
|
# note these have to be loaded here rather than in pry_instance as
|
2011-11-30 01:35:45 +13:00
|
|
|
# we only want them loaded once per entire Pry lifetime.
|
2013-02-17 20:43:07 -08:00
|
|
|
load_rc_files
|
2012-01-25 01:07:50 +13:00
|
|
|
load_plugins if Pry.config.should_load_plugins
|
2011-11-13 04:31:43 +13:00
|
|
|
load_requires if Pry.config.should_load_requires
|
|
|
|
load_history if Pry.config.history.should_load
|
2011-11-22 09:06:27 -08:00
|
|
|
load_traps if Pry.config.should_trap_interrupts
|
2011-11-13 04:31:43 +13:00
|
|
|
end
|
|
|
|
|
2010-12-29 22:14:12 +13:00
|
|
|
# Start a Pry REPL.
|
2012-12-27 21:52:58 -08:00
|
|
|
# This method also loads `~/.pryrc` and `./.pryrc` as necessary the
|
2011-03-05 01:37:59 +13:00
|
|
|
# first time it is invoked.
|
2010-12-29 22:14:12 +13:00
|
|
|
# @param [Object, Binding] target The receiver of the Pry session
|
2010-12-31 04:01:11 +13:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options (see Pry#initialize)
|
2010-12-29 22:14:12 +13:00
|
|
|
# @example
|
|
|
|
# Pry.start(Object.new, :input => MyInput.new)
|
2012-11-07 23:09:31 -08:00
|
|
|
def self.start(target=nil, options={})
|
2012-10-31 01:42:55 -07:00
|
|
|
return if ENV['DISABLE_PRY']
|
2014-01-28 15:20:26 +01:00
|
|
|
options = options.to_hash
|
2012-11-13 21:59:59 -08:00
|
|
|
|
|
|
|
if in_critical_section?
|
|
|
|
output.puts "ERROR: Pry started inside Pry."
|
|
|
|
output.puts "This can happen if you have a binding.pry inside a #to_s or #inspect function."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2012-12-18 01:12:57 -08:00
|
|
|
options[:target] = Pry.binding_for(target || toplevel_binding)
|
2014-01-21 09:26:50 +01:00
|
|
|
options[:hooks] = Pry::Hooks.from_hash options.delete(:hooks) if options.key?(:hooks)
|
2011-11-15 03:16:38 +13:00
|
|
|
initial_session_setup
|
2011-11-30 01:35:45 +13:00
|
|
|
|
2012-12-27 21:52:58 -08:00
|
|
|
# Unless we were given a backtrace, save the current one
|
2012-12-19 23:05:32 -08:00
|
|
|
if options[:backtrace].nil?
|
|
|
|
options[:backtrace] = caller
|
2012-01-20 17:39:25 +13:00
|
|
|
|
2012-12-27 21:52:58 -08:00
|
|
|
# If Pry was started via `binding.pry`, elide that from the backtrace
|
2012-12-19 23:05:32 -08:00
|
|
|
if options[:backtrace].first =~ /pry.*core_extensions.*pry/
|
2012-12-20 23:10:17 -08:00
|
|
|
options[:backtrace].shift
|
2012-12-19 23:05:32 -08:00
|
|
|
end
|
2012-01-18 17:20:13 +13:00
|
|
|
end
|
2011-11-30 01:35:45 +13:00
|
|
|
|
2012-12-21 00:02:13 -08:00
|
|
|
driver = options[:driver] || Pry::REPL
|
2012-05-25 16:49:50 +02:00
|
|
|
|
2011-11-30 01:35:45 +13:00
|
|
|
# Enter the matrix
|
2012-12-21 00:02:13 -08:00
|
|
|
driver.start(options)
|
2012-11-07 23:21:52 -08:00
|
|
|
rescue Pry::TooSafeException
|
2012-11-07 23:09:31 -08:00
|
|
|
puts "ERROR: Pry cannot work with $SAFE > 0"
|
|
|
|
raise
|
2010-12-26 02:59:37 +13:00
|
|
|
end
|
|
|
|
|
2012-06-17 22:50:18 +12:00
|
|
|
# Execute the file through the REPL loop, non-interactively.
|
|
|
|
# @param [String] file_name File name to load through the REPL.
|
|
|
|
def self.load_file_through_repl(file_name)
|
|
|
|
require "pry/repl_file_loader"
|
|
|
|
REPLFileLoader.new(file_name).load
|
|
|
|
end
|
|
|
|
|
2014-03-16 06:58:28 +01:00
|
|
|
#
|
2011-09-15 21:08:05 +12:00
|
|
|
# An inspector that clips the output to `max_length` chars.
|
2011-09-09 01:35:21 +03:00
|
|
|
# In case of > `max_length` chars the `#<Object...> notation is used.
|
2014-03-16 06:58:28 +01:00
|
|
|
#
|
2014-03-16 23:54:45 +09:00
|
|
|
# @param [Object] obj
|
2014-03-16 09:49:46 +01:00
|
|
|
# The object to view.
|
2014-03-16 06:58:28 +01:00
|
|
|
#
|
2014-03-16 09:49:46 +01:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options [Integer] :max_length (60)
|
|
|
|
# The maximum number of chars before clipping occurs.
|
|
|
|
#
|
|
|
|
# @option options [Boolean] :id (false)
|
|
|
|
# Boolean to indicate whether or not a hex reprsentation of the object ID
|
|
|
|
# is attached to the return value when the length of inspect is greater than
|
|
|
|
# value of `:max_length`.
|
2014-03-16 06:58:28 +01:00
|
|
|
#
|
|
|
|
# @return [String]
|
2014-03-16 09:49:46 +01:00
|
|
|
# The string representation of `obj`.
|
2014-03-16 06:58:28 +01:00
|
|
|
#
|
|
|
|
def self.view_clip(obj, options = {})
|
|
|
|
max = options.fetch :max_length, 60
|
|
|
|
id = options.fetch :id, false
|
|
|
|
if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max
|
2011-09-04 18:38:01 +08:00
|
|
|
obj.name.to_s
|
2014-01-17 10:32:29 +01:00
|
|
|
elsif Pry.main == obj
|
2014-01-17 10:27:32 +01:00
|
|
|
# special-case to support jruby.
|
|
|
|
# fixed as of https://github.com/jruby/jruby/commit/d365ebd309cf9df3dde28f5eb36ea97056e0c039
|
|
|
|
# we can drop in the future.
|
2011-09-08 23:12:26 -07:00
|
|
|
obj.to_s
|
2014-03-16 06:58:28 +01:00
|
|
|
elsif Pry.config.prompt_safe_objects.any? { |v| v === obj } && obj.inspect.length <= max
|
2011-09-08 15:36:24 +12:00
|
|
|
obj.inspect
|
2011-09-08 03:39:22 +12:00
|
|
|
else
|
2014-03-16 06:58:28 +01:00
|
|
|
id == true ? "#<#{obj.class}:0x%x>" % (obj.object_id << 1) : "#<#{obj.class}>"
|
2011-02-14 04:49:53 +13:00
|
|
|
end
|
2011-09-08 15:36:24 +12:00
|
|
|
rescue RescuableException
|
2011-09-04 18:38:01 +08:00
|
|
|
"unknown"
|
2011-02-14 04:49:53 +13:00
|
|
|
end
|
|
|
|
|
2011-05-28 23:58:27 +12:00
|
|
|
# Load Readline history if required.
|
|
|
|
def self.load_history
|
2011-12-01 21:03:36 -08:00
|
|
|
Pry.history.load
|
2011-05-28 23:58:27 +12:00
|
|
|
end
|
|
|
|
|
|
|
|
# @return [Boolean] Whether this is the first time a Pry session has
|
|
|
|
# been started since loading the Pry class.
|
|
|
|
def self.initial_session?
|
|
|
|
@initial_session
|
|
|
|
end
|
|
|
|
|
2011-02-19 06:01:21 +13:00
|
|
|
# Run a Pry command from outside a session. The commands available are
|
|
|
|
# those referenced by `Pry.commands` (the default command set).
|
2012-06-26 22:30:00 -07:00
|
|
|
# @param [String] command_string The Pry command (including arguments,
|
2011-02-19 06:01:21 +13:00
|
|
|
# if any).
|
|
|
|
# @param [Hash] options Optional named parameters.
|
2011-02-19 07:01:02 +13:00
|
|
|
# @return [Object] The return value of the Pry command.
|
2013-02-05 10:34:28 -08:00
|
|
|
# @option options [Object, Binding] :target The object to run the
|
2011-02-19 06:01:21 +13:00
|
|
|
# command under. Defaults to `TOPLEVEL_BINDING` (main).
|
|
|
|
# @option options [Boolean] :show_output Whether to show command
|
2011-05-28 04:08:08 +12:00
|
|
|
# output. Defaults to true.
|
2011-02-19 06:01:21 +13:00
|
|
|
# @example Run at top-level with no output.
|
|
|
|
# Pry.run_command "ls"
|
|
|
|
# @example Run under Pry class, returning only public methods.
|
2013-02-05 10:34:28 -08:00
|
|
|
# Pry.run_command "ls -m", :target => Pry
|
2011-02-19 06:01:21 +13:00
|
|
|
# @example Display command output.
|
|
|
|
# Pry.run_command "ls -av", :show_output => true
|
2011-05-28 04:08:08 +12:00
|
|
|
def self.run_command(command_string, options={})
|
2011-02-19 06:01:21 +13:00
|
|
|
options = {
|
2013-02-05 10:34:28 -08:00
|
|
|
:target => TOPLEVEL_BINDING,
|
2011-05-28 04:08:08 +12:00
|
|
|
:show_output => true,
|
2011-03-06 03:17:54 +13:00
|
|
|
:output => Pry.output,
|
|
|
|
:commands => Pry.commands
|
2011-02-19 06:01:21 +13:00
|
|
|
}.merge!(options)
|
2011-04-16 23:17:30 +02:00
|
|
|
|
2013-02-05 10:34:28 -08:00
|
|
|
# :context for compatibility with <= 0.9.11.4
|
|
|
|
target = options[:context] || options[:target]
|
2011-05-28 04:08:08 +12:00
|
|
|
output = options[:show_output] ? options[:output] : StringIO.new
|
2011-02-19 06:01:21 +13:00
|
|
|
|
2013-02-05 10:34:28 -08:00
|
|
|
pry = Pry.new(:output => output, :target => target, :commands => options[:commands])
|
|
|
|
pry.eval command_string
|
2011-02-19 06:01:21 +13:00
|
|
|
end
|
|
|
|
|
2011-04-26 04:17:20 +12:00
|
|
|
def self.default_editor_for_platform
|
2011-12-26 18:28:48 -06:00
|
|
|
return ENV['VISUAL'] if ENV['VISUAL'] and not ENV['VISUAL'].empty?
|
|
|
|
return ENV['EDITOR'] if ENV['EDITOR'] and not ENV['EDITOR'].empty?
|
2011-12-26 18:48:59 -06:00
|
|
|
if Helpers::BaseHelpers.windows?
|
2011-12-26 18:28:48 -06:00
|
|
|
'notepad'
|
2011-04-26 04:17:20 +12:00
|
|
|
else
|
2011-12-27 22:59:08 +00:00
|
|
|
%w(editor nano vi).detect do |editor|
|
|
|
|
system("which #{editor} > /dev/null 2>&1")
|
2011-12-26 15:39:39 -06:00
|
|
|
end
|
2011-04-26 04:17:20 +12:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-10-27 08:51:51 +00:00
|
|
|
def self.auto_resize!
|
2014-02-02 17:36:06 -08:00
|
|
|
Pry.config.input # by default, load Readline
|
|
|
|
|
|
|
|
if !defined?(Readline) || Pry.config.input != Readline
|
|
|
|
warn "Sorry, you must be using Readline for Pry.auto_resize! to work."
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if Readline::VERSION =~ /edit/i
|
2013-02-08 00:41:37 -06:00
|
|
|
warn <<-EOT
|
2014-02-02 17:36:06 -08:00
|
|
|
Readline version #{Readline::VERSION} detected - will not auto_resize! correctly.
|
2013-02-08 00:41:37 -06:00
|
|
|
For the fix, use GNU Readline instead:
|
|
|
|
https://github.com/guard/guard/wiki/Add-proper-Readline-support-to-Ruby-on-Mac-OS-X
|
|
|
|
EOT
|
|
|
|
return
|
|
|
|
end
|
2014-02-02 17:36:06 -08:00
|
|
|
|
2012-10-27 08:51:51 +00:00
|
|
|
trap :WINCH do
|
2013-01-30 21:14:06 -06:00
|
|
|
begin
|
2013-03-02 16:01:55 -08:00
|
|
|
Readline.set_screen_size(*Terminal.size!)
|
2013-01-30 21:14:06 -06:00
|
|
|
rescue => e
|
|
|
|
warn "\nPry.auto_resize!'s Readline.set_screen_size failed: #{e}"
|
|
|
|
end
|
|
|
|
begin
|
|
|
|
Readline.refresh_line
|
|
|
|
rescue => e
|
|
|
|
warn "\nPry.auto_resize!'s Readline.refresh_line failed: #{e}"
|
|
|
|
end
|
2012-10-27 08:51:51 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-23 22:29:02 +12:00
|
|
|
# Set all the configurable options back to their default values
|
|
|
|
def self.reset_defaults
|
2011-05-28 23:58:27 +12:00
|
|
|
@initial_session = true
|
2014-01-20 09:46:26 +01:00
|
|
|
self.config = Pry::Config.new Pry::Config::Default.new
|
2011-05-28 23:58:27 +12:00
|
|
|
self.cli = false
|
2011-08-22 21:19:20 +12:00
|
|
|
self.current_line = 1
|
|
|
|
self.line_buffer = [""]
|
2011-05-28 23:58:27 +12:00
|
|
|
self.eval_path = "(pry)"
|
2010-12-26 02:59:37 +13:00
|
|
|
end
|
|
|
|
|
2011-05-19 15:31:21 +12:00
|
|
|
# Basic initialization.
|
|
|
|
def self.init
|
2011-05-20 18:39:26 +12:00
|
|
|
@plugin_manager ||= PluginManager.new
|
2011-05-19 15:31:21 +12:00
|
|
|
reset_defaults
|
|
|
|
locate_plugins
|
|
|
|
end
|
2010-12-26 02:59:37 +13:00
|
|
|
|
2011-02-17 05:27:55 +13:00
|
|
|
# Return a `Binding` object for `target` or return `target` if it is
|
|
|
|
# already a `Binding`.
|
|
|
|
# In the case where `target` is top-level then return `TOPLEVEL_BINDING`
|
|
|
|
# @param [Object] target The object to get a `Binding` object for.
|
|
|
|
# @return [Binding] The `Binding` object.
|
|
|
|
def self.binding_for(target)
|
2012-04-05 14:01:45 -05:00
|
|
|
if Binding === target
|
2011-02-17 05:27:55 +13:00
|
|
|
target
|
|
|
|
else
|
2014-01-17 10:39:13 +01:00
|
|
|
if Pry.main == target
|
2011-02-17 05:27:55 +13:00
|
|
|
TOPLEVEL_BINDING
|
|
|
|
else
|
|
|
|
target.__binding__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-05-19 15:31:21 +12:00
|
|
|
|
2012-10-13 21:00:36 +00:00
|
|
|
def self.toplevel_binding
|
2013-03-02 16:01:55 -08:00
|
|
|
unless defined?(@toplevel_binding) && @toplevel_binding
|
2012-10-13 21:00:36 +00:00
|
|
|
# Grab a copy of the TOPLEVEL_BINDING without any local variables.
|
|
|
|
# This binding has a default definee of Object, and new methods are
|
|
|
|
# private (just as in TOPLEVEL_BINDING).
|
|
|
|
TOPLEVEL_BINDING.eval <<-RUBY
|
|
|
|
def self.__pry__
|
|
|
|
binding
|
|
|
|
end
|
|
|
|
Pry.toplevel_binding = __pry__
|
|
|
|
class << self; undef __pry__; end
|
|
|
|
RUBY
|
|
|
|
end
|
|
|
|
@toplevel_binding.eval('private')
|
|
|
|
@toplevel_binding
|
|
|
|
end
|
2012-11-13 21:59:59 -08:00
|
|
|
|
2013-03-02 16:01:55 -08:00
|
|
|
def self.toplevel_binding=(binding)
|
|
|
|
@toplevel_binding = binding
|
|
|
|
end
|
|
|
|
|
2012-11-13 21:59:59 -08:00
|
|
|
def self.in_critical_section?
|
2013-06-26 16:33:09 -04:00
|
|
|
Thread.current[:pry_critical_section] ||= 0
|
|
|
|
Thread.current[:pry_critical_section] > 0
|
2012-11-13 21:59:59 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.critical_section(&block)
|
2013-06-26 16:33:09 -04:00
|
|
|
Thread.current[:pry_critical_section] ||= 0
|
|
|
|
Thread.current[:pry_critical_section] += 1
|
2012-11-13 21:59:59 -08:00
|
|
|
yield
|
|
|
|
ensure
|
2013-06-26 16:33:09 -04:00
|
|
|
Thread.current[:pry_critical_section] -= 1
|
2012-11-13 21:59:59 -08:00
|
|
|
end
|
2012-06-10 22:57:40 -07:00
|
|
|
end
|
|
|
|
|
2011-05-19 15:31:21 +12:00
|
|
|
Pry.init
|