console 1.10.1 → 1.11.1

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d9156ec2b29460187235a07fa3d66c53a008c78987873413415a8f66d0d3ccb6
4
- data.tar.gz: e27d6622b77fc3b5b2c616523956f67ee33e1cc0243d4d8313242e88cb8fe007
3
+ metadata.gz: 149a9d0738e731ea3ef3159cd25a449f663070406dfa232538a5996fb93c9ce9
4
+ data.tar.gz: 212772395d4aef7dc7a510d6630e11dfabeb4c8cf2c70f5d272c279a3d2a32ef
5
5
  SHA512:
6
- metadata.gz: ae3e4e8c9aba59786238e9ff0ba6e5568d891aae95f7dbe2276875a62ef895def36fc31d33aced9ed287897374d96d9678a1c7c0f0d045eca2b2e6f4f56f8fbb
7
- data.tar.gz: ec3bba369b4bad6139a1509fe51665b7d75df5af2a4176db6f6abe73e64753a86eca97aa11852cc4073db0645fbe9d3b32981b8507a46409f6c4a4cb43071910
6
+ metadata.gz: d02021cfb6dbf99651d5f1b8653394dd59f2c830b0a62b8c394b70b275030cd2c066a786a9cb1281df55029f9a69455c00a73a1763785cba3a5ccf8769766654
7
+ data.tar.gz: be05f291ccae55c3a6cce6ed2e5e79fa968682d856bcc502693408c69cc85b563017e8505a0459f6cb0d628bbdc57b58efd28146b460e09b45695652e6263145
data/lib/console.rb CHANGED
@@ -20,6 +20,7 @@
20
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
21
  # THE SOFTWARE.
22
22
 
23
+ require_relative 'console/version'
23
24
  require_relative 'console/logger'
24
25
 
25
26
  module Console
@@ -0,0 +1,50 @@
1
+ # Copyright, 2021, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ module Console
22
+ module Clock
23
+ def self.formatted_duration(duration)
24
+ if duration < 60.0
25
+ return "#{duration.round(2)}s"
26
+ end
27
+
28
+ duration /= 60.0
29
+
30
+ if duration < 60.0
31
+ return "#{duration.round}m"
32
+ end
33
+
34
+ duration /= 60.0
35
+
36
+ if duration < 60.0
37
+ return "#{duration.round(1)}h"
38
+ end
39
+
40
+ duration /= 24.0
41
+
42
+ return "#{duration.round(1)}d"
43
+ end
44
+
45
+ # Get the current elapsed monotonic time.
46
+ def self.now
47
+ ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
48
+ end
49
+ end
50
+ end
@@ -47,6 +47,8 @@ module Console
47
47
  terminal[:exception_title] ||= terminal.style(:red, nil, :bold)
48
48
  terminal[:exception_detail] ||= terminal.style(:yellow)
49
49
  terminal[:exception_backtrace] ||= terminal.style(:red)
50
+ terminal[:exception_backtrace_other] ||= terminal.style(:red, nil, :faint)
51
+ terminal[:exception_message] ||= terminal.style(:default)
50
52
  end
51
53
 
52
54
  def to_h
@@ -66,13 +68,18 @@ module Console
66
68
  output.puts " #{terminal[:exception_detail]}#{line}#{terminal.reset}"
67
69
  end
68
70
 
71
+ root_pattern = /^#{@root}\// if @root
72
+
69
73
  exception.backtrace&.each_with_index do |line, index|
70
74
  path, offset, message = line.split(":")
75
+ style = :exception_backtrace
71
76
 
72
77
  # Make the path a bit more readable
73
- path.gsub!(/^#{@root}\//, "./") if @root
78
+ if root_pattern and path.sub!(root_pattern, "").nil?
79
+ style = :exception_backtrace_other
80
+ end
74
81
 
75
- output.puts " #{index == 0 ? "→" : " "} #{terminal[:exception_backtrace]}#{path}:#{offset}#{terminal.reset} #{message}"
82
+ output.puts " #{index == 0 ? "→" : " "} #{terminal[style]}#{path}:#{offset}#{terminal[:exception_message]} #{message}#{terminal.reset}"
76
83
  end
77
84
 
78
85
  if exception.cause
@@ -0,0 +1,42 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'metric'
22
+ require_relative '../clock'
23
+
24
+ module Console
25
+ module Event
26
+ class Enter < Generic
27
+ def initialize(name)
28
+ @name = name
29
+ end
30
+
31
+ def format(output, terminal, verbose)
32
+ output.puts "→ #{@name}"
33
+ end
34
+ end
35
+
36
+ class Exit < Metric
37
+ def value_string
38
+ "← #{@name} took #{Clock.formatted_duration(@value)}"
39
+ end
40
+ end
41
+ end
42
+ end
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'generic'
22
+ require_relative '../clock'
22
23
 
23
24
  module Console
24
25
  module Event
@@ -41,8 +42,16 @@ module Console
41
42
  {name: @name, value: @value, tags: @tags}
42
43
  end
43
44
 
45
+ def value_string
46
+ "#{@name}: #{@value}"
47
+ end
48
+
44
49
  def format(output, terminal, verbose)
45
- output.puts "#{@name}=#{@value} #{@tags.inspect}"
50
+ if @tags&.any?
51
+ output.puts "#{value_string} #{@tags.inspect}"
52
+ else
53
+ output.puts value_string
54
+ end
46
55
  end
47
56
  end
48
57
  end
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'filter'
22
+ require_relative 'measure'
22
23
  require_relative 'progress'
23
24
 
24
25
  require_relative 'resolver'
@@ -54,7 +55,7 @@ module Console
54
55
  terminal = Terminal::Logger.new(output, verbose: verbose)
55
56
 
56
57
  logger = self.new(terminal, verbose: verbose, level: level)
57
- resolver = Resolver.default_resolver(logger)
58
+ Resolver.default_resolver(logger)
58
59
 
59
60
  return logger
60
61
  end
@@ -73,8 +74,15 @@ module Console
73
74
  Progress.new(self, subject, total, **options)
74
75
  end
75
76
 
76
- # @deprecated Please use {progress}.
77
- alias measure progress
77
+ def measure(subject, name = "block", **tags, &block)
78
+ measure = Measure.new(self, subject, **tags)
79
+
80
+ if block_given?
81
+ return measure.duration(name, &block)
82
+ else
83
+ return measure
84
+ end
85
+ end
78
86
 
79
87
  def failure(subject, exception, *arguments, &block)
80
88
  fatal(subject, *arguments, Event::Failure.new(exception), &block)
@@ -0,0 +1,49 @@
1
+ # Copyright, 2017, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
+ #
3
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ # of this software and associated documentation files (the "Software"), to deal
5
+ # in the Software without restriction, including without limitation the rights
6
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ # copies of the Software, and to permit persons to whom the Software is
8
+ # furnished to do so, subject to the following conditions:
9
+ #
10
+ # The above copyright notice and this permission notice shall be included in
11
+ # all copies or substantial portions of the Software.
12
+ #
13
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ # THE SOFTWARE.
20
+
21
+ require_relative 'event/measure'
22
+ require_relative 'clock'
23
+
24
+ module Console
25
+ class Measure
26
+ def initialize(output, subject, **tags)
27
+ @output = output
28
+ @subject = subject
29
+ @tags = tags
30
+ end
31
+
32
+ attr :tags
33
+
34
+ # Measure the execution of a block of code.
35
+ def duration(name, &block)
36
+ @output.info(@subject) {Event::Enter.new(name)}
37
+
38
+ start_time = Clock.now
39
+
40
+ result = yield(self)
41
+
42
+ duration = Clock.now - start_time
43
+
44
+ @output.info(@subject) {Event::Exit.new(name, duration, **@tags)}
45
+
46
+ return result
47
+ end
48
+ end
49
+ end
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require_relative 'event/progress'
22
+ require_relative 'clock'
22
23
 
23
24
  module Console
24
25
  class Progress
@@ -93,7 +94,7 @@ module Console
93
94
 
94
95
  def to_s
95
96
  if estimated_remaining_time = self.estimated_remaining_time
96
- "#{@current}/#{@total} completed in #{formatted_duration(self.duration)}, #{formatted_duration(estimated_remaining_time)} remaining."
97
+ "#{@current}/#{@total} completed in #{Clock.formatted_duration(self.duration)}, #{Clock.formatted_duration(estimated_remaining_time)} remaining."
97
98
  else
98
99
  "#{@current}/#{@total} completed, waiting for estimate..."
99
100
  end
@@ -108,33 +109,13 @@ module Console
108
109
  end
109
110
 
110
111
  def output?
111
- if duration = duration_since_last_output
112
+ if remaining.zero?
113
+ return true
114
+ elsif duration = duration_since_last_output
112
115
  return duration > @minimum_output_duration
113
116
  else
114
117
  return true
115
118
  end
116
119
  end
117
-
118
- def formatted_duration(duration)
119
- if duration < 60.0
120
- return "#{duration.round(2)}s"
121
- end
122
-
123
- duration /= 60.0
124
-
125
- if duration < 60.0
126
- return "#{duration.round}m"
127
- end
128
-
129
- duration /= 60.0
130
-
131
- if duration < 60.0
132
- return "#{duration.round(1)}h"
133
- end
134
-
135
- duration /= 24.0
136
-
137
- return "#{duration.round(1)}d"
138
- end
139
120
  end
140
121
  end
@@ -25,6 +25,7 @@ require_relative 'text'
25
25
  require_relative 'xterm'
26
26
 
27
27
  require 'json'
28
+ require 'fiber'
28
29
 
29
30
  module Console
30
31
  module Terminal
@@ -66,7 +67,6 @@ module Console
66
67
  @verbose = verbose
67
68
  end
68
69
 
69
- @terminal[:logger_prefix] ||= @terminal.style(nil, nil, nil)
70
70
  @terminal[:logger_suffix] ||= @terminal.style(:white, nil, :faint)
71
71
  @terminal[:subject] ||= @terminal.style(nil, nil, :bold)
72
72
  @terminal[:debug] = @terminal.style(:cyan)
@@ -136,7 +136,7 @@ module Console
136
136
  def format_argument(argument, output)
137
137
  case argument
138
138
  when Exception
139
- Event::Failure.new(argument).format(output, @terminal, @verbose)
139
+ Event::Failure.for(argument).format(output, @terminal, @verbose)
140
140
  when Event::Generic
141
141
  argument.format(output, @terminal, @verbose)
142
142
  else
@@ -154,11 +154,21 @@ module Console
154
154
  end
155
155
  end
156
156
 
157
+ def default_suffix(object = nil)
158
+ buffer = +" #{@terminal[:logger_suffix]}"
159
+
160
+ if object
161
+ buffer << "[oid=0x#{object.object_id.to_s(16)}] "
162
+ end
163
+
164
+ buffer << "[ec=0x#{Fiber.current.object_id.to_s(16)}] [pid=#{Process.pid}] [#{::Time.now}]#{@terminal.reset}"
165
+ end
166
+
157
167
  def format_object_subject(severity, prefix, subject, output)
158
168
  prefix_style = @terminal[severity]
159
169
 
160
170
  if @verbose
161
- suffix = " #{@terminal[:logger_suffix]}[oid=0x#{subject.object_id.to_s(16)}] [pid=#{Process.pid}] [#{Time.now}]#{@terminal.reset}"
171
+ suffix = default_suffix(subject)
162
172
  end
163
173
 
164
174
  prefix = "#{prefix_style}#{prefix}:#{@terminal.reset} "
@@ -170,7 +180,7 @@ module Console
170
180
  prefix_style = @terminal[severity]
171
181
 
172
182
  if @verbose
173
- suffix = " #{@terminal[:logger_suffix]}[pid=#{Process.pid}] [#{Time.now}]#{@terminal.reset}"
183
+ suffix = default_suffix
174
184
  end
175
185
 
176
186
  prefix = "#{prefix_style}#{prefix}:#{@terminal.reset} "
@@ -187,15 +197,7 @@ module Console
187
197
  end
188
198
 
189
199
  def time_offset_prefix
190
- offset = Time.now - @start_at
191
- minutes = (offset/60).floor
192
- seconds = (offset - (minutes*60))
193
-
194
- if minutes > 0
195
- "#{minutes}m#{seconds.floor}s"
196
- else
197
- "#{seconds.round(2)}s"
198
- end.rjust(6)
200
+ Clock.formatted_duration(Time.now - @start_at).rjust(6)
199
201
  end
200
202
 
201
203
  def build_prefix(name)
@@ -19,5 +19,5 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  module Console
22
- VERSION = "1.10.1"
22
+ VERSION = "1.11.1"
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: console
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.10.1
4
+ version: 1.11.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-01 00:00:00.000000000 Z
11
+ date: 2021-04-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: fiber-local
@@ -104,14 +104,17 @@ files:
104
104
  - lib/console.rb
105
105
  - lib/console/buffer.rb
106
106
  - lib/console/capture.rb
107
+ - lib/console/clock.rb
107
108
  - lib/console/event.rb
108
109
  - lib/console/event/failure.rb
109
110
  - lib/console/event/generic.rb
111
+ - lib/console/event/measure.rb
110
112
  - lib/console/event/metric.rb
111
113
  - lib/console/event/progress.rb
112
114
  - lib/console/event/spawn.rb
113
115
  - lib/console/filter.rb
114
116
  - lib/console/logger.rb
117
+ - lib/console/measure.rb
115
118
  - lib/console/progress.rb
116
119
  - lib/console/resolver.rb
117
120
  - lib/console/serialized/logger.rb
@@ -140,7 +143,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
140
143
  - !ruby/object:Gem::Version
141
144
  version: '0'
142
145
  requirements: []
143
- rubygems_version: 3.1.2
146
+ rubygems_version: 3.2.3
144
147
  signing_key:
145
148
  specification_version: 4
146
149
  summary: Beautiful logging for Ruby.