infobar 0.13.0 → 0.13.2

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.
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,
@@ -90,10 +154,32 @@ class Infobar
90
154
  self
91
155
  end
92
156
 
157
+ # Runs a busy indicator (spinner without a total) around a block.
158
+ #
159
+ # @param opts [Hash] keyword arguments, see #busy for the full list
160
+ # @yield the work to perform
161
+ # @return [Object] the result of the block
162
+ # @see #busy
93
163
  def self.busy(**opts, &block)
94
164
  instance.busy(**opts, &block)
95
165
  end
96
166
 
167
+ # Runs a busy indicator (spinner without a total) around a block.
168
+ #
169
+ # The block executes in a separate thread; the spinner updates
170
+ # independently until the block completes.
171
+ #
172
+ # @param frames [Symbol] the spinner animation (see +Infobar::Spinner+)
173
+ # @param opts [Hash] additional options forwarded to #call
174
+ # @option opts [Numeric] :sleep the sleep interval in seconds (default 0.1)
175
+ # @yield the work to perform
176
+ # @yieldreturn [Object] the return value of the block
177
+ # @return [Object] the result of the block
178
+ #
179
+ # @example
180
+ # Infobar.busy(label: 'Loading', frames: :braille1) {
181
+ # do_work
182
+ # }
97
183
  def busy(frames: :circle1, **opts, &block)
98
184
  block_given? or raise ArgumentError, 'block is required as an argument'
99
185
  duration = opts.delete(:sleep) || 0.1
@@ -113,10 +199,16 @@ class Infobar
113
199
  t.join
114
200
  r
115
201
  ensure
116
- finish
117
202
  ib&.kill
203
+ finish
204
+ newline
118
205
  end
119
206
 
207
+ # Resets the counter, message, and optionally the display.
208
+ #
209
+ # @param display [Boolean] whether to also clear the display
210
+ # (default true)
211
+ # @return [Infobar] self
120
212
  def reset(display: true)
121
213
  @message = convert_to_message('%l %c/%t in %te, ETA %e @%E %s')
122
214
  counter.reset(total: 0, current: 0)
@@ -124,12 +216,28 @@ class Infobar
124
216
  self
125
217
  end
126
218
 
219
+ # Renders an immediate update of the progress bar.
220
+ #
221
+ # @param message [String, Hash, Infobar::Message, nil] an optional
222
+ # new message format to render
223
+ # @param force [Boolean] bypass the frequency limiter (default true)
224
+ # @return [Infobar] self
127
225
  def update(message: nil, force: true)
128
226
  @message = convert_to_message(message)
129
227
  display.update(message: @message, counter: counter, force: force)
130
228
  self
131
229
  end
132
230
 
231
+ # Advances the progress counter and renders an update.
232
+ #
233
+ # @param by [Integer] the number of items to add (default 1)
234
+ # @param as [Symbol, String, nil] a named sub-progress category
235
+ # @param message [String, Hash, Infobar::Message, nil] an optional
236
+ # new message format
237
+ # @param finish [Boolean, String, Hash, nil] if truthy and the
238
+ # counter is done, calls #finish (default true)
239
+ # @param force [Boolean] bypass the frequency limiter (default false)
240
+ # @return [Infobar] self
133
241
  def progress(by: 1, as: nil, message: nil, finish: true, force: false)
134
242
  counter.progress(by: by, as: as)
135
243
  @message = convert_to_message(message)
@@ -138,6 +246,11 @@ class Infobar
138
246
  self
139
247
  end
140
248
 
249
+ # Marks the progress bar as finished and renders a final update.
250
+ #
251
+ # @param message [String, Hash, Infobar::Message, nil] an optional
252
+ # final message to display
253
+ # @return [Infobar] self
141
254
  def finish(message: nil)
142
255
  counter.finish
143
256
  @message = convert_to_message(message)
@@ -145,10 +258,22 @@ class Infobar
145
258
  self
146
259
  end
147
260
 
261
+ # Converts a message-like object to an +Infobar::Message+ (class form).
262
+ #
263
+ # @param message [String, Hash, Infobar::Message, Object]
264
+ # @return [Infobar::Message]
148
265
  def self.convert_to_message(message)
149
266
  instance.convert_to_message(message)
150
267
  end
151
268
 
269
+ # Converts a message-like object to an +Infobar::Message+.
270
+ #
271
+ # Accepts a +String+ (wraps in a format hash), a +Hash+ (passes to
272
+ # +Message.new+), an existing +Message+ (returns as-is), or anything
273
+ # else (returns the current @message).
274
+ #
275
+ # @param message [String, Hash, Infobar::Message, Object]
276
+ # @return [Infobar::Message]
152
277
  def convert_to_message(message)
153
278
  case message
154
279
  when Message
@@ -163,15 +288,42 @@ class Infobar
163
288
  end
164
289
  end
165
290
 
291
+ # Creates and configures a new infobar (top-level convenience method).
292
+ #
293
+ # @param opts [Hash] keyword arguments forwarded to +Infobar.call+
294
+ # @return [Infobar] the configured singleton
295
+ #
296
+ # @example
297
+ # Infobar(total: 42)
166
298
  def Infobar(**opts)
167
299
  Infobar.(**opts)
168
300
  end
169
301
 
170
302
  class ::Object
303
+ # Returns the +Infobar+ singleton instance.
304
+ #
305
+ # @return [Infobar]
171
306
  def infobar
172
307
  ::Infobar.instance
173
308
  end
174
309
 
310
+ # Iterates over the receiver with a progress bar.
311
+ #
312
+ # If +:total+ is given as a symbol, it is resolved via
313
+ # +__send__+. If +:total+ is not given, the receiver's +size+ is
314
+ # used.
315
+ #
316
+ # @param opts [Hash] options forwarded to +Infobar.call+
317
+ # @option opts [Integer, Symbol, nil] :total the total count or
318
+ # a method name to resolve it
319
+ # @yield each element of the receiver
320
+ # @return [Object] self
321
+ #
322
+ # @example
323
+ # (1..100).with_infobar.each { |i| process(i) }
324
+ #
325
+ # @example With a symbol total
326
+ # items.with_infobar(total: :count) { |item| save(item) }
175
327
  def with_infobar(**opts, &block)
176
328
  case
177
329
  when total = opts[:total].ask_and_send(:to_sym)
data/spec/infobar_spec.rb CHANGED
@@ -20,6 +20,7 @@ describe Infobar do
20
20
  it 'can be used to signal being busy with a block' do
21
21
  expect(infobar.display).to receive(:update).at_least(1).and_call_original
22
22
  expect(infobar).to receive(:finish).at_least(1).and_call_original
23
+ expect(infobar).to receive(:newline).at_least(1).and_call_original
23
24
  Infobar.busy { sleep 0.2 }
24
25
  end
25
26
 
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.13.0
4
+ version: 0.13.2
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.17.0
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.17.0
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.3
226
+ rubygems_version: 4.0.24
226
227
  specification_version: 4
227
228
  summary: Gem to display information about computations.
228
229
  test_files: