pretty_trace 0.2.5 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7895d1287b0efe660fd6807cc8b26c6bde79d96aef0030b4da2fe97bd0b77cf
4
- data.tar.gz: b0618c23b25b8cd17fe82ac77b4daace01828163c46a2f5af370e587f55e1b4f
3
+ metadata.gz: 8a8414a3aea1ebb47b1e194b2b9aefcbb8086064032148609d42b287b2f4eff6
4
+ data.tar.gz: 22649b6a4c529271f418c7212cfe735fa16f468c6a54a0e4057af6a88cf8c9d7
5
5
  SHA512:
6
- metadata.gz: f90fb665503e3a4e273783874a0c032b4224453913ed7f7b1c4d9c11a8f5d8e7d3860e7805c6931221c21feb7a8a12d68cb441af4bc24bffde01120ba8bad854
7
- data.tar.gz: 184e2517630768749a2aef7b051f9c7e75859ff09bec1fcd200d5140e2f040dcc0618d5e634575e9d1098ceb25fb34b42f6e95535cd9eb84ffad597000b9d7da
6
+ metadata.gz: ba665c8242bcf0eb200f25e6413f45cbf49073ab4da2700cf808dd879305fb301b0db9b608c26ab8491d4e247d4398a9d8d706e9c64dfeeb55bef88423428bd9
7
+ data.tar.gz: fd8e854d010fc604ce3d21c67acaa0b81d6a665f0361f1d8b678776902933395767c639e207bb947537958fe6cdf40a1c92533590c18267ea5e66211b0bec4b6
data/README.md CHANGED
@@ -1,11 +1,19 @@
1
+ <div align='center'>
2
+
1
3
  # Pretty Trace - Pretty Errors and Backtrace
2
4
 
3
5
  [![Gem Version](https://badge.fury.io/rb/pretty_trace.svg)](https://badge.fury.io/rb/pretty_trace)
4
6
  [![Build Status](https://github.com/DannyBen/pretty_trace/workflows/Test/badge.svg)](https://github.com/DannyBen/pretty_trace/actions?query=workflow%3ATest)
5
7
  [![Maintainability](https://api.codeclimate.com/v1/badges/c9db6ec58ec7ac1484aa/maintainability)](https://codeclimate.com/github/DannyBen/pretty_trace/maintainability)
6
8
 
9
+ ![screenshot](support/screenshot/enabled.svg)
10
+
11
+ </div>
12
+
7
13
  ---
8
14
 
15
+
16
+
9
17
  Make your Ruby backtrace pretty again. Just require `pretty_trace/enable`
10
18
  in your ruby script, and errors will become clearer and more readable.
11
19
 
@@ -32,22 +40,16 @@ gem 'pretty_trace', require: 'pretty_trace/enable-trim'
32
40
  ```
33
41
 
34
42
 
35
- ## Example
36
-
37
- ### Create this ruby file:
43
+ ## Quick Start
38
44
 
39
45
  ```ruby
40
46
  # test.rb
41
47
  require "pretty_trace/enable-trim"
42
48
  require "fileutils"
49
+
43
50
  FileUtils.rm 'no_such_file'
44
51
  ```
45
52
 
46
- ### Run it:
47
-
48
- ![screenshot](/screenshot.gif)
49
-
50
-
51
53
  ## Usage
52
54
 
53
55
  The easiest way to use Pretty Trace is to require its activation script in
@@ -82,10 +84,12 @@ PrettyTrace.disable
82
84
 
83
85
  PrettyTrace.enable
84
86
  PrettyTrace.trim
85
- # Exceptions here will be formatted and trimmed
87
+ PrettyTrace.reverse
88
+ # Exceptions here will be formatted, trimmed and in reverse order
86
89
 
87
90
  PrettyTrace.no_trim
88
- # Exceptions here will not be trimmed
91
+ PrettyTrace.no_reverse
92
+ # Exceptions here will not be trimmed or reversed
89
93
  ```
90
94
 
91
95
 
@@ -6,17 +6,18 @@ module PrettyTrace
6
6
 
7
7
  def initialize(original_line)
8
8
  @original_line = original_line
9
- @path, @file, @line, @dir, @full_dir = nil, nil, nil, nil, nil
10
9
  @formatted = false
11
10
 
12
- if @original_line =~ /(.+):(-?\d+):in `(.+)'/
13
- @formatted = true
14
- @path, @line, @method = $1, $2, $3
15
- @full_dir = File.dirname(@path)
16
- @dir = @full_dir.split('/').last
17
- @dir = @dir == '.' ? '' : "#{dir}/"
18
- @file = File.basename @path
19
- end
11
+ return unless @original_line =~ /(.+):(-?\d+):in `(.+)'/
12
+
13
+ @formatted = true
14
+ @path = $1
15
+ @line = $2
16
+ @method = $3
17
+ @full_dir = File.dirname(@path)
18
+ @dir = @full_dir.split('/').last
19
+ @dir = @dir == '.' ? '' : "#{dir}/"
20
+ @file = File.basename @path
20
21
  end
21
22
 
22
23
  def formatted?
@@ -28,7 +29,7 @@ module PrettyTrace
28
29
  end
29
30
 
30
31
  def colored_line
31
- "line %{green}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
32
+ "line %{green_bold}#{line.to_s.ljust 4}%{reset} in %{cyan}#{dir}%{magenta}#{file}%{reset} > %{blue}#{method}%{reset}" % colors
32
33
  end
33
34
  end
34
35
  end
@@ -2,9 +2,16 @@ module PrettyTrace
2
2
  module Colors
3
3
  def colors
4
4
  {
5
- reset: "\e[0m", black: "\e[30m", red: "\e[31m",
6
- green: "\e[32m", yellow: "\e[33m", blue: "\e[34m",
7
- magenta:"\e[35m", cyan: "\e[36m", white: "\e[37m",
5
+ reset: "\e[0m",
6
+ bold: "\e[1m",
7
+ black: "\e[30m", black_bold: "\e[1;30m",
8
+ red: "\e[31m", red_bold: "\e[1;31m",
9
+ green: "\e[32m", green_bold: "\e[1;32m",
10
+ yellow: "\e[33m", yellow_bold: "\e[1;33m",
11
+ blue: "\e[34m", blue_bold: "\e[1;34m",
12
+ magenta: "\e[35m", magenta_bold: "\e[1;35m",
13
+ cyan: "\e[36m", cyan_bold: "\e[1;36m",
14
+ white: "\e[37m", white_bold: "\e[1;37m",
8
15
  }
9
16
  end
10
17
  end
@@ -1,70 +1,70 @@
1
- require 'singleton'
2
-
3
1
  module PrettyTrace
4
2
  class Handler
5
- include Singleton
6
- include Colors
3
+ class << self
4
+ include Colors
5
+
6
+ attr_writer :options
7
7
 
8
- def enable
9
- @enabled = true
10
- # :nocov: - this is actually covered through an external process
11
- at_exit do
12
- if @enabled and $! and !ignored.include? $!.class
13
- show_errors $!
14
- $stderr.reopen IO::NULL
15
- $stdout.reopen IO::NULL
8
+ def enable
9
+ @enabled = true
10
+ # :nocov: - this is actually covered through an external process
11
+ at_exit do
12
+ if @enabled && $! && !ignored.include?($!.class)
13
+ show_errors $!
14
+ $stderr.reopen IO::NULL
15
+ $stdout.reopen IO::NULL
16
+ end
16
17
  end
18
+ # :nocov:
17
19
  end
18
- # :nocov:
19
- end
20
20
 
21
- def disable
22
- @enabled = false
23
- end
21
+ def disable
22
+ @enabled = false
23
+ end
24
24
 
25
- def enabled?
26
- @enabled
27
- end
25
+ def enabled?
26
+ @enabled
27
+ end
28
28
 
29
- def options
30
- @options ||= default_options
31
- end
29
+ def debug_tip?
30
+ !!(options[:debug_tip] and ENV['PRETTY_TRACE'] != 'full')
31
+ end
32
32
 
33
- def options=(new_options)
34
- @options = new_options
35
- end
33
+ def options
34
+ @options ||= default_options
35
+ end
36
36
 
37
- private
37
+ private
38
38
 
39
- def ignored
40
- # :nocov:
41
- [ SystemExit ]
42
- # :nocov:
43
- end
39
+ def ignored
40
+ # :nocov:
41
+ [SystemExit]
42
+ # :nocov:
43
+ end
44
44
 
45
- def show_errors(exception)
46
- # :nocov:
47
- backtrace = StructuredBacktrace.new exception.backtrace, options
45
+ def show_errors(exception)
46
+ # :nocov:
47
+ backtrace = StructuredBacktrace.new exception.backtrace, options
48
48
 
49
- puts "\n#{backtrace}" unless backtrace.empty?
49
+ puts "\n#{backtrace}" unless backtrace.empty?
50
50
 
51
- message = exception.message
52
- if message.empty?
53
- puts "\n%{blue}#{exception.class}%{reset}\n" % colors
54
- else
55
- puts "\n%{blue}#{exception.class}\n%{red}#{message}%{reset}\n" % colors
56
- end
51
+ message = exception.message
52
+ puts "\n%{red}█ %{reset}%{bold}#{exception.class}%{reset}\n" % colors
53
+ unless message.empty?
54
+ puts "%{red}█ %{reset}%{yellow}#{message}%{reset}\n" % colors
55
+ end
57
56
 
58
- if options[:debug_tip] and ENV['PRETTY_TRACE'] != 'full'
59
- puts "\nTIP: Run with %{cyan}PRETTY_TRACE=full%{reset} (or %{cyan}off%{reset}) for debug information" % colors
60
- end
57
+ if debug_tip?
58
+ puts "\nTip: Run with %{cyan}PRETTY_TRACE=full%{reset} (or %{cyan}off%{reset}) for debug information" % colors
59
+ end
61
60
 
62
- $stdout.flush
63
- # :nocov:
64
- end
61
+ $stdout.flush
62
+ # :nocov:
63
+ end
65
64
 
66
- def default_options
67
- { filter: [] }
65
+ def default_options
66
+ { filter: [] }
67
+ end
68
68
  end
69
69
  end
70
- end;
70
+ end
@@ -1,33 +1,43 @@
1
1
  module PrettyTrace
2
- def self.enable
3
- Handler.instance.enable unless ENV['PRETTY_TRACE'] == 'off'
4
- end
2
+ class << self
3
+ def enable
4
+ Handler.enable unless ENV['PRETTY_TRACE'] == 'off'
5
+ end
5
6
 
6
- def self.disable
7
- Handler.instance.disable
8
- end
7
+ def disable
8
+ Handler.disable
9
+ end
9
10
 
10
- def self.filter(filter)
11
- if filter.is_a? Array
12
- Handler.instance.options[:filter] += filter
13
- else
14
- Handler.instance.options[:filter] << filter
11
+ def filter(filter)
12
+ if filter.is_a? Array
13
+ Handler.options[:filter] += filter
14
+ else
15
+ Handler.options[:filter] << filter
16
+ end
15
17
  end
16
- end
17
18
 
18
- def self.debug_tip
19
- Handler.instance.options[:debug_tip] = true
20
- end
19
+ def debug_tip
20
+ Handler.options[:debug_tip] = true
21
+ end
21
22
 
22
- def self.no_debug_tip
23
- Handler.instance.options[:debug_tip] = false
24
- end
23
+ def no_debug_tip
24
+ Handler.options[:debug_tip] = false
25
+ end
25
26
 
26
- def self.trim
27
- Handler.instance.options[:trim] = true
28
- end
27
+ def trim
28
+ Handler.options[:trim] = true
29
+ end
30
+
31
+ def no_trim
32
+ Handler.options[:trim] = false
33
+ end
34
+
35
+ def reverse
36
+ Handler.options[:reverse] = true
37
+ end
29
38
 
30
- def self.no_trim
31
- Handler.instance.options[:trim] = false
39
+ def no_reverse
40
+ Handler.options[:reverse] = false
41
+ end
32
42
  end
33
- end
43
+ end
@@ -2,33 +2,20 @@ module PrettyTrace
2
2
  class StructuredBacktrace
3
3
  attr_reader :options, :backtrace
4
4
 
5
- def initialize(backtrace, options={})
5
+ def initialize(backtrace, options = {})
6
6
  @options = options
7
7
  @backtrace = backtrace
8
8
  end
9
9
 
10
10
  def structure
11
- filter = options[:filter] || []
12
- filter = [filter] unless filter.is_a? Array
13
-
14
- result = backtrace.dup
15
-
16
- unless ENV['PRETTY_TRACE'] == 'full'
17
- filter.each do |expression|
18
- result.reject! { |trace| trace =~ expression }
19
- end
11
+ result = backtrace_list.map { |line| BacktraceItem.new line }
12
+ result = if should_trim? result
13
+ result.group_by(&:path).flat_map { |_, items| [items.first, items.last].uniq }
14
+ else
15
+ result
20
16
  end
21
17
 
22
- result.map! { |line| BacktraceItem.new line }
23
- first_line = result[0]
24
- result.reverse!
25
- result.uniq!(&:path) if should_trim? result
26
-
27
- if first_line and first_line.original_line != result[-1].original_line
28
- result.push first_line
29
- end
30
-
31
- result
18
+ should_reverse? ? result.reverse : result
32
19
  end
33
20
 
34
21
  def formatted_backtrace
@@ -45,8 +32,29 @@ module PrettyTrace
45
32
 
46
33
  private
47
34
 
35
+ def backtrace_list
36
+ result = backtrace.dup
37
+
38
+ unless ENV['PRETTY_TRACE'] == 'full'
39
+ filters.each do |expression|
40
+ result.reject! { |trace| trace =~ expression }
41
+ end
42
+ end
43
+
44
+ result.reverse
45
+ end
46
+
47
+ def filters
48
+ result = options[:filter] || []
49
+ result.is_a?(Array) ? result : [result]
50
+ end
51
+
48
52
  def should_trim?(backtrace)
49
53
  options[:trim] and ENV['PRETTY_TRACE'] != 'full' and backtrace.size > 3
50
54
  end
55
+
56
+ def should_reverse?
57
+ options[:reverse]
58
+ end
51
59
  end
52
60
  end
@@ -1,3 +1,3 @@
1
1
  module PrettyTrace
2
- VERSION = "0.2.5"
3
- end
2
+ VERSION = '0.3.0'
3
+ end
data/lib/pretty_trace.rb CHANGED
@@ -5,4 +5,4 @@ require 'pretty_trace/handler'
5
5
  require 'pretty_trace/structured_backtrace'
6
6
  require 'pretty_trace/module_methods'
7
7
 
8
- require 'byebug' if ENV['BYEBUG']
8
+ require 'debug' if ENV['DEBUGGER']
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pretty_trace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Danny Ben Shitrit
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-01 00:00:00.000000000 Z
11
+ date: 2024-08-25 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Display clean and colorful error messages and backtrace
14
14
  email: db@dannyben.com
@@ -29,8 +29,9 @@ files:
29
29
  homepage: https://github.com/DannyBen/pretty_trace
30
30
  licenses:
31
31
  - MIT
32
- metadata: {}
33
- post_install_message:
32
+ metadata:
33
+ rubygems_mfa_required: 'true'
34
+ post_install_message:
34
35
  rdoc_options: []
35
36
  require_paths:
36
37
  - lib
@@ -38,15 +39,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
38
39
  requirements:
39
40
  - - ">="
40
41
  - !ruby/object:Gem::Version
41
- version: 2.0.0
42
+ version: '3.0'
42
43
  required_rubygems_version: !ruby/object:Gem::Requirement
43
44
  requirements:
44
45
  - - ">="
45
46
  - !ruby/object:Gem::Version
46
47
  version: '0'
47
48
  requirements: []
48
- rubygems_version: 3.0.3
49
- signing_key:
49
+ rubygems_version: 3.5.17
50
+ signing_key:
50
51
  specification_version: 4
51
52
  summary: Pretty backtrace and error messages
52
53
  test_files: []