1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00
pry--pry/lib/pry.rb

275 lines
8.8 KiB
Ruby
Raw Normal View History

# (C) John Mair (banisterfiend) 2011
# MIT License
2011-10-08 17:12:25 -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'
class Pry
# The default hooks - display messages when beginning and ending Pry sessions.
DEFAULT_HOOKS = Pry::Hooks.new.add_hook(:before_session, :default) do |out, target, _pry_|
next if _pry_.quiet?
_pry_.run_command("whereami --quiet")
2011-10-16 21:09:12 -04:00
end
# The default print
DEFAULT_PRINT = proc do |output, value|
2012-11-05 02:30:45 -05:00
output_with_default_format(output, value, :hashrocket => true)
end
2012-11-05 02:30:45 -05:00
def self.output_with_default_format(output, value, options = {})
stringified = begin
value.pretty_inspect
2011-09-15 05:43:58 -04:00
rescue RescuableException
nil
end
unless String === stringified
# Read the class name off of the singleton class to provide a default
# inspect.
eig = class << value; self; end
klass = Pry::Method.safe_send(eig, :ancestors).first
2013-01-25 02:57:38 -05:00
id = value.__id__.to_s(16) rescue 0
stringified = "#<#{klass}:0x#{id}>"
end
nonce = rand(0x100000000).to_s(16) # whatever
stringified.gsub!(/#</, "%<#{nonce}")
# Don't recolorize output with color (for cucumber, looksee, etc.) [Issue #751]
colorized = if stringified =~ /\e\[/
stringified
else
Helpers::BaseHelpers.colorize_code(stringified)
end
# avoid colour-leak from CodeRay and any of the users' previous output
2012-09-25 08:18:16 -04:00
colorized = colorized.sub(/(\n*)\z/, "\e[0m\\1") if Pry.color
result = colorized.gsub(/%<(.*?)#{nonce}/, '#<\1')
result = "=> #{result}"if options[:hashrocket]
Helpers::BaseHelpers.stagger_output(result, output)
end
# 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
# useful when playing with truly enormous objects
CLIPPED_PRINT = proc do |output, value|
output.puts Pry.view_clip(value)
end
# Will only show the first line of the backtrace
DEFAULT_EXCEPTION_HANDLER = proc do |output, exception, _|
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
end
DEFAULT_PROMPT_NAME = 'pry'
# The default prompt; includes the target and nesting level
DEFAULT_PROMPT = [
proc { |target_self, nest_level, pry|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}> "
},
proc { |target_self, nest_level, pry|
"[#{pry.input_array.size}] #{Pry.config.prompt_name}(#{Pry.view_clip(target_self)})#{":#{nest_level}" unless nest_level.zero?}* "
}
]
# A simple prompt - doesn't display target or nesting level
2011-07-26 13:16:25 -04:00
SIMPLE_PROMPT = [proc { ">> " }, proc { " | " }]
SHELL_PROMPT = [
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} * " }
]
# A prompt that includes the full object path as well as
# input/output (_in_ and _out_) information. Good for navigation.
NAV_PROMPT = [
proc do |conf|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{conf.expr_number}] (#{Pry.config.prompt_name}) #{tree}: #{conf.nesting_level}> "
end,
proc do |conf|
tree = conf.binding_stack.map { |b| Pry.view_clip(b.eval("self")) }.join " / "
"[#{conf.expr_number}] (#{ Pry.config.prompt_name}) #{tree}: #{conf.nesting_level}* "
end,
]
# 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 ..`.
DEFAULT_CONTROL_D_HANDLER = proc do |eval_string, _pry_|
if !eval_string.empty?
eval_string.replace('') # Clear input buffer.
elsif _pry_.binding_stack.one?
_pry_.binding_stack.clear
throw(:breakout)
else
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
# Otherwise, saves current binding stack as old stack and pops last
# binding out of binding stack (the old stack still has that binding).
_pry_.command_state["cd"] ||= OpenStruct.new # FIXME
_pry_.command_state['cd'].old_stack = _pry_.binding_stack.dup
Change behavior of `cd -` command Since banister begged me to do that... completely rewrite `cd -` command (implemetation is much simpler now). This commit brings such changes: * completely rewrite behavior of `cd -` command; * implement ScratchPad aka Pad for unit testing purposes (by banister); * use Pad riches in the unit tests for `cd -` command; * remove verbose and clunky unit tests; This commit brings new meaning to the `cd -` command. The main difference is that the new command saves entire binding stack, not just the last binding. Let me show you an example of the variance between these two implemetations: * Old `cd -` implementation saves *only* last binding. With our next `cd -` invocation our interjacent results are lost: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):1> nesting Nesting status: -- 0. main (Pry top level) 1. 4 Also, there are a few bugs in old `cd -` command: * you type `cd :foo`, `cd 1/2/3` and `cd -`. The last command relocates you to the scope of `3` (leaves you where you was), when `:foo` is expected; * you type `cd :foo`, `cd 1/2/3/../4`, `cd -`. The last command relocates you to the scope of `3`, when `:foo` is expected. * New and shiny `cd -` is devoid of those shortcomings: [1] pry(main)> cd 1/2/3/../4 [2] pry(4):3> cd - [3] pry(main)> cd - [4] pry(4):3> nesting Nesting status: -- 0. main (Pry top level) 1. 1 2. 2 3. 4 As I said before, this solution is *much* simpler and less error-prone. Signed-off-by: Kyrylo Silin <kyrylosilin@gmail.com>
2012-06-27 07:54:07 -04:00
_pry_.binding_stack.pop
end
end
DEFAULT_SYSTEM = proc do |output, cmd, _|
if !system(cmd)
output.puts "Error: there was a problem executing system command: #{cmd}"
end
end
2012-08-13 01:41:28 -04: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
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
# 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
# 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 *Pry.config.exception_whitelist
2011-08-26 20:44:13 -04:00
false
# All other exceptions will be caught.
2011-08-26 20:44:13 -04:00
else
true
end
end
end
# An Exception Tag (cf. Exceptional Ruby) that instructs Pry to show the error in
# a more user-friendly manner. This should be used when the exception happens within
# Pry itself as a direct consequence of the user typing something wrong.
#
# This allows us to distinguish between the user typing:
#
# pry(main)> def )
# SyntaxError: unexpected )
#
# pry(main)> method_that_evals("def )")
# SyntaxError: (eval):1: syntax error, unexpected ')'
# from ./a.rb:2 in `eval'
module UserError; end
# Catches SecurityErrors if $SAFE is set
module TooSafeException
def self.===(exception)
$SAFE > 0 && SecurityError === exception
end
end
# Don't catch these exceptions
DEFAULT_EXCEPTION_WHITELIST = [SystemExit, SignalException, Pry::TooSafeException]
# CommandErrors are caught by the REPL loop and displayed to the user. They
# indicate an exceptional condition that's fatal to the current command.
2012-01-16 02:45:08 -05:00
class CommandError < StandardError; end
class MethodNotFound < CommandError; end
2012-01-23 01:33:34 -05:00
# indicates obsolete API
class ObsoleteError < StandardError; end
2012-01-23 01:33:34 -05:00
# This is to keep from breaking under Rails 3.2 for people who are doing that
# IRB = Pry thing.
module ExtendCommandBundle
end
end
if Pry::Helpers::BaseHelpers.mri_18?
begin
require 'ruby18_source_location'
rescue LoadError
end
end
require 'method_source'
require 'shellwords'
require 'stringio'
require 'coderay'
require 'slop'
require 'rbconfig'
require 'tempfile'
2013-02-17 23:43:07 -05:00
require 'pathname'
begin
require 'readline'
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
if Pry::Helpers::BaseHelpers.jruby?
2011-09-11 00:28:31 -04:00
begin
require 'ffi'
rescue LoadError
2012-01-22 22:40:40 -05:00
warn "Need to `gem install ffi`"
2011-09-11 00:28:31 -04:00
end
end
if Pry::Helpers::BaseHelpers.windows? && !Pry::Helpers::BaseHelpers.windows_ansi?
begin
require 'win32console'
# 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.
rescue LoadError
warn "For a better pry experience, please use ansicon: http://adoxa.3eeweb.com/ansicon/"
end
end
2012-09-06 02:14:53 -04:00
begin
require 'bond'
rescue LoadError
end
require 'pry/version'
2012-12-21 03:02:13 -05:00
require 'pry/repl'
require 'pry/rbx_method'
require 'pry/rbx_path'
require 'pry/code'
require 'pry/history_array'
require 'pry/helpers'
require 'pry/code_object'
require 'pry/method'
require 'pry/wrapped_module'
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'
require 'pry/pager'
require 'pry/terminal'
require 'pry/editor'
require 'pry/rubygem'