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.
data/lib/infobar/fifo.rb CHANGED
@@ -1,15 +1,37 @@
1
+ # A fixed-capacity first-in-first-out buffer.
2
+ #
3
+ # Includes +Enumerable+ for iteration. When the buffer is full, the
4
+ # oldest element is evicted on push.
5
+ #
6
+ # @example Create and fill a FIFO
7
+ # fifo = Infobar::FIFO.new(3)
8
+ # fifo << 1 # => 1
9
+ # fifo << 2 # => 2
10
+ # fifo << 3 # => 3
11
+ # fifo << 4 # => 4 (1 is evicted)
1
12
  class Infobar::FIFO
2
13
  include Enumerable
3
14
 
15
+ # Initializes a new FIFO with the given maximum capacity.
16
+ #
17
+ # @param n [Integer] the maximum number of elements to retain
4
18
  def initialize(n)
5
19
  @n = n
6
20
  clear
7
21
  end
8
22
 
23
+ # Returns the maximum capacity of the buffer.
24
+ #
25
+ # @return [Integer]
9
26
  def max_size
10
27
  @n
11
28
  end
12
29
 
30
+ # Pushes a value into the buffer, evicting the oldest element
31
+ # if the buffer is at capacity.
32
+ #
33
+ # @param value [Object] the element to push
34
+ # @return [Infobar::FIFO] self for chaining
13
35
  def <<(value)
14
36
  @i += 1
15
37
  if @i > @n
@@ -20,21 +42,30 @@ class Infobar::FIFO
20
42
  self
21
43
  end
22
44
 
45
+ # Iterates over all elements currently in the buffer.
46
+ #
47
+ # @yield [element] yields each stored element
48
+ # @return [Infobar::FIFO] self
23
49
  def each(&block)
24
50
  @values.each(&block)
25
51
  self
26
52
  end
27
53
 
54
+ # Whether the buffer is empty.
55
+ #
56
+ # @return [Boolean]
28
57
  def empty?
29
58
  @i.zero?
30
59
  end
31
60
 
32
61
  alias size count
33
62
 
63
+ # Clears all elements from the buffer.
64
+ #
65
+ # @return [Infobar::FIFO] self
34
66
  def clear
35
67
  @i = 0
36
68
  @values = []
37
69
  self
38
70
  end
39
71
  end
40
-
@@ -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
@@ -14,6 +24,7 @@ module Infobar::InputOutput
14
24
  display.clear
15
25
  if display.output.respond_to?(method)
16
26
  display.output.public_send(method, *a, &b)
27
+ display.save_position!
17
28
  end
18
29
  end
19
30
  end
@@ -23,6 +34,7 @@ module Infobar::InputOutput
23
34
  display.clear
24
35
  if display.input.respond_to?(method)
25
36
  display.input.public_send(method, *a, &b)
37
+ display.save_position!
26
38
  end
27
39
  end
28
40
  end
@@ -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.14.0'
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: