2011-05-01 10:25:37 +00:00
|
|
|
require "pry/default_commands/ls"
|
2012-03-02 01:04:28 +00:00
|
|
|
require "pry/default_commands/cd"
|
2011-05-01 10:25:37 +00:00
|
|
|
|
|
|
|
class Pry
|
|
|
|
module DefaultCommands
|
|
|
|
|
2011-05-07 05:32:05 +00:00
|
|
|
Context = Pry::CommandSet.new do
|
2011-05-01 10:25:37 +00:00
|
|
|
import Ls
|
2012-03-02 01:04:28 +00:00
|
|
|
import Cd
|
2011-05-01 10:25:37 +00:00
|
|
|
|
2012-03-02 08:12:18 +00:00
|
|
|
command "whereami", "Show the code context for the session. (whereami <n> shows <n> extra lines of code around the invocation line. Default: 5)" do |num|
|
|
|
|
file, line_num = file_and_line_from_binding(target)
|
|
|
|
i_num = num ? num.to_i : 5
|
|
|
|
|
|
|
|
if file != Pry.eval_path && (file =~ /(\(.*\))|<.*>/ || file == "" || file == "-e")
|
|
|
|
raise CommandError, "Cannot find local context. Did you use binding.pry?"
|
|
|
|
end
|
|
|
|
|
|
|
|
set_file_and_dir_locals(file)
|
|
|
|
|
|
|
|
method = Pry::Method.from_binding(target)
|
|
|
|
method_description = method ? " in #{method.name_with_owner}" : ""
|
|
|
|
output.puts "\n#{text.bold('From:')} #{file} @ line #{line_num}#{method_description}:\n\n"
|
|
|
|
|
|
|
|
code = Pry::Code.from_file(file).around(line_num, i_num)
|
|
|
|
output.puts code.with_line_numbers.with_marker(line_num)
|
|
|
|
output.puts
|
|
|
|
end
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
create_command "pry-backtrace", "Show the backtrace for the Pry session." do
|
2012-01-15 00:21:10 +00:00
|
|
|
banner <<-BANNER
|
2012-02-23 03:28:00 +00:00
|
|
|
Usage: pry-backtrace [OPTIONS] [--help]
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
Show the backtrace for the position in the code where Pry was started. This can be used to
|
|
|
|
infer the behavior of the program immediately before it entered Pry, just like the backtrace
|
|
|
|
property of an exception.
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
(NOTE: if you are looking for the backtrace of the most recent exception raised,
|
|
|
|
just type: `_ex_.backtrace` instead, see https://github.com/pry/pry/wiki/Special-Locals)
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g: pry-backtrace
|
2012-01-15 00:21:10 +00:00
|
|
|
BANNER
|
|
|
|
|
2012-01-14 09:01:51 +00:00
|
|
|
def process
|
2012-02-23 03:28:00 +00:00
|
|
|
output.puts "\n#{text.bold('Backtrace:')}\n--\n"
|
|
|
|
stagger_output _pry_.backtrace.join("\n")
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
command "reset", "Reset the REPL to a clean state." do
|
|
|
|
output.puts "Pry reset."
|
|
|
|
exec "pry"
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
create_command /wtf([?!]*)/, "Show the backtrace of the most recent exception" do
|
|
|
|
options :listing => 'wtf?'
|
2012-01-15 00:21:10 +00:00
|
|
|
|
|
|
|
banner <<-BANNER
|
2012-02-23 03:28:00 +00:00
|
|
|
Show's a few lines of the backtrace of the most recent exception (also available
|
|
|
|
as _ex_.backtrace).
|
|
|
|
|
|
|
|
If you want to see more lines, add more question marks or exclamation marks:
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g.
|
|
|
|
pry(main)> wtf?
|
|
|
|
pry(main)> wtf?!???!?!?
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
To see the entire backtrace, pass the -v/--verbose flag:
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
e.g.
|
|
|
|
pry(main)> wtf -v
|
2012-01-15 00:21:10 +00:00
|
|
|
BANNER
|
|
|
|
|
2012-02-23 03:28:00 +00:00
|
|
|
def options(opt)
|
|
|
|
opt.on(:v, :verbose, "Show the full backtrace.")
|
|
|
|
end
|
2012-01-15 00:21:10 +00:00
|
|
|
|
2012-01-14 09:01:51 +00:00
|
|
|
def process
|
2012-02-23 03:28:00 +00:00
|
|
|
raise Pry::CommandError, "No most-recent exception" unless _pry_.last_exception
|
2012-03-02 08:12:18 +00:00
|
|
|
|
|
|
|
output.puts "#{text.bold('Exception:')} #{_pry_.last_exception.class}: #{_pry_.last_exception}\n--"
|
2012-02-23 03:28:00 +00:00
|
|
|
if opts.verbose?
|
2012-03-02 08:12:18 +00:00
|
|
|
output.puts Code.new(_pry_.last_exception.backtrace, 0, :text).with_line_numbers.to_s
|
2011-08-25 09:41:43 +00:00
|
|
|
else
|
2012-03-02 08:12:18 +00:00
|
|
|
output.puts Code.new(_pry_.last_exception.backtrace.first([captures[0].size, 0.5].max * 10), 0, :text).with_line_numbers.to_s
|
2011-08-25 09:41:43 +00:00
|
|
|
end
|
|
|
|
end
|
2011-11-29 12:35:45 +00:00
|
|
|
end
|
|
|
|
|
2011-05-01 10:25:37 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|