infobar 0.13.1 → 0.14.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.
- checksums.yaml +4 -4
- data/.github/workflows/static.yml +51 -0
- data/CHANGES.md +73 -0
- data/README.md +4 -0
- data/Rakefile +6 -2
- data/infobar.gemspec +5 -5
- data/lib/infobar/counter.rb +65 -0
- data/lib/infobar/display.rb +102 -5
- data/lib/infobar/duration.rb +16 -0
- data/lib/infobar/fancy_interface.rb +29 -0
- data/lib/infobar/fifo.rb +32 -1
- data/lib/infobar/frequency.rb +27 -0
- data/lib/infobar/input_output.rb +12 -0
- data/lib/infobar/message.rb +50 -0
- data/lib/infobar/number.rb +25 -0
- data/lib/infobar/rate.rb +22 -0
- data/lib/infobar/spinner.rb +22 -0
- data/lib/infobar/timer.rb +39 -0
- data/lib/infobar/trend.rb +17 -0
- data/lib/infobar/version.rb +7 -2
- data/lib/infobar.rb +175 -0
- data/spec/infobar/config_spec.rb +1 -1
- data/spec/infobar/display_spec.rb +22 -6
- data/spec/infobar/input_output_spec.rb +2 -0
- data/spec/infobar_spec.rb +5 -3
- metadata +5 -4
data/lib/infobar.rb
CHANGED
|
@@ -2,6 +2,24 @@ require 'tins/xt'
|
|
|
2
2
|
require 'complex_config'
|
|
3
3
|
require 'more_math'
|
|
4
4
|
|
|
5
|
+
# Singleton progress bar manager.
|
|
6
|
+
#
|
|
7
|
+
# +Infobar+ is the primary entry point for creating and controlling
|
|
8
|
+
# terminal progress bars. It encapsulates a +Counter+ (state), a
|
|
9
|
+
# +Display+ (rendering), and a +Message+ (formatting) and exposes a
|
|
10
|
+
# compact API for updating progress.
|
|
11
|
+
#
|
|
12
|
+
# @example Basic usage
|
|
13
|
+
# Infobar(total: 100)
|
|
14
|
+
# 100.times { +infobar }
|
|
15
|
+
#
|
|
16
|
+
# @example With a custom message
|
|
17
|
+
# Infobar(total: 50, message: '%l %c/%t %s')
|
|
18
|
+
#
|
|
19
|
+
# @example Busy indicator (no total)
|
|
20
|
+
# Infobar.busy(label: 'Thinking', frames: :braille1) {
|
|
21
|
+
# do_long_work
|
|
22
|
+
# }
|
|
5
23
|
class Infobar
|
|
6
24
|
end
|
|
7
25
|
|
|
@@ -21,46 +39,92 @@ class Infobar
|
|
|
21
39
|
prepend Infobar::InputOutput
|
|
22
40
|
include ComplexConfig::Provider::Shortcuts
|
|
23
41
|
|
|
42
|
+
# Initializes the singleton with a fresh counter and display.
|
|
24
43
|
def initialize
|
|
25
44
|
@counter = Infobar::Counter.new
|
|
26
45
|
@display = Infobar::Display.new
|
|
27
46
|
reset display: false
|
|
28
47
|
end
|
|
29
48
|
|
|
49
|
+
# @return [Infobar::Display] the singleton display instance
|
|
30
50
|
def self.display
|
|
31
51
|
infobar.display
|
|
32
52
|
end
|
|
33
53
|
|
|
54
|
+
# @!attribute [r] message
|
|
55
|
+
# @return [Infobar::Message] the active message format
|
|
34
56
|
attr_reader :message
|
|
35
57
|
|
|
58
|
+
# @!attribute [r] counter
|
|
59
|
+
# @return [Infobar::Counter] the progress state tracker
|
|
36
60
|
attr_reader :counter
|
|
37
61
|
|
|
62
|
+
# @!attribute [r] display
|
|
63
|
+
# @return [Infobar::Display] the rendering output handler
|
|
38
64
|
attr_reader :display
|
|
39
65
|
|
|
66
|
+
# @!attribute [rw] label
|
|
67
|
+
# @return [String] the label shown in the progress bar
|
|
40
68
|
attr_accessor :label
|
|
41
69
|
|
|
70
|
+
# Whether the counter has started tracking time.
|
|
71
|
+
#
|
|
72
|
+
# @return [Boolean]
|
|
42
73
|
delegate :started?, to: :counter
|
|
43
74
|
|
|
75
|
+
# Whether the current count has reached the total.
|
|
76
|
+
#
|
|
77
|
+
# @return [Boolean]
|
|
44
78
|
delegate :done?, to: :counter
|
|
45
79
|
|
|
80
|
+
# Whether the bar has been explicitly finished.
|
|
81
|
+
#
|
|
82
|
+
# @return [Boolean]
|
|
46
83
|
delegate :finished?, to: :counter
|
|
47
84
|
|
|
85
|
+
# Whether the display is currently visible.
|
|
86
|
+
#
|
|
87
|
+
# @return [Boolean]
|
|
48
88
|
delegate :show?, to: :display
|
|
49
89
|
|
|
90
|
+
# Sets whether the display is visible.
|
|
50
91
|
delegate :show=, to: :display
|
|
51
92
|
|
|
93
|
+
# Merges new style options into the display style.
|
|
52
94
|
delegate :style=, to: :display
|
|
53
95
|
|
|
96
|
+
# Sets per-`as:` style overrides for the display.
|
|
54
97
|
delegate :as_styles=, to: :display
|
|
55
98
|
|
|
99
|
+
# Sets the input IO for the display.
|
|
56
100
|
delegate :input=, to: :display
|
|
57
101
|
|
|
102
|
+
# Sets the output IO for the display.
|
|
58
103
|
delegate :output=, to: :display
|
|
59
104
|
|
|
105
|
+
# Configures and activates the progress bar.
|
|
106
|
+
#
|
|
107
|
+
# @param opts [Hash] keyword arguments, see #call for the full list
|
|
108
|
+
# @return [Infobar] self
|
|
109
|
+
# @see #call
|
|
60
110
|
def self.call(**opts)
|
|
61
111
|
instance.call(**opts)
|
|
62
112
|
end
|
|
63
113
|
|
|
114
|
+
# Configures and activates the progress bar (instance form).
|
|
115
|
+
#
|
|
116
|
+
# @param total [Integer, Float] the total number of items
|
|
117
|
+
# @param current [Integer] the starting progress value
|
|
118
|
+
# @param label [String] the bar label
|
|
119
|
+
# @param message [Hash, Infobar::Message, nil] the message format
|
|
120
|
+
# @param show [Boolean, nil] whether the bar is visible
|
|
121
|
+
# @param style [Hash, nil] display style overrides
|
|
122
|
+
# @param as_styles [Hash, nil] per-`as:` style overrides
|
|
123
|
+
# @param frequency [Numeric, nil] update frequency in seconds
|
|
124
|
+
# @param update [Boolean] force an immediate update
|
|
125
|
+
# @param input [IO] the input IO
|
|
126
|
+
# @param output [IO] the output IO
|
|
127
|
+
# @return [Infobar] self
|
|
64
128
|
def call(
|
|
65
129
|
total:,
|
|
66
130
|
current: 0,
|
|
@@ -87,13 +151,36 @@ class Infobar
|
|
|
87
151
|
self.input = input
|
|
88
152
|
self.output = output
|
|
89
153
|
update and update(message: @message, force: true)
|
|
154
|
+
install_winch_trap
|
|
90
155
|
self
|
|
91
156
|
end
|
|
92
157
|
|
|
158
|
+
# Runs a busy indicator (spinner without a total) around a block.
|
|
159
|
+
#
|
|
160
|
+
# @param opts [Hash] keyword arguments, see #busy for the full list
|
|
161
|
+
# @yield the work to perform
|
|
162
|
+
# @return [Object] the result of the block
|
|
163
|
+
# @see #busy
|
|
93
164
|
def self.busy(**opts, &block)
|
|
94
165
|
instance.busy(**opts, &block)
|
|
95
166
|
end
|
|
96
167
|
|
|
168
|
+
# Runs a busy indicator (spinner without a total) around a block.
|
|
169
|
+
#
|
|
170
|
+
# The block executes in a separate thread; the spinner updates
|
|
171
|
+
# independently until the block completes.
|
|
172
|
+
#
|
|
173
|
+
# @param frames [Symbol] the spinner animation (see +Infobar::Spinner+)
|
|
174
|
+
# @param opts [Hash] additional options forwarded to #call
|
|
175
|
+
# @option opts [Numeric] :sleep the sleep interval in seconds (default 0.1)
|
|
176
|
+
# @yield the work to perform
|
|
177
|
+
# @yieldreturn [Object] the return value of the block
|
|
178
|
+
# @return [Object] the result of the block
|
|
179
|
+
#
|
|
180
|
+
# @example
|
|
181
|
+
# Infobar.busy(label: 'Loading', frames: :braille1) {
|
|
182
|
+
# do_work
|
|
183
|
+
# }
|
|
97
184
|
def busy(frames: :circle1, **opts, &block)
|
|
98
185
|
block_given? or raise ArgumentError, 'block is required as an argument'
|
|
99
186
|
duration = opts.delete(:sleep) || 0.1
|
|
@@ -118,6 +205,11 @@ class Infobar
|
|
|
118
205
|
newline
|
|
119
206
|
end
|
|
120
207
|
|
|
208
|
+
# Resets the counter, message, and optionally the display.
|
|
209
|
+
#
|
|
210
|
+
# @param display [Boolean] whether to also clear the display
|
|
211
|
+
# (default true)
|
|
212
|
+
# @return [Infobar] self
|
|
121
213
|
def reset(display: true)
|
|
122
214
|
@message = convert_to_message('%l %c/%t in %te, ETA %e @%E %s')
|
|
123
215
|
counter.reset(total: 0, current: 0)
|
|
@@ -125,12 +217,28 @@ class Infobar
|
|
|
125
217
|
self
|
|
126
218
|
end
|
|
127
219
|
|
|
220
|
+
# Renders an immediate update of the progress bar.
|
|
221
|
+
#
|
|
222
|
+
# @param message [String, Hash, Infobar::Message, nil] an optional
|
|
223
|
+
# new message format to render
|
|
224
|
+
# @param force [Boolean] bypass the frequency limiter (default true)
|
|
225
|
+
# @return [Infobar] self
|
|
128
226
|
def update(message: nil, force: true)
|
|
129
227
|
@message = convert_to_message(message)
|
|
130
228
|
display.update(message: @message, counter: counter, force: force)
|
|
131
229
|
self
|
|
132
230
|
end
|
|
133
231
|
|
|
232
|
+
# Advances the progress counter and renders an update.
|
|
233
|
+
#
|
|
234
|
+
# @param by [Integer] the number of items to add (default 1)
|
|
235
|
+
# @param as [Symbol, String, nil] a named sub-progress category
|
|
236
|
+
# @param message [String, Hash, Infobar::Message, nil] an optional
|
|
237
|
+
# new message format
|
|
238
|
+
# @param finish [Boolean, String, Hash, nil] if truthy and the
|
|
239
|
+
# counter is done, calls #finish (default true)
|
|
240
|
+
# @param force [Boolean] bypass the frequency limiter (default false)
|
|
241
|
+
# @return [Infobar] self
|
|
134
242
|
def progress(by: 1, as: nil, message: nil, finish: true, force: false)
|
|
135
243
|
counter.progress(by: by, as: as)
|
|
136
244
|
@message = convert_to_message(message)
|
|
@@ -139,17 +247,35 @@ class Infobar
|
|
|
139
247
|
self
|
|
140
248
|
end
|
|
141
249
|
|
|
250
|
+
# Marks the progress bar as finished and renders a final update.
|
|
251
|
+
#
|
|
252
|
+
# @param message [String, Hash, Infobar::Message, nil] an optional
|
|
253
|
+
# final message to display
|
|
254
|
+
# @return [Infobar] self
|
|
142
255
|
def finish(message: nil)
|
|
143
256
|
counter.finish
|
|
144
257
|
@message = convert_to_message(message)
|
|
145
258
|
display.update(message: @message, force: true, counter: counter)
|
|
259
|
+
restore_winch_trap
|
|
146
260
|
self
|
|
147
261
|
end
|
|
148
262
|
|
|
263
|
+
# Converts a message-like object to an +Infobar::Message+ (class form).
|
|
264
|
+
#
|
|
265
|
+
# @param message [String, Hash, Infobar::Message, Object]
|
|
266
|
+
# @return [Infobar::Message]
|
|
149
267
|
def self.convert_to_message(message)
|
|
150
268
|
instance.convert_to_message(message)
|
|
151
269
|
end
|
|
152
270
|
|
|
271
|
+
# Converts a message-like object to an +Infobar::Message+.
|
|
272
|
+
#
|
|
273
|
+
# Accepts a +String+ (wraps in a format hash), a +Hash+ (passes to
|
|
274
|
+
# +Message.new+), an existing +Message+ (returns as-is), or anything
|
|
275
|
+
# else (returns the current @message).
|
|
276
|
+
#
|
|
277
|
+
# @param message [String, Hash, Infobar::Message, Object]
|
|
278
|
+
# @return [Infobar::Message]
|
|
153
279
|
def convert_to_message(message)
|
|
154
280
|
case message
|
|
155
281
|
when Message
|
|
@@ -162,17 +288,66 @@ class Infobar
|
|
|
162
288
|
@message
|
|
163
289
|
end
|
|
164
290
|
end
|
|
291
|
+
|
|
292
|
+
private
|
|
293
|
+
|
|
294
|
+
# Installs a `SIGWINCH` handler that clears the display so the
|
|
295
|
+
# bar re-saves its position after a terminal resize.
|
|
296
|
+
#
|
|
297
|
+
# @api private
|
|
298
|
+
def install_winch_trap
|
|
299
|
+
@winch_handler ||= Signal.trap('WINCH', 'DEFAULT')
|
|
300
|
+
Signal.trap('WINCH') do
|
|
301
|
+
display.position_saved = false if display.show?
|
|
302
|
+
end
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# Restores the original `SIGWINCH` handler saved by
|
|
306
|
+
# #install_winch_trap.
|
|
307
|
+
#
|
|
308
|
+
# @api private
|
|
309
|
+
def restore_winch_trap
|
|
310
|
+
Signal.trap('WINCH', @winch_handler) if @winch_handler
|
|
311
|
+
@winch_handler = nil
|
|
312
|
+
end
|
|
165
313
|
end
|
|
166
314
|
|
|
315
|
+
# Creates and configures a new infobar (top-level convenience method).
|
|
316
|
+
#
|
|
317
|
+
# @param opts [Hash] keyword arguments forwarded to +Infobar.call+
|
|
318
|
+
# @return [Infobar] the configured singleton
|
|
319
|
+
#
|
|
320
|
+
# @example
|
|
321
|
+
# Infobar(total: 42)
|
|
167
322
|
def Infobar(**opts)
|
|
168
323
|
Infobar.(**opts)
|
|
169
324
|
end
|
|
170
325
|
|
|
171
326
|
class ::Object
|
|
327
|
+
# Returns the +Infobar+ singleton instance.
|
|
328
|
+
#
|
|
329
|
+
# @return [Infobar]
|
|
172
330
|
def infobar
|
|
173
331
|
::Infobar.instance
|
|
174
332
|
end
|
|
175
333
|
|
|
334
|
+
# Iterates over the receiver with a progress bar.
|
|
335
|
+
#
|
|
336
|
+
# If +:total+ is given as a symbol, it is resolved via
|
|
337
|
+
# +__send__+. If +:total+ is not given, the receiver's +size+ is
|
|
338
|
+
# used.
|
|
339
|
+
#
|
|
340
|
+
# @param opts [Hash] options forwarded to +Infobar.call+
|
|
341
|
+
# @option opts [Integer, Symbol, nil] :total the total count or
|
|
342
|
+
# a method name to resolve it
|
|
343
|
+
# @yield each element of the receiver
|
|
344
|
+
# @return [Object] self
|
|
345
|
+
#
|
|
346
|
+
# @example
|
|
347
|
+
# (1..100).with_infobar.each { |i| process(i) }
|
|
348
|
+
#
|
|
349
|
+
# @example With a symbol total
|
|
350
|
+
# items.with_infobar(total: :count) { |item| save(item) }
|
|
176
351
|
def with_infobar(**opts, &block)
|
|
177
352
|
case
|
|
178
353
|
when total = opts[:total].ask_and_send(:to_sym)
|
data/spec/infobar/config_spec.rb
CHANGED
|
@@ -19,7 +19,7 @@ describe 'Infobar config' do
|
|
|
19
19
|
|
|
20
20
|
it 'configures default infobar' do
|
|
21
21
|
Infobar(total: 23)
|
|
22
|
-
expect(infobar.display.output).to receive(:<<).with(
|
|
22
|
+
expect(infobar.display.output).to receive(:<<).with("\e[s")
|
|
23
23
|
expect(infobar.display.output).to receive(:<<).with(
|
|
24
24
|
"\e[38;5;22m\e[48;5;40m\e[0m\e[0m"
|
|
25
25
|
)
|
|
@@ -21,13 +21,29 @@ describe Infobar::Display do
|
|
|
21
21
|
it 'can be updated' do
|
|
22
22
|
display.update message: 'test', counter: counter
|
|
23
23
|
expect(output.tap(&:rewind).read).to eq\
|
|
24
|
-
"\
|
|
24
|
+
"\e[s\e[38;5;22m\e[48;5;40m░░░░░░░░░░░░░░░░░░te\e[0m\e[0m\e[38;5;40m\e[48;5;22mst \e[0m\e[0m"
|
|
25
25
|
end
|
|
26
26
|
|
|
27
|
-
it 'update
|
|
28
|
-
expect(display).to receive(:carriage_return).and_call_original
|
|
27
|
+
it 'update saves position first' do
|
|
29
28
|
display.update message: 'test', counter: counter
|
|
30
|
-
expect(output.tap(&:rewind).read).to start_with
|
|
29
|
+
expect(output.tap(&:rewind).read).to start_with "\e[s"
|
|
30
|
+
display.update message: 'test', counter: counter, force: true
|
|
31
|
+
output.rewind
|
|
32
|
+
expect(output.read).to include "\e[u"
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
it 're-saves position after clear and write' do
|
|
36
|
+
display.update message: 'test', counter: counter
|
|
37
|
+
output.rewind
|
|
38
|
+
|
|
39
|
+
display.clear
|
|
40
|
+
output << "hello\n"
|
|
41
|
+
display.save_position!
|
|
42
|
+
|
|
43
|
+
display.update message: 'test', counter: counter, force: true
|
|
44
|
+
content = output.tap(&:rewind).read
|
|
45
|
+
expect(content).to include "\e[u"
|
|
46
|
+
expect(content).not_to include "\e[s" + "\e[s"
|
|
31
47
|
end
|
|
32
48
|
|
|
33
49
|
it 'reset clears and sets to default style' do
|
|
@@ -53,7 +69,7 @@ describe Infobar::Display do
|
|
|
53
69
|
display.style = test_style
|
|
54
70
|
display.update message: 'test', counter: counter
|
|
55
71
|
expect(output.tap(&:rewind).read).to eq\
|
|
56
|
-
"\
|
|
72
|
+
"\e[s\e[38;5;1m\e[48;5;9mXXXXXXXXXXXXXXXXXXte\e[0m\e[0m\e[38;5;0m\e[48;5;8mstOOOOOOOOOOOOOOOOOO\e[0m\e[0m"
|
|
57
73
|
expect(display.style).to eq(test_style)
|
|
58
74
|
end
|
|
59
75
|
|
|
@@ -79,7 +95,7 @@ describe Infobar::Display do
|
|
|
79
95
|
).at_least(:once)
|
|
80
96
|
display.update message: 'test', counter: counter
|
|
81
97
|
expect(output.tap(&:rewind).read).to eq\
|
|
82
|
-
"\
|
|
98
|
+
"\e[s\e[38;5;1m\e[48;5;9mXXXXXXXXXXXXXXXXX\e[0m\e[0m\e[38;5;0m\e[48;5;10m*test******\e[0m\e[0m\e[38;5;40m\e[48;5;22m \e[0m\e[0m"
|
|
83
99
|
end
|
|
84
100
|
|
|
85
101
|
it 'can output newline' do
|
|
@@ -21,12 +21,14 @@ describe Infobar::InputOutput do
|
|
|
21
21
|
it 'can puts after clearing' do
|
|
22
22
|
expect(infobar.display).to receive(:clear)
|
|
23
23
|
expect(infobar.display.output).to receive(:puts)
|
|
24
|
+
expect(infobar.display).to receive(:save_position!)
|
|
24
25
|
infobar.puts 'hello'
|
|
25
26
|
end
|
|
26
27
|
|
|
27
28
|
it 'can gets after clearing' do
|
|
28
29
|
expect(infobar.display).to receive(:clear)
|
|
29
30
|
expect(infobar.display.input).to receive(:gets)
|
|
31
|
+
expect(infobar.display).to receive(:save_position!)
|
|
30
32
|
infobar.gets
|
|
31
33
|
end
|
|
32
34
|
|
data/spec/infobar_spec.rb
CHANGED
|
@@ -18,13 +18,15 @@ describe Infobar do
|
|
|
18
18
|
|
|
19
19
|
describe '.busy' do
|
|
20
20
|
it 'can be used to signal being busy with a block' do
|
|
21
|
-
expect(infobar.display).to receive(:update).at_least(1)
|
|
22
|
-
expect(infobar).to receive(:finish).at_least(1)
|
|
23
|
-
expect(infobar).to receive(:newline).at_least(1)
|
|
21
|
+
expect(infobar.display).to receive(:update).at_least(1)
|
|
22
|
+
expect(infobar).to receive(:finish).at_least(1)
|
|
23
|
+
expect(infobar).to receive(:newline).at_least(1)
|
|
24
24
|
Infobar.busy { sleep 0.2 }
|
|
25
25
|
end
|
|
26
26
|
|
|
27
27
|
it 'needs a block for busy display' do
|
|
28
|
+
expect(infobar).to receive(:newline).at_least(1)
|
|
29
|
+
expect(infobar).to receive(:finish).at_least(1)
|
|
28
30
|
expect { Infobar.busy }.to raise_error ArgumentError
|
|
29
31
|
end
|
|
30
32
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: infobar
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.14.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Florian Frank
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: 2.
|
|
18
|
+
version: 2.21.0
|
|
19
19
|
type: :development
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: 2.
|
|
25
|
+
version: 2.21.0
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: simplecov
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -164,6 +164,7 @@ extra_rdoc_files:
|
|
|
164
164
|
- lib/infobar/trend.rb
|
|
165
165
|
- lib/infobar/version.rb
|
|
166
166
|
files:
|
|
167
|
+
- ".github/workflows/static.yml"
|
|
167
168
|
- CHANGES.md
|
|
168
169
|
- COPYING
|
|
169
170
|
- Gemfile
|
|
@@ -222,7 +223,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
222
223
|
- !ruby/object:Gem::Version
|
|
223
224
|
version: '0'
|
|
224
225
|
requirements: []
|
|
225
|
-
rubygems_version: 4.0.
|
|
226
|
+
rubygems_version: 4.0.24
|
|
226
227
|
specification_version: 4
|
|
227
228
|
summary: Gem to display information about computations.
|
|
228
229
|
test_files:
|