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