2012-08-12 00:22:29 +00:00
|
|
|
class Pry
|
2012-12-25 21:35:17 +00:00
|
|
|
class Command::Wtf < Pry::ClassCommand
|
|
|
|
match /wtf([?!]*)/
|
2012-08-12 01:26:59 +00:00
|
|
|
group 'Context'
|
2012-12-25 21:35:17 +00:00
|
|
|
description 'Show the backtrace of the most recent exception'
|
2012-08-12 00:22:29 +00:00
|
|
|
options :listing => 'wtf?'
|
|
|
|
|
|
|
|
banner <<-BANNER
|
|
|
|
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:
|
|
|
|
|
|
|
|
e.g.
|
|
|
|
pry(main)> wtf?
|
|
|
|
pry(main)> wtf?!???!?!?
|
|
|
|
|
|
|
|
To see the entire backtrace, pass the -v/--verbose flag:
|
|
|
|
|
|
|
|
e.g.
|
|
|
|
pry(main)> wtf -v
|
|
|
|
BANNER
|
|
|
|
|
|
|
|
def options(opt)
|
|
|
|
opt.on(:v, :verbose, "Show the full backtrace.")
|
|
|
|
end
|
|
|
|
|
|
|
|
def process
|
2013-01-03 15:49:07 +00:00
|
|
|
raise Pry::CommandError, "No most-recent exception" unless exception
|
2012-08-12 00:22:29 +00:00
|
|
|
|
2013-01-03 15:49:07 +00:00
|
|
|
output.puts "#{text.bold('Exception:')} #{exception.class}: #{exception}\n--"
|
2012-08-12 00:22:29 +00:00
|
|
|
if opts.verbose?
|
2013-01-02 15:25:18 +00:00
|
|
|
output.puts with_line_numbers(backtrace)
|
2012-08-12 00:22:29 +00:00
|
|
|
else
|
2013-01-02 15:25:18 +00:00
|
|
|
output.puts with_line_numbers(backtrace.first(size_of_backtrace))
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
|
|
|
end
|
2013-01-02 01:06:51 +00:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2013-01-03 15:49:07 +00:00
|
|
|
def exception
|
|
|
|
_pry_.last_exception
|
|
|
|
end
|
|
|
|
|
2013-01-02 15:25:18 +00:00
|
|
|
def with_line_numbers(bt)
|
|
|
|
Pry::Code.new(bt, 0, :text).with_line_numbers.to_s
|
|
|
|
end
|
|
|
|
|
|
|
|
def backtrace
|
2013-01-03 15:49:07 +00:00
|
|
|
exception.backtrace
|
2013-01-02 15:25:18 +00:00
|
|
|
end
|
|
|
|
|
2013-01-02 01:06:51 +00:00
|
|
|
def size_of_backtrace
|
|
|
|
[captures[0].size, 0.5].max * 10
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|
2013-01-02 01:06:51 +00:00
|
|
|
end
|
2012-12-25 21:35:17 +00:00
|
|
|
|
2013-01-02 01:06:51 +00:00
|
|
|
Pry::Commands.add_command(Pry::Command::Wtf)
|
2012-08-12 00:22:29 +00:00
|
|
|
end
|