Class: Pry::Command::Cat::ExceptionFormatter

Inherits:
AbstractFormatter show all
Includes:
Helpers::Text
Defined in:
lib/pry/commands/cat/exception_formatter.rb

Constant Summary

Constants included from Helpers::Text

Helpers::Text::COLORS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers::Text

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

Methods inherited from AbstractFormatter

#between_lines, #code_type, #decorate, #use_line_numbers?

Methods included from Helpers::BaseHelpers

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

Methods included from Platform

#jruby?, #jruby_19?, #known_engines, #linux?, #mac_osx?, #mri?, #mri_19?, #mri_2?, #rbx?, #windows?, #windows_ansi?

Methods included from Helpers::CommandHelpers

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

Methods included from Helpers::OptionsHelpers

#method_object, method_object, #method_options, method_options

Constructor Details

#initialize(exception, _pry_, opts) ⇒ ExceptionFormatter

Returns a new instance of ExceptionFormatter



9
10
11
12
13
# File 'lib/pry/commands/cat/exception_formatter.rb', line 9

def initialize(exception, _pry_, opts)
  @ex = exception
  @opts = opts
  @_pry_ = _pry_
end

Instance Attribute Details

#_pry_Object (readonly)

Returns the value of attribute pry



6
7
8
# File 'lib/pry/commands/cat/exception_formatter.rb', line 6

def _pry_
  @_pry_
end

#exObject (readonly)

Returns the value of attribute ex



4
5
6
# File 'lib/pry/commands/cat/exception_formatter.rb', line 4

def ex
  @ex
end

#optsObject (readonly)

Returns the value of attribute opts



5
6
7
# File 'lib/pry/commands/cat/exception_formatter.rb', line 5

def opts
  @opts
end

Instance Method Details

#backtrace_fileObject (private)



48
49
50
# File 'lib/pry/commands/cat/exception_formatter.rb', line 48

def backtrace_file
  Array(ex.bt_source_location_for(backtrace_level)).first
end

#backtrace_levelObject (private)



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/pry/commands/cat/exception_formatter.rb', line 30

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_lineObject (private)



52
53
54
# File 'lib/pry/commands/cat/exception_formatter.rb', line 52

def backtrace_line
  Array(ex.bt_source_location_for(backtrace_level)).last
end

#check_for_errorsObject (private)

Raises:



56
57
58
59
# File 'lib/pry/commands/cat/exception_formatter.rb', line 56

def check_for_errors
  raise CommandError, "No exception found." unless ex
  raise CommandError, "The given backtrace level is out of bounds." unless backtrace_file
end

#code_window_sizeObject (private)



26
27
28
# File 'lib/pry/commands/cat/exception_formatter.rb', line 26

def code_window_size
  _pry_.config.default_window_size || 5
end

#formatObject



15
16
17
18
19
20
21
22
# File 'lib/pry/commands/cat/exception_formatter.rb', line 15

def format
  check_for_errors
  set_file_and_dir_locals(backtrace_file, _pry_, _pry_.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

#headerObject (private)



68
69
70
71
72
73
74
75
# File 'lib/pry/commands/cat/exception_formatter.rb', line 68

def header
  unindent %{
  #{bold 'Exception:'} #{ex.class}: #{ex.message}
  --
  #{bold('From:')} #{backtrace_file} @ line #{backtrace_line} @ #{bold("level: #{backtrace_level}")} of backtrace (of #{ex.backtrace.size - 1}).

}
end

#increment_backtrace_levelObject (private)



44
45
46
# File 'lib/pry/commands/cat/exception_formatter.rb', line 44

def increment_backtrace_level
  ex.inc_bt_index
end

#start_and_end_line_for_code_windowObject (private)



61
62
63
64
65
66
# File 'lib/pry/commands/cat/exception_formatter.rb', line 61

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