epuber 0.9.4 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,122 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'abstract_logger'
4
+
5
+ module Epuber
6
+ module Logger
7
+ class ConsoleLogger < AbstractLogger
8
+ def _log(level, message, location: nil, backtrace: nil, sticky: false)
9
+ prev_line = _remove_sticky_message
10
+
11
+ # save sticky message
12
+ @sticky_message = message if sticky
13
+
14
+ formatted_message = _format_message(level, message, location: location, backtrace: backtrace)
15
+
16
+ # print the message
17
+ if sticky
18
+ $stdout.print(formatted_message)
19
+ else
20
+ $stdout.puts(formatted_message)
21
+
22
+ # reprint previous sticky message when this message is not sticky
23
+ if prev_line
24
+ @sticky_message = prev_line
25
+ $stdout.print(prev_line)
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ # @param [Symbol] level color of the output
33
+ #
34
+ # @return [Symbol] color
35
+ #
36
+ def _color_from_level(level)
37
+ case level
38
+ when :error then :red
39
+ when :warning then :yellow
40
+ when :info then :white
41
+ when :debug then :blue
42
+ else
43
+ raise "Unknown output level #{level}"
44
+ end
45
+ end
46
+
47
+ # @param [Symbol] level color of the output
48
+ # @param [String] message message of the error
49
+ # @param [Thread::Backtrace::Location] location location of the error
50
+ #
51
+ # @return [String] formatted message
52
+ #
53
+ def _format_message(level, message, location: nil, backtrace: nil)
54
+ location = _location_from_obj(location)
55
+
56
+ comps = []
57
+ comps << message.to_s
58
+ message_already_formatted =
59
+ message.is_a?(Epuber::Compiler::Problem) || message.is_a?(Epuber::Checker::TextChecker::MatchProblem)
60
+
61
+ should_add_location = if message_already_formatted || location.nil?
62
+ false
63
+ else
64
+ %i[error warning].include?(level)
65
+ end
66
+
67
+ # add location
68
+ if should_add_location
69
+ path = location.path
70
+
71
+ # calculate relative path when path is absolute and in project
72
+ path = path[Config.instance.project_path.size + 1..-1] if path.start_with?(Config.instance.project_path)
73
+
74
+ line_parts = [
75
+ " (in file #{path}",
76
+ ]
77
+ line_parts << "line #{location.lineno}" if location.lineno
78
+ line_parts << "column #{location.column}" if location.column
79
+
80
+ comps << "#{line_parts.join(' ')})"
81
+ end
82
+
83
+ # add backtrace
84
+ comps += _format_backtrace(backtrace, location: location) if backtrace && @verbose && level == :error
85
+
86
+ comps.join("\n").ansi.send(_color_from_level(level))
87
+ end
88
+
89
+ # @param [Array<Thread::Backtrace::Location>] locations locations of the error (only for verbose output)
90
+ # @param [Thread::Backtrace::Location] location location of the error
91
+ #
92
+ # @return [Array<String>] formatted message
93
+ #
94
+ def _format_backtrace(locations, location: nil)
95
+ index = locations.index(location) || 0
96
+
97
+ formatted = []
98
+ formatted << '' # empty line
99
+ formatted << 'Full backtrace:'
100
+ formatted += locations[index, locations.size].map do |loc|
101
+ " #{loc}"
102
+ end
103
+
104
+ formatted
105
+ end
106
+
107
+ # @return [String, nil] last line
108
+ #
109
+ def _remove_sticky_message
110
+ last_line = @sticky_message
111
+
112
+ unless @sticky_message.nil?
113
+ $stdout.print("\033[2K") # remove line, but without moving cursor
114
+ $stdout.print("\r") # go to beginning of line
115
+ @sticky_message = nil
116
+ end
117
+
118
+ last_line
119
+ end
120
+ end
121
+ end
122
+ end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Epuber
4
- VERSION = '0.9.4'
4
+ VERSION = '0.10.0'
5
5
 
6
6
  HOME_URL = 'https://github.com/epuber-io/epuber'
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epuber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.4
4
+ version: 0.10.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roman Kříž
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-13 00:00:00.000000000 Z
11
+ date: 2024-05-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -568,6 +568,9 @@ files:
568
568
  - lib/epuber/transformer/book_transformer.rb
569
569
  - lib/epuber/transformer/text_transformer.rb
570
570
  - lib/epuber/user_interface.rb
571
+ - lib/epuber/utils/location.rb
572
+ - lib/epuber/utils/logger/abstract_logger.rb
573
+ - lib/epuber/utils/logger/console_logger.rb
571
574
  - lib/epuber/vendor/hash_binding.rb
572
575
  - lib/epuber/vendor/nokogiri_extensions.rb
573
576
  - lib/epuber/vendor/ruby_templater.rb