infobar 0.13.1 → 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.
@@ -1,18 +1,39 @@
1
+ # Throttles execution to a fixed time interval.
2
+ #
3
+ # Tracks the timestamp of the last invocation and only allows a
4
+ # block to run when the configured +duration+ has elapsed since
5
+ # the previous call.
1
6
  class Infobar::Frequency
7
+ # Initializes a new Frequency gate.
8
+ #
9
+ # @param duration [Numeric] minimum seconds between calls
2
10
  def initialize(duration)
3
11
  @duration = duration.to_f
4
12
  @called = 0
5
13
  end
6
14
 
15
+ # The configured minimum interval in seconds.
16
+ #
17
+ # @return [Float]
7
18
  attr_reader :duration
8
19
 
20
+ # The number of times the block has been permitted to run.
21
+ #
22
+ # @return [Integer]
9
23
  attr_reader :called
10
24
 
25
+ # Records the current (or given) timestamp and increments the
26
+ # call counter.
27
+ #
28
+ # @param now [Time] the reference time (defaults to +Time.now+)
11
29
  def update(now: Time.now)
12
30
  @update = now
13
31
  @called += 1
14
32
  end
15
33
 
34
+ # Executes the block if the frequency threshold has been met.
35
+ #
36
+ # @yield The block to execute when the interval has elapsed
16
37
  def call(&block)
17
38
  now = Time.now
18
39
  if !@update || now - @update > @duration
@@ -21,12 +42,18 @@ class Infobar::Frequency
21
42
  end
22
43
  end
23
44
 
45
+ # Resets the internal state so the next call is always permitted.
46
+ #
47
+ # @return [Infobar::Frequency] self
24
48
  def reset
25
49
  @update = nil
26
50
  @called = 0
27
51
  self
28
52
  end
29
53
 
54
+ # Returns the configured duration as a string.
55
+ #
56
+ # @return [String]
30
57
  def to_s
31
58
  @duration.to_s
32
59
  end
@@ -1,9 +1,19 @@
1
+ # Prepended into +Infobar+ to intercept standard I/O methods so the
2
+ # progress bar is cleared before each write and the display IO is
3
+ # used as the target.
4
+ #
5
+ # Defines +print+, +printf+, +putc+, +puts+, +gets+, +readline+,
6
+ # +readlines+, +newline+, and +clear+ that all clear the bar first.
1
7
  module Infobar::InputOutput
8
+ # Clears the bar and writes a newline to the display output.
9
+ # @return [Infobar] the infobar instance
2
10
  def newline
3
11
  display.newline
4
12
  self
5
13
  end
6
14
 
15
+ # Clears the entire progress bar line.
16
+ # @return [Infobar] the infobar instance
7
17
  def clear
8
18
  display.clear
9
19
  self
@@ -3,10 +3,32 @@ require 'infobar/duration'
3
3
  require 'infobar/number'
4
4
  require 'infobar/rate'
5
5
 
6
+ # Renders a progress bar message from a format string containing
7
+ # registered `%`-directives.
8
+ #
9
+ # Directives like +%c+, +%t+, +%e+, +%s+ are resolved at render
10
+ # time by calling their registered blocks. Custom directives can
11
+ # be added via +Message.register+.
12
+ #
13
+ # @example Basic usage
14
+ # msg = Infobar::Message.new(format: '%l %c/%t ETA %e %s')
15
+ # msg.to_str # => "Infobar 12/100 ETA 00:45 |"
6
16
  class Infobar::Message
7
17
  class << self
8
18
  prepend Tins::Delegate
9
19
 
20
+ # Registers a new `%`-directive for use in message format strings.
21
+ #
22
+ # @param directive [String] the directive key, e.g. +'%c'+
23
+ # @param opts [Hash] default options merged into each invocation
24
+ # @yield [directive, opts] the block that produces the replacement
25
+ # string for this directive
26
+ # @return [Infobar::Message] self for chaining
27
+ #
28
+ # @example Register a custom directive
29
+ # Message.register('%x', format: '%d') do |d, opts|
30
+ # Infobar::Number.new(some_value, **opts)
31
+ # end
10
32
  def register(directive, **opts, &block)
11
33
  directives.key?(directive) and
12
34
  warn "Overwriting old directive #{directive}."
@@ -15,10 +37,12 @@ class Infobar::Message
15
37
  self
16
38
  end
17
39
 
40
+ # @return [Hash] the registry of all registered directives
18
41
  def directives
19
42
  @directives ||= {}
20
43
  end
21
44
 
45
+ # @return [Hash] default options keyed by directive
22
46
  def directive_default_options
23
47
  @directive_default_options ||= {}
24
48
  end
@@ -116,24 +140,45 @@ class Infobar::Message
116
140
  ?%
117
141
  end
118
142
 
143
+ # Initializes a new Message with the given options.
144
+ #
145
+ # @param opts [Hash] message options; must contain a +'format'+
146
+ # key with the format string. Additional keys are per-directive
147
+ # option overrides (stringified keys).
148
+ #
149
+ # @example
150
+ # Message.new(format: '%l %c/%t', '%c' => { format: '%03d' })
119
151
  def initialize(opts)
120
152
  @opts = opts.each_with_object({}) { |(k, v), h| h[k.to_s] = v }
121
153
  end
122
154
 
155
+ # @!attribute [r] opts
156
+ # @return [Hash] the raw options hash
123
157
  attr_reader :opts
124
158
 
159
+ # @return [Hash] the class-level directive registry
125
160
  def directives
126
161
  self.class.directives
127
162
  end
128
163
 
164
+ # @return [Hash] the class-level default options registry
129
165
  def directive_default_options
130
166
  self.class.directive_default_options
131
167
  end
132
168
 
169
+ # Resolves options for a given directive, falling back to the
170
+ # registered defaults.
171
+ #
172
+ # @param directive [String] the directive key
173
+ # @return [Hash] the effective options for this directive
133
174
  def opts_for(directive)
134
175
  @opts.fetch(directive, directive_default_options[directive])
135
176
  end
136
177
 
178
+ # Renders the format string, replacing all directives with their
179
+ # resolved values.
180
+ #
181
+ # @return [String] the fully rendered message
137
182
  def to_str
138
183
  keys = directives.keys.sort_by { |k| -k.size }
139
184
  format.gsub(/(?<!%)(#{keys * ?|})/) do
@@ -141,12 +186,17 @@ class Infobar::Message
141
186
  end
142
187
  end
143
188
 
189
+ # @return [String, nil] the raw format string
144
190
  def format
145
191
  @opts['format']
146
192
  end
147
193
 
148
194
  alias to_s format
149
195
 
196
+ # Returns the options as a symbolized hash with +:format+ as the
197
+ # format key.
198
+ #
199
+ # @return [Hash]
150
200
  def to_hash
151
201
  result = @opts.dup
152
202
  result[:format] = result.delete('format')
@@ -1,4 +1,26 @@
1
+ # A formatted numeric string supporting unit-aware and plain formats.
2
+ #
3
+ # When the format contains +%U+, the value is rendered through
4
+ # +Tins::Unit#+format with the given unit and prefix. Otherwise a
5
+ # standard C-style format string is applied.
6
+ #
7
+ # @example Plain integer with thousands separator
8
+ # Infobar::Number.new(1234567, format: '%d', separate: ?_).to_s
9
+ # # => "1_234_567"
10
+ #
11
+ # @example Unit-aware rate
12
+ # Infobar::Number.new(1500.0, format: '%.3f%U%t', unit: 'i/s').to_s
1
13
  class Infobar::Number
14
+ # Initializes a new Number formatter.
15
+ #
16
+ # @param value [Numeric] the value to format
17
+ # @param format [String, nil] a C-style format string; if it
18
+ # contains +%U+ the value is unit-formatted via +Tins::Unit+
19
+ # @param unit [String, nil] the unit label (default +'i/s'+ when
20
+ # using +%U+)
21
+ # @param prefix [Integer] the unit prefix divisor (default 1000)
22
+ # @param separate [String, nil] thousands separator inserted into
23
+ # integer values (e.g. +'?_'++)
2
24
  def initialize(value, format: nil, unit: nil, prefix: 1000, separate: nil)
3
25
  case format
4
26
  when /%U/
@@ -14,6 +36,9 @@ class Infobar::Number
14
36
  end
15
37
  end
16
38
 
39
+ # Returns the pre-formatted number string.
40
+ #
41
+ # @return [String]
17
42
  def to_s
18
43
  @string
19
44
  end
data/lib/infobar/rate.rb CHANGED
@@ -1,6 +1,17 @@
1
1
  require 'infobar/trend'
2
2
 
3
+ # Formats a processing rate with optional unit scaling and a trend
4
+ # arrow derived from recent FIFO samples.
3
5
  class Infobar::Rate
6
+ # Initializes a new Rate formatter.
7
+ #
8
+ # @param value [Numeric] the rate value to format
9
+ # @param fifo_values [Array<Numeric>] recent rate samples used to
10
+ # compute the trend arrow
11
+ # @param opts [Hash] formatting options
12
+ # @option opts [String] :format the format string (default
13
+ # +'%.3f%U%t'+); +%U+ triggers unit formatting, +%t+ is
14
+ # replaced by the trend symbol
4
15
  def initialize(value, fifo_values = [], **opts)
5
16
  opts[:format] ||= '%.3f%U%t'
6
17
  opts[:format] = add_trend(opts[:format], fifo_values)
@@ -13,12 +24,23 @@ class Infobar::Rate
13
24
  end.to_s
14
25
  end
15
26
 
27
+ # Returns the formatted rate string.
28
+ #
29
+ # @return [String]
16
30
  def to_s
17
31
  @string
18
32
  end
19
33
 
20
34
  private
21
35
 
36
+ # Replaces the +%t+ placeholder in the format string with a trend
37
+ # arrow (↘, →, or ↗) computed from the FIFO values, or with ⤿ if
38
+ # no values are available.
39
+ #
40
+ # @api private
41
+ # @param format [String] the format string containing +%t+
42
+ # @param fifo_values [Array<Numeric>] the rate samples
43
+ # @return [String] the format string with the trend symbol
22
44
  def add_trend(format, fifo_values)
23
45
  if fifo_values.empty?
24
46
  format.gsub('%t', ?⤿)
@@ -1,4 +1,11 @@
1
+ # A terminal spinner that cycles through a set of frame strings.
2
+ #
3
+ # Supports custom frame arrays or any of the built-in +PREDEFINED+
4
+ # animations (e.g. pipe, braille, cylon, pacman).
1
5
  class Infobar::Spinner
6
+ # Built-in frame sets keyed by symbol.
7
+ #
8
+ # @api private
2
9
  PREDEFINED = {
3
10
  pipe: %w[ | / – \\ ],
4
11
  arrow: %w[ ↑ ↗ → ↘ ↓ ↙ ← ↖ ],
@@ -25,6 +32,13 @@ class Infobar::Spinner
25
32
  hourglass: %w[ ⏳ ⌛ ],
26
33
  }
27
34
 
35
+ # Initializes a new Spinner.
36
+ #
37
+ # @param frames [Array<String>, Symbol, nil] a custom frame array,
38
+ # a +PREDEFINED+ key (e.g. +:pipe+), or +nil+ for the default
39
+ # pipe animation
40
+ # @raise [KeyError] if a +Symbol+ is given that is not in
41
+ # +PREDEFINED+
28
42
  def initialize(frames = nil)
29
43
  @frames =
30
44
  case frames
@@ -39,6 +53,11 @@ class Infobar::Spinner
39
53
  end
40
54
  end
41
55
 
56
+ # Advances the spinner to the frame at the given index.
57
+ #
58
+ # @param count [Integer, :random] the frame index (wrapped
59
+ # modulo the frame count) or +:random+ for a random frame
60
+ # @return [Infobar::Spinner] self
42
61
  def spin(count)
43
62
  @string =
44
63
  if count == :random
@@ -49,6 +68,9 @@ class Infobar::Spinner
49
68
  self
50
69
  end
51
70
 
71
+ # Returns the current frame string.
72
+ #
73
+ # @return [String]
52
74
  def to_s
53
75
  @string
54
76
  end
data/lib/infobar/timer.rb CHANGED
@@ -1,18 +1,44 @@
1
1
  require 'infobar/fifo'
2
2
 
3
+ # Tracks processing rate using a Welford-style online mean.
4
+ #
5
+ # Accepts time-and-count samples, maintains a running average of
6
+ # per-item duration, and pushes each new rate into a 30-slot
7
+ # +FIFO+ for trend detection.
3
8
  class Infobar::Timer
9
+ # Initializes a new Timer with zeroed statistics and an empty
10
+ # 30-slot FIFO.
4
11
  def initialize
5
12
  @n = 0
6
13
  @x = 0.0
7
14
  @fifo = Infobar::FIFO.new(30)
8
15
  end
9
16
 
17
+ # The running online mean of per-item time (seconds).
18
+ #
19
+ # @return [Float]
10
20
  attr_reader :x
11
21
 
22
+ # The number of samples fed into the online mean.
23
+ #
24
+ # @return [Integer]
12
25
  attr_reader :n
13
26
 
27
+ # The 30-slot +FIFO+ of recent rate samples.
28
+ #
29
+ # @return [Infobar::FIFO]
14
30
  attr_reader :fifo
15
31
 
32
+ # Records a processing sample.
33
+ #
34
+ # On the first call it primes the timer; on the second call it
35
+ # seeds the mean with two samples (including the gap between
36
+ # calls); subsequent calls push a single rate sample.
37
+ #
38
+ # @param time [Time] the timestamp of this sample
39
+ # @param count [Integer] the number of items processed since the
40
+ # last call
41
+ # @return [Infobar::Timer] self
16
42
  def add(time, count)
17
43
  case @n
18
44
  when 0
@@ -30,6 +56,10 @@ class Infobar::Timer
30
56
  self
31
57
  end
32
58
 
59
+ # The current items-per-second rate.
60
+ #
61
+ # @return [Float] 1.0 divided by the average per-item time, or
62
+ # 0.0 if no samples exist
33
63
  def rate
34
64
  if @x.zero?
35
65
  0.0
@@ -38,12 +68,21 @@ class Infobar::Timer
38
68
  end
39
69
  end
40
70
 
71
+ # The average time per item in seconds.
72
+ #
73
+ # @return [Float]
41
74
  def average_time
42
75
  @x
43
76
  end
44
77
 
45
78
  protected
46
79
 
80
+ # Pushes a single per-item time sample into the online mean and
81
+ # the rate FIFO.
82
+ #
83
+ # @api protected
84
+ # @param x [Float] the per-item duration in seconds
85
+ # @return [Infobar::Timer] self
47
86
  def <<(x)
48
87
  @n +=1
49
88
  @x += (x - @x) / @n
data/lib/infobar/trend.rb CHANGED
@@ -1,16 +1,33 @@
1
+ # Determines a trend direction (up, flat, down) from a series of
2
+ # values using linear regression.
3
+ #
4
+ # @example Trend from a FIFO of rate samples
5
+ # Infobar::Trend.new([1.0, 1.1, 1.2]).to_s
6
+ # # => "↗"
1
7
  class Infobar::Trend
8
+ # Initializes a new Trend indicator.
9
+ #
10
+ # @param values [Array<Numeric>] the data series to analyze
11
+ # @param symbols [Array<Symbol>] the three symbols for down,
12
+ # flat, and up (default +[↘, →, ↗]+)
2
13
  def initialize(values, symbols: %i[ ↘ → ↗ ])
3
14
  @values = values
4
15
  @symbols = symbols
5
16
  @string = arrow
6
17
  end
7
18
 
19
+ # Returns the trend arrow as a string.
20
+ #
21
+ # @return [String] one of the configured symbols
8
22
  def to_s
9
23
  @string
10
24
  end
11
25
 
12
26
  private
13
27
 
28
+ # Computes the arrow by fitting a linear regression on the values.
29
+ #
30
+ # @return [String] the matching symbol from +@symbols+
14
31
  def arrow
15
32
  lr = MoreMath::LinearRegression.new(@values)
16
33
  case
@@ -1,6 +1,11 @@
1
+ # Version information for the Infobar gem.
2
+ #
3
+ # This class is auto-generated by GemHadar and exposes the
4
+ # current release version as a set of constants. Do not edit it
5
+ # by hand; it is rewritten on every `rake version` invocation.
1
6
  class Infobar
2
- # Infobar version
3
- VERSION = '0.13.1'
7
+ # The current version of the Infobar gem as a String.
8
+ VERSION = '0.13.2'
4
9
  VERSION_ARRAY = VERSION.split('.').map(&:to_i) # :nodoc:
5
10
  VERSION_MAJOR = VERSION_ARRAY[0] # :nodoc:
6
11
  VERSION_MINOR = VERSION_ARRAY[1] # :nodoc:
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
@@ -118,6 +204,11 @@ class Infobar
118
204
  newline
119
205
  end
120
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
121
212
  def reset(display: true)
122
213
  @message = convert_to_message('%l %c/%t in %te, ETA %e @%E %s')
123
214
  counter.reset(total: 0, current: 0)
@@ -125,12 +216,28 @@ class Infobar
125
216
  self
126
217
  end
127
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
128
225
  def update(message: nil, force: true)
129
226
  @message = convert_to_message(message)
130
227
  display.update(message: @message, counter: counter, force: force)
131
228
  self
132
229
  end
133
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
134
241
  def progress(by: 1, as: nil, message: nil, finish: true, force: false)
135
242
  counter.progress(by: by, as: as)
136
243
  @message = convert_to_message(message)
@@ -139,6 +246,11 @@ class Infobar
139
246
  self
140
247
  end
141
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
142
254
  def finish(message: nil)
143
255
  counter.finish
144
256
  @message = convert_to_message(message)
@@ -146,10 +258,22 @@ class Infobar
146
258
  self
147
259
  end
148
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]
149
265
  def self.convert_to_message(message)
150
266
  instance.convert_to_message(message)
151
267
  end
152
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]
153
277
  def convert_to_message(message)
154
278
  case message
155
279
  when Message
@@ -164,15 +288,42 @@ class Infobar
164
288
  end
165
289
  end
166
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)
167
298
  def Infobar(**opts)
168
299
  Infobar.(**opts)
169
300
  end
170
301
 
171
302
  class ::Object
303
+ # Returns the +Infobar+ singleton instance.
304
+ #
305
+ # @return [Infobar]
172
306
  def infobar
173
307
  ::Infobar.instance
174
308
  end
175
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) }
176
327
  def with_infobar(**opts, &block)
177
328
  case
178
329
  when total = opts[:total].ask_and_send(:to_sym)