Class: Pry::Command::Wtf

Inherits:
Pry::ClassCommand show all
Defined in:
lib/pry/commands/wtf.rb

Constant Summary collapse

RUBY_FRAME_PATTERN =
/\A(?<file>(.+)):(?<line>(\d+))/.freeze

Constants inherited from Pry::Command

VOID_VALUE

Constants included from Helpers::DocumentationHelpers

Helpers::DocumentationHelpers::YARD_TAGS

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary

Attributes inherited from Pry::ClassCommand

#args, #opts

Attributes inherited from Pry::Command

#arg_string, #captures, #command_block, #command_set, #context, #eval_string, #hooks, #output, #pry_instance, #target

Instance Method Summary collapse

Methods inherited from Pry::ClassCommand

#call, #complete, doc, #help, inherited, #setup, #slop, source, source_file, source_line, source_location, source_object, #subcommands

Methods inherited from Pry::Command

#_pry_, #after_hooks, banner, #before_hooks, #block, #call_safely, #call_with_hooks, #check_for_command_collision, command_name, #command_name, #command_options, command_regex, #commands, #complete, convert_to_regex, default_options, #description, doc, #find_hooks, group, #initialize, inspect, #interpolate_string, #match, match_score, matches?, name, #name, #normalize_method_args, options, #pass_block, #process_line, #run, #source, source, source_file, source_line, #state, state, subclass, #target_self, #tokenize, #use_unpatched_symbol, #void

Methods included from Pry::CodeObject::Helpers

#c_method?, #c_module?, #command?, #module_with_yard_docs?, #real_method_object?

Methods included from Helpers::DocumentationHelpers

#get_comment_content, get_comment_content, process_comment_markup, #process_comment_markup, #process_rdoc, process_rdoc, #process_yardoc, process_yardoc, process_yardoc_tag, #process_yardoc_tag, #strip_comments_from_c_code, strip_comments_from_c_code, #strip_leading_whitespace, strip_leading_whitespace

Methods included from Helpers::Text

#bold, #default, #indent, #no_color, #no_pager, #strip_color, #with_line_numbers

Methods included from Helpers::CommandHelpers

#absolute_index_number, #absolute_index_range, #get_method_or_raise, #internal_binding?, #one_index_number, #one_index_range, #one_index_range_or_number, #restrict_to_lines, #set_file_and_dir_locals, #temp_file, #unindent

Methods included from Helpers::OptionsHelpers

method_object, #method_object, method_options, #method_options

Methods included from Helpers::BaseHelpers

#colorize_code, #find_command, #heading, #highlight, #not_a_real_file?, #safe_send, #silence_warnings, #stagger_output, #use_ansi_codes?

Constructor Details

This class inherits a constructor from Pry::Command

Instance Method Details

#format_backtrace(backtrace) ⇒ Object (private)


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/pry/commands/wtf.rb', line 64

def format_backtrace(backtrace)
  lines = trim_backtrace(backtrace).map do |frame|
    next frame unless opts.code?

    match = frame.match(RUBY_FRAME_PATTERN)
    code = read_line(match[:file], match[:line].to_i)
    [bold(frame), code].join("\n")
  end

  Pry::Code.new(lines.compact, 0, :text).with_line_numbers.to_s
end

#format_header(title, exception) ⇒ Object (private)


60
61
62
# File 'lib/pry/commands/wtf.rb', line 60

def format_header(title, exception)
  "#{bold(title + ':')} #{exception.class}: #{exception}\n--\n"
end

#options(opt) ⇒ Object


27
28
29
30
# File 'lib/pry/commands/wtf.rb', line 27

def options(opt)
  opt.on :v, :verbose, "Show the full backtrace"
  opt.on :c, :code, "Show code corresponding to the backtrace frame"
end

#processObject


32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pry/commands/wtf.rb', line 32

def process
  unless pry_instance.last_exception
    raise Pry::CommandError, "No most-recent exception"
  end

  text = ''.dup
  unwind_exceptions.each_with_index do |exception, i|
    title = (i == 0 ? 'Exception' : 'Caused by')
    text << format_header(title, exception)
    text << format_backtrace(exception.backtrace)
  end
  output.puts(text)
end

#read_line(file, line) ⇒ Object (private)


83
84
85
86
87
88
89
90
# File 'lib/pry/commands/wtf.rb', line 83

def read_line(file, line)
  File.open(file, 'r') do |f|
    (line - 1).times { f.gets }
    f.gets
  end
rescue Errno::ENOENT
  nil
end

#trim_backtrace(backtrace) ⇒ Object (private)


76
77
78
79
80
81
# File 'lib/pry/commands/wtf.rb', line 76

def trim_backtrace(backtrace)
  return backtrace if opts.verbose?

  size_of_backtrace = [captures[0].size, 0.5].max * 10
  backtrace.first(size_of_backtrace)
end

#unwind_exceptionsObject (private)


48
49
50
51
52
53
54
55
56
57
58
# File 'lib/pry/commands/wtf.rb', line 48

def unwind_exceptions
  exception_list = []
  exception = pry_instance.last_exception

  while exception
    exception_list << exception
    exception = (exception.cause if exception.respond_to?(:cause))
  end

  exception_list
end