2011-05-20 02:39:26 -04:00
|
|
|
require 'ostruct'
|
2011-05-18 23:31:21 -04:00
|
|
|
require 'forwardable'
|
2011-05-28 07:58:27 -04:00
|
|
|
require 'pry/config'
|
2011-05-18 23:31:21 -04:00
|
|
|
|
2010-12-25 08:59:37 -05:00
|
|
|
class Pry
|
|
|
|
|
2011-04-16 17:17:30 -04:00
|
|
|
# The RC Files to load.
|
2011-09-13 10:28:24 -04:00
|
|
|
RC_FILES = ["~/.pryrc", "./.pryrc"]
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2010-12-25 08:59:37 -05:00
|
|
|
# class accessors
|
|
|
|
class << self
|
2011-05-18 23:31:21 -04:00
|
|
|
extend Forwardable
|
2010-12-29 04:14:12 -05:00
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
# convenience method
|
|
|
|
def self.delegate_accessors(delagatee, *names)
|
|
|
|
def_delegators delagatee, *names
|
|
|
|
def_delegators delagatee, *names.map { |v| "#{v}=" }
|
|
|
|
end
|
|
|
|
|
2011-04-07 04:38:50 -04:00
|
|
|
# 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('.') }
|
2011-04-06 12:59:09 -04:00
|
|
|
attr_accessor :custom_completions
|
|
|
|
|
2011-05-24 04:16:46 -04:00
|
|
|
# @return [Fixnum] The current input line.
|
2011-05-24 08:34:55 -04:00
|
|
|
attr_accessor :current_line
|
2011-05-24 04:16:46 -04:00
|
|
|
|
|
|
|
# @return [Array] The Array of evaluated expressions.
|
2011-05-24 08:34:55 -04:00
|
|
|
attr_accessor :line_buffer
|
2011-05-24 04:16:46 -04:00
|
|
|
|
2011-05-24 20:48:53 -04:00
|
|
|
# @return [String] The __FILE__ for the `eval()`. Should be "(pry)"
|
|
|
|
# by default.
|
|
|
|
attr_accessor :eval_path
|
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
# @return [OpenStruct] Return Pry's config object.
|
|
|
|
attr_accessor :config
|
|
|
|
|
2011-09-05 16:52:49 -04:00
|
|
|
# @return [History] Return Pry's line history object.
|
2011-09-05 04:46:16 -04:00
|
|
|
attr_accessor :history
|
2011-09-05 04:45:58 -04:00
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
# @return [Boolean] Whether Pry was activated from the command line.
|
|
|
|
attr_accessor :cli
|
|
|
|
|
2011-08-25 03:32:09 -04:00
|
|
|
# @return [Fixnum] The number of active Pry sessions.
|
|
|
|
attr_accessor :active_sessions
|
|
|
|
|
2011-05-20 02:39:26 -04:00
|
|
|
# plugin forwardables
|
2011-05-18 23:31:21 -04:00
|
|
|
def_delegators :@plugin_manager, :plugins, :load_plugins, :locate_plugins
|
2011-05-20 02:39:26 -04:00
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
delegate_accessors :@config, :input, :output, :commands, :prompt, :print, :exception_handler,
|
2011-09-17 08:41:30 -04:00
|
|
|
:hooks, :color, :pager, :editor, :memory_size, :input_stack
|
2011-05-15 05:58:27 -04:00
|
|
|
end
|
|
|
|
|
2011-03-04 07:37:59 -05:00
|
|
|
# Load the rc files given in the `Pry::RC_FILES` array.
|
2011-10-18 12:55:31 -04:00
|
|
|
# This method can also be used to reload the files if they have changed.
|
2011-03-04 07:37:59 -05:00
|
|
|
def self.load_rc
|
2011-10-18 12:55:31 -04:00
|
|
|
files = RC_FILES.collect { |file_name| File.expand_path(file_name) }.uniq
|
|
|
|
files.each do |file_name|
|
2011-11-02 03:45:54 -04:00
|
|
|
begin
|
|
|
|
load(file_name) if File.exists?(file_name)
|
|
|
|
rescue RescuableException => e
|
|
|
|
puts "Error loading #{file_name}: #{e}"
|
|
|
|
end
|
2011-03-04 07:37:59 -05:00
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2011-07-24 23:04:44 -04: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-12 10:31:43 -05:00
|
|
|
# Do basic setup for initial session.
|
|
|
|
# Including: loading .pryrc, loading plugins, loading requires, and
|
|
|
|
# loading history.
|
|
|
|
def self.initial_session_setup
|
|
|
|
# note these have to be loaded here rather than in pry_instance as
|
|
|
|
# we only want them loaded once per entire Pry lifetime, not
|
|
|
|
# multiple times per each new session (i.e in debugging)
|
|
|
|
load_rc if Pry.config.should_load_rc
|
|
|
|
load_plugins if Pry.config.plugins.enabled
|
|
|
|
load_requires if Pry.config.should_load_requires
|
|
|
|
load_history if Pry.config.history.should_load
|
|
|
|
|
|
|
|
@initial_session = false
|
|
|
|
end
|
|
|
|
|
2010-12-29 04:14:12 -05:00
|
|
|
# Start a Pry REPL.
|
2011-03-04 07:37:59 -05:00
|
|
|
# This method also loads the files specified in `Pry::RC_FILES` the
|
|
|
|
# first time it is invoked.
|
2010-12-29 04:14:12 -05:00
|
|
|
# @param [Object, Binding] target The receiver of the Pry session
|
2010-12-30 10:01:11 -05:00
|
|
|
# @param [Hash] options
|
|
|
|
# @option options (see Pry#initialize)
|
2010-12-29 04:14:12 -05:00
|
|
|
# @example
|
|
|
|
# Pry.start(Object.new, :input => MyInput.new)
|
2010-12-25 08:59:37 -05:00
|
|
|
def self.start(target=TOPLEVEL_BINDING, options={})
|
2011-11-13 05:25:39 -05:00
|
|
|
target = Pry.binding_for(target)
|
2011-05-28 07:58:27 -04:00
|
|
|
if initial_session?
|
2011-11-12 10:31:43 -05:00
|
|
|
initial_session_setup
|
2011-03-04 07:37:59 -05:00
|
|
|
end
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2011-11-12 10:31:43 -05:00
|
|
|
Pry.config.hooks.exec_hook(:when_started, target)
|
2010-12-25 08:59:37 -05:00
|
|
|
new(options).repl(target)
|
|
|
|
end
|
|
|
|
|
2011-09-15 05:08:05 -04:00
|
|
|
# An inspector that clips the output to `max_length` chars.
|
2011-09-08 18:35:21 -04:00
|
|
|
# In case of > `max_length` chars the `#<Object...> notation is used.
|
2011-02-13 10:49:53 -05:00
|
|
|
# @param obj The object to view.
|
2011-09-08 18:35:21 -04:00
|
|
|
# @param max_length The maximum number of chars before clipping occurs.
|
2011-02-13 10:49:53 -05:00
|
|
|
# @return [String] The string representation of `obj`.
|
2011-09-04 06:38:01 -04:00
|
|
|
def self.view_clip(obj, max_length = 60)
|
2011-09-07 15:11:11 -04:00
|
|
|
if obj.kind_of?(Module) && obj.name.to_s != "" && obj.name.to_s.length <= max_length
|
2011-09-04 06:38:01 -04:00
|
|
|
obj.name.to_s
|
2011-09-07 15:11:11 -04:00
|
|
|
elsif TOPLEVEL_BINDING.eval('self') == obj
|
|
|
|
# special case for 'main' object :)
|
2011-09-09 02:12:26 -04:00
|
|
|
obj.to_s
|
2011-09-08 02:30:23 -04:00
|
|
|
elsif [String, Numeric, Symbol, nil, true, false].any? { |v| v === obj } && obj.inspect.length <= max_length
|
2011-09-07 23:36:24 -04:00
|
|
|
obj.inspect
|
2011-09-07 11:39:22 -04:00
|
|
|
else
|
2011-09-08 02:30:23 -04:00
|
|
|
"#<#{obj.class}>"#:%x>"# % (obj.object_id << 1)
|
2011-02-13 10:49:53 -05:00
|
|
|
end
|
2011-09-04 06:38:01 -04:00
|
|
|
|
2011-09-07 23:36:24 -04:00
|
|
|
rescue RescuableException
|
2011-09-04 06:38:01 -04:00
|
|
|
"unknown"
|
2011-02-13 10:49:53 -05:00
|
|
|
end
|
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
# Load Readline history if required.
|
|
|
|
def self.load_history
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.load(history_file) if File.exists?(history_file)
|
2011-05-28 07:58:27 -04:00
|
|
|
end
|
|
|
|
|
2011-08-07 06:16:54 -04:00
|
|
|
# Save new lines of Readline history if required.
|
2011-08-07 05:30:08 -04:00
|
|
|
def self.save_history
|
2011-09-05 04:46:16 -04:00
|
|
|
Pry.history.save(history_file)
|
2011-08-07 05:30:08 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get the full path of the history_path for pry.
|
|
|
|
def self.history_file
|
|
|
|
File.expand_path(Pry.config.history.file)
|
|
|
|
end
|
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
# @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-18 12:01:21 -05:00
|
|
|
# Run a Pry command from outside a session. The commands available are
|
|
|
|
# those referenced by `Pry.commands` (the default command set).
|
|
|
|
# @param [String] arg_string The Pry command (including arguments,
|
|
|
|
# if any).
|
|
|
|
# @param [Hash] options Optional named parameters.
|
2011-02-18 13:01:02 -05:00
|
|
|
# @return [Object] The return value of the Pry command.
|
2011-02-18 12:01:21 -05:00
|
|
|
# @option options [Object, Binding] :context The object context to run the
|
|
|
|
# command under. Defaults to `TOPLEVEL_BINDING` (main).
|
|
|
|
# @option options [Boolean] :show_output Whether to show command
|
2011-05-27 12:08:08 -04:00
|
|
|
# output. Defaults to true.
|
2011-02-18 12:01:21 -05:00
|
|
|
# @example Run at top-level with no output.
|
|
|
|
# Pry.run_command "ls"
|
|
|
|
# @example Run under Pry class, returning only public methods.
|
|
|
|
# Pry.run_command "ls -m", :context => Pry
|
|
|
|
# @example Display command output.
|
|
|
|
# Pry.run_command "ls -av", :show_output => true
|
2011-05-27 12:08:08 -04:00
|
|
|
def self.run_command(command_string, options={})
|
2011-02-18 12:01:21 -05:00
|
|
|
options = {
|
|
|
|
:context => TOPLEVEL_BINDING,
|
2011-05-27 12:08:08 -04:00
|
|
|
:show_output => true,
|
2011-03-05 09:17:54 -05:00
|
|
|
:output => Pry.output,
|
|
|
|
:commands => Pry.commands
|
2011-02-18 12:01:21 -05:00
|
|
|
}.merge!(options)
|
2011-04-16 17:17:30 -04:00
|
|
|
|
2011-05-27 12:08:08 -04:00
|
|
|
output = options[:show_output] ? options[:output] : StringIO.new
|
2011-02-18 12:01:21 -05:00
|
|
|
|
2011-11-12 08:03:02 -05:00
|
|
|
Pry.new(:output => output, :input => StringIO.new(command_string), :commands => options[:commands], :prompt => proc {""}, :hooks => Pry::Hooks.new).rep(options[:context])
|
2011-02-18 12:01:21 -05:00
|
|
|
end
|
|
|
|
|
2011-04-25 12:17:20 -04:00
|
|
|
def self.default_editor_for_platform
|
|
|
|
if RUBY_PLATFORM =~ /mswin|mingw/
|
2011-08-22 13:45:24 -04:00
|
|
|
ENV['VISUAL'] || ENV['EDITOR'] || "notepad"
|
2011-04-25 12:17:20 -04:00
|
|
|
else
|
2011-08-22 13:45:24 -04:00
|
|
|
ENV['VISUAL'] || ENV['EDITOR'] || "nano"
|
2011-04-25 12:17:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-24 04:16:46 -04:00
|
|
|
def self.set_config_defaults
|
2011-05-20 02:39:26 -04:00
|
|
|
config.input = Readline
|
|
|
|
config.output = $stdout
|
|
|
|
config.commands = Pry::Commands
|
|
|
|
config.prompt = DEFAULT_PROMPT
|
|
|
|
config.print = DEFAULT_PRINT
|
|
|
|
config.exception_handler = DEFAULT_EXCEPTION_HANDLER
|
2011-09-12 13:07:05 -04:00
|
|
|
config.exception_window_size = 5
|
2011-09-17 21:45:46 -04:00
|
|
|
config.exception_whitelist = DEFAULT_EXCEPTION_WHITELIST
|
2011-05-20 02:39:26 -04:00
|
|
|
config.hooks = DEFAULT_HOOKS
|
2011-09-15 05:08:05 -04:00
|
|
|
config.input_stack = []
|
2011-10-09 15:03:24 -04:00
|
|
|
config.color = Pry::Helpers::BaseHelpers.use_ansi_codes?
|
2011-05-20 02:39:26 -04:00
|
|
|
config.pager = true
|
2011-09-17 02:35:17 -04:00
|
|
|
config.system = DEFAULT_SYSTEM
|
2011-05-20 02:39:26 -04:00
|
|
|
config.editor = default_editor_for_platform
|
2011-05-28 07:58:27 -04:00
|
|
|
config.should_load_rc = true
|
2011-07-25 01:57:40 -04:00
|
|
|
config.disable_auto_reload = false
|
2011-07-26 03:34:54 -04:00
|
|
|
config.command_prefix = ""
|
2011-10-08 07:40:55 -04:00
|
|
|
config.auto_indent = true
|
2011-10-28 02:57:01 -04:00
|
|
|
config.correct_indent = true
|
2011-06-02 05:41:41 -04:00
|
|
|
|
|
|
|
config.plugins ||= OpenStruct.new
|
|
|
|
config.plugins.enabled = true
|
2011-05-28 07:58:27 -04:00
|
|
|
|
2011-07-24 23:04:44 -04:00
|
|
|
config.requires ||= []
|
|
|
|
config.should_load_requires = true
|
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
config.history ||= OpenStruct.new
|
2011-06-09 06:25:06 -04:00
|
|
|
config.history.should_save = true
|
|
|
|
config.history.should_load = true
|
2011-05-28 07:58:27 -04:00
|
|
|
config.history.file = File.expand_path("~/.pry_history")
|
2011-06-05 12:37:54 -04:00
|
|
|
|
2011-08-29 10:21:51 -04:00
|
|
|
config.control_d_handler = DEFAULT_CONTROL_D_HANDLER
|
|
|
|
|
2011-06-05 12:37:54 -04:00
|
|
|
config.memory_size = 100
|
2011-09-07 11:39:22 -04:00
|
|
|
|
2011-10-08 19:47:21 -04:00
|
|
|
config.ls ||= OpenStruct.new({
|
2011-10-16 21:11:32 -04:00
|
|
|
:heading_color => :default,
|
2011-10-08 19:47:21 -04:00
|
|
|
|
2011-10-10 00:41:33 -04:00
|
|
|
:public_method_color => :default,
|
2011-10-08 19:47:21 -04:00
|
|
|
:private_method_color => :green,
|
|
|
|
:protected_method_color => :yellow,
|
|
|
|
:method_missing_color => :bright_red,
|
|
|
|
|
2011-10-10 00:41:33 -04:00
|
|
|
:local_var_color => :default,
|
2011-10-08 19:47:21 -04:00
|
|
|
:pry_var_color => :red, # e.g. _, _pry_, _file_
|
|
|
|
|
|
|
|
:instance_var_color => :blue, # e.g. @foo
|
|
|
|
:class_var_color => :bright_blue, # e.g. @@foo
|
|
|
|
|
2011-10-10 00:41:33 -04:00
|
|
|
:global_var_color => :default, # e.g. $CODERAY_DEBUG, $eventmachine_library
|
2011-10-08 19:47:21 -04:00
|
|
|
:builtin_global_color => :cyan, # e.g. $stdin, $-w, $PID
|
|
|
|
:pseudo_global_color => :cyan, # e.g. $~, $1..$9, $LAST_MATCH_INFO
|
|
|
|
|
2011-10-10 00:41:33 -04:00
|
|
|
:constant_color => :default, # e.g. VERSION, ARGF
|
2011-10-08 19:47:21 -04:00
|
|
|
:class_constant_color => :blue, # e.g. Object, Kernel
|
|
|
|
:exception_constant_color => :magenta, # e.g. Exception, RuntimeError
|
|
|
|
|
|
|
|
# What should separate items listed by ls? (TODO: we should allow a columnar layout)
|
|
|
|
:separator => " ",
|
|
|
|
|
|
|
|
# Any methods defined on these classes, or modules included into these classes, will not
|
|
|
|
# be shown by ls unless the -v flag is used.
|
|
|
|
# A user of Rails may wih to add ActiveRecord::Base to the list.
|
|
|
|
# add the following to your .pryrc:
|
|
|
|
# Pry.config.ls.ceiling << ActiveRecord::Base if defined? ActiveRecordBase
|
|
|
|
:ceiling => [Object, Module, Class]
|
|
|
|
})
|
2011-05-23 06:29:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Set all the configurable options back to their default values
|
|
|
|
def self.reset_defaults
|
2011-05-24 04:16:46 -04:00
|
|
|
set_config_defaults
|
2011-05-20 02:39:26 -04:00
|
|
|
|
2011-05-28 07:58:27 -04:00
|
|
|
@initial_session = true
|
|
|
|
|
|
|
|
self.custom_completions = DEFAULT_CUSTOM_COMPLETIONS
|
|
|
|
self.cli = false
|
2011-08-22 05:19:20 -04:00
|
|
|
self.current_line = 1
|
|
|
|
self.line_buffer = [""]
|
2011-05-28 07:58:27 -04:00
|
|
|
self.eval_path = "(pry)"
|
2011-08-25 03:32:09 -04:00
|
|
|
self.active_sessions = 0
|
2011-10-19 01:49:11 -04:00
|
|
|
|
|
|
|
fix_coderay_colors
|
|
|
|
end
|
|
|
|
|
|
|
|
# To avoid mass-confusion, we change the default colour of "white" to
|
2011-10-25 01:17:27 -04:00
|
|
|
# "blue" enabling global legibility
|
2011-10-19 01:49:11 -04:00
|
|
|
def self.fix_coderay_colors
|
|
|
|
to_fix = if (CodeRay::Encoders::Terminal::TOKEN_COLORS rescue nil)
|
|
|
|
# CodeRay 1.0.0
|
|
|
|
CodeRay::Encoders::Terminal::TOKEN_COLORS
|
|
|
|
else
|
|
|
|
# CodeRay 0.9
|
|
|
|
begin
|
|
|
|
require 'coderay/encoders/term'
|
|
|
|
CodeRay::Encoders::Term::TOKEN_COLORS
|
|
|
|
rescue => e
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-10-25 01:17:27 -04:00
|
|
|
to_fix[:comment] = "0;34" if to_fix
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
|
|
|
|
2011-05-18 23:31:21 -04:00
|
|
|
# Basic initialization.
|
|
|
|
def self.init
|
2011-05-20 02:39:26 -04:00
|
|
|
@plugin_manager ||= PluginManager.new
|
2011-05-28 07:58:27 -04:00
|
|
|
self.config ||= Config.new
|
2011-09-05 04:46:16 -04:00
|
|
|
self.history ||= History.new
|
2011-09-05 04:45:58 -04:00
|
|
|
|
2011-05-18 23:31:21 -04:00
|
|
|
reset_defaults
|
|
|
|
locate_plugins
|
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
|
2011-02-16 11:27:55 -05: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)
|
|
|
|
if target.is_a?(Binding)
|
|
|
|
target
|
|
|
|
else
|
2011-07-16 12:40:44 -04:00
|
|
|
if TOPLEVEL_BINDING.eval('self') == target
|
2011-02-16 11:27:55 -05:00
|
|
|
TOPLEVEL_BINDING
|
|
|
|
else
|
|
|
|
target.__binding__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-12-25 08:59:37 -05:00
|
|
|
end
|
2011-05-18 23:31:21 -04:00
|
|
|
|
|
|
|
Pry.init
|