class Pry::Command::Cat::ExceptionFormatter

Attributes

ex[R]
opts[R]
pry_instance[R]

Public Class Methods

new(exception, pry_instance, opts) click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 12
def initialize(exception, pry_instance, opts)
  @ex = exception
  @opts = opts
  @pry_instance = pry_instance
end

Public Instance Methods

format() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 18
def format
  check_for_errors
  set_file_and_dir_locals(
    backtrace_file, pry_instance, pry_instance.current_context
  )
  code = decorate(
    Pry::Code.from_file(backtrace_file)
      .between(*start_and_end_line_for_code_window)
      .with_marker(backtrace_line)
  )
  "#{header}#{code}"
end

Private Instance Methods

backtrace_file() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 56
def backtrace_file
  Array(ex.bt_source_location_for(backtrace_level)).first
end
backtrace_level() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 37
def backtrace_level
  @backtrace_level ||=
    begin
      bl =
        if opts[:ex].nil?
          ex.bt_index
        else
          ex.bt_index = absolute_index_number(opts[:ex], ex.backtrace.size)
        end

      increment_backtrace_level
      bl
    end
end
backtrace_line() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 60
def backtrace_line
  Array(ex.bt_source_location_for(backtrace_level)).last
end
check_for_errors() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 64
def check_for_errors
  raise CommandError, "No exception found." unless ex
  return if backtrace_file

  raise CommandError, "The given backtrace level is out of bounds."
end
code_window_size() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 33
def code_window_size
  pry_instance.config.default_window_size || 5
end
header() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 78
def header
  unindent(
    "#{bold 'Exception:'} #{ex.class}: #{ex.message}\n" \
    "--\n" \
    "#{bold('From:')} #{backtrace_file}:#{backtrace_line} @ " \
    "#{bold("level: #{backtrace_level}")} of backtrace " \
    "(of #{ex.backtrace.size - 1}).\n\n"
  )
end
increment_backtrace_level() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 52
def increment_backtrace_level
  ex.inc_bt_index
end
start_and_end_line_for_code_window() click to toggle source
# File lib/pry/commands/cat/exception_formatter.rb, line 71
def start_and_end_line_for_code_window
  start_line = backtrace_line - code_window_size
  start_line = 1 if start_line < 1

  [start_line, backtrace_line + code_window_size]
end