Class: PrettyTrace::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/pretty_trace/formatter.rb

Class Method Summary collapse

Class Method Details

.colorsObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/pretty_trace/formatter.rb', line 28

def self.colors
  {
    reset:  "\e[0m",
    black:  "\e[30m",
    red:    "\e[31m",
    green:  "\e[32m",
    yellow: "\e[33m",
    blue:   "\e[34m",
    magenta:"\e[35m",
    cyan:   "\e[36m",
    white:  "\e[37m",
  }
end

.pretty_trace(backtrace, opts = {}) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/pretty_trace/formatter.rb', line 3

def self.pretty_trace(backtrace, opts={})
  filter = opts[:filter] || []
  filter = [filter] unless filter.is_a? Array

  filter.each do |expression|
    backtrace.reject! { |trace| trace =~ expression }
  end

  backtrace = trim backtrace if should_trim? backtrace

  backtrace.map! do |item|
    if item =~ /(.+):(-?\d+):in `(.+)'/
      file, line, method = $1, $2, $3
      dir = File.dirname(file).split('/').last
      dir = dir == '.' ? '' : "#{dir}/"
      file = File.basename file

      item = "line %{green}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
    end
    item
  end

  backtrace
end

.should_trim?(backtrace) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
# File 'lib/pretty_trace/formatter.rb', line 46

def self.should_trim?(backtrace)
  ENV['PRETTY_TRACE_TRIM'] and ENV['PRETTY_TRACE'] != 'full' and
    backtrace.size > 3
end

.trim(backtrace) ⇒ Object



42
43
44
# File 'lib/pretty_trace/formatter.rb', line 42

def self.trim(backtrace)
  [backtrace[0], '......    (trimmed)', backtrace[-1]] 
end