console 1.10.2 → 1.11.1

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: 8a777953a8fd749c475d101a185f5f45ab11005574c3f23e413c16058e22500b
4
- data.tar.gz: 778b3da976350ffc23d5aad6d142a9398ffbd0be6c8e5931c8c5c9b4f3c49f63
3
+ metadata.gz: 149a9d0738e731ea3ef3159cd25a449f663070406dfa232538a5996fb93c9ce9
4
+ data.tar.gz: 212772395d4aef7dc7a510d6630e11dfabeb4c8cf2c70f5d272c279a3d2a32ef
5
5
  SHA512:
6
- metadata.gz: '0280c9d59cd23431f0c2ede8da258795790af325c64dacc63dc791a0682baad85348ddbd04ef954ad17ade370958a606b6f83e42ae4c8873eaea4bc11f4a409a'
7
- data.tar.gz: eb013fb3e45477510bf1cdf40fbfbba739be5cb3fda1f8366eb97c3898d31a7eb932a3c72c8a668e6e78f0ea57cc0d813207f50567168d0e6ea723ef09898533
6
+ metadata.gz: d02021cfb6dbf99651d5f1b8653394dd59f2c830b0a62b8c394b70b275030cd2c066a786a9cb1281df55029f9a69455c00a73a1763785cba3a5ccf8769766654
7
+ data.tar.gz: be05f291ccae55c3a6cce6ed2e5e79fa968682d856bcc502693408c69cc85b563017e8505a0459f6cb0d628bbdc57b58efd28146b460e09b45695652e6263145
@@ -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,15 +68,18 @@ module Console
66
68
  output.puts " #{terminal[:exception_detail]}#{line}#{terminal.reset}"
67
69
  end
68
70
 
69
- root_expr = /^#{@root}\// if @root
70
-
71
+ root_pattern = /^#{@root}\// if @root
72
+
71
73
  exception.backtrace&.each_with_index do |line, index|
72
74
  path, offset, message = line.split(":")
75
+ style = :exception_backtrace
73
76
 
74
77
  # Make the path a bit more readable
75
- path.sub!(root_expr, "./") if root_expr
76
-
77
- output.puts " #{index == 0 ? "→" : " "} #{terminal[:exception_backtrace]}#{path}:#{offset}#{terminal.reset} #{message}"
78
+ if root_pattern and path.sub!(root_pattern, "").nil?
79
+ style = :exception_backtrace_other
80
+ end
81
+
82
+ output.puts " #{index == 0 ? "→" : " "} #{terminal[style]}#{path}:#{offset}#{terminal[:exception_message]} #{message}#{terminal.reset}"
78
83
  end
79
84
 
80
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'
@@ -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
@@ -116,27 +117,5 @@ module Console
116
117
  return true
117
118
  end
118
119
  end
119
-
120
- def formatted_duration(duration)
121
- if duration < 60.0
122
- return "#{duration.round(2)}s"
123
- end
124
-
125
- duration /= 60.0
126
-
127
- if duration < 60.0
128
- return "#{duration.round}m"
129
- end
130
-
131
- duration /= 60.0
132
-
133
- if duration < 60.0
134
- return "#{duration.round(1)}h"
135
- end
136
-
137
- duration /= 24.0
138
-
139
- return "#{duration.round(1)}d"
140
- end
141
120
  end
142
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.2"
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.2
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: 2021-03-27 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