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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ce90bf89a05aa85dd2dafe44ce71c68719984a4553195f7c5ee3ea85a557e544
4
- data.tar.gz: 2843fc691f0d520c3b1af317ec4faa6605cb40062abe384623f7c9fcc2ccf77e
3
+ metadata.gz: beec20adb697be698aa12ed3f834604867867e6fe75c8dcc4ad078ec74846f2e
4
+ data.tar.gz: 06a4590b0743fd05b408dee31f336c137f8386b3f6bd0d68996749f449ad6ff7
5
5
  SHA512:
6
- metadata.gz: b46cf5616ff55923b82a17d116729121b893be14446549878e911693986e9cff250dfeccfde5f0fd980d8775ec9b4c096d4117fe78c63556ad6560b8452c3ee4
7
- data.tar.gz: a473c4788dd8cb8f09801537b18b8732fec231a62138a7a5b2bd75623d5d58186a7e7cafe27a59713ec5b272c8b13fbeea66d8768ddb31b20b8da178e06f757f
6
+ metadata.gz: 1a162cbf417cda56c28fa60aecc64868142bf538045b7383192ec8cc78c2eb98f766ec8da3a9d216eca8da82e81dd2fe8d5a3efd054b5947ddc6f19f32268797
7
+ data.tar.gz: 73babe25cdd25aa3c84ba53441a8948a2da82b2c1abc209d2644d338eb41c90197a0b1916822aa7e18afbf41e24844d137055a3176e67088d8cff89eacab79b6
@@ -0,0 +1,51 @@
1
+ # Simple workflow for deploying static content to GitHub Pages
2
+ name: Deploy static content to Pages
3
+
4
+ on:
5
+ # Runs on pushes targeting the default branch
6
+ push:
7
+ branches: [ "master" ]
8
+
9
+ # Allows you to run this workflow manually from the Actions tab
10
+ workflow_dispatch:
11
+
12
+ # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13
+ permissions:
14
+ contents: read
15
+ pages: write
16
+ id-token: write
17
+
18
+ # Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
19
+ # However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
20
+ concurrency:
21
+ group: "pages"
22
+ cancel-in-progress: false
23
+
24
+ jobs:
25
+ # Single deploy job since we're just deploying
26
+ deploy:
27
+ environment:
28
+ name: github-pages
29
+ url: ${{ steps.deployment.outputs.page_url }}
30
+ runs-on: ubuntu-latest
31
+ steps:
32
+ - name: Checkout
33
+ uses: actions/checkout@v6
34
+ - name: Setup Pages
35
+ uses: actions/configure-pages@v6
36
+ - name: Setup Ruby
37
+ uses: ruby/setup-ruby@v1
38
+ with:
39
+ ruby-version: '4.0'
40
+ - name: Generate Documentation
41
+ run: |
42
+ gem install gem_hadar
43
+ bundle install
44
+ rake doc
45
+ - name: Upload artifact
46
+ uses: actions/upload-pages-artifact@v5
47
+ with:
48
+ path: 'doc'
49
+ - name: Deploy to GitHub Pages
50
+ id: deployment
51
+ uses: actions/deploy-pages@v5
data/CHANGES.md CHANGED
@@ -1,5 +1,78 @@
1
1
  # Changes
2
2
 
3
+ ## 2026-11-15 v0.14.0
4
+
5
+ ### Features
6
+
7
+ - Added `Infobar::Display#save_position!` to explicitly save the cursor
8
+ position and update the `@position_saved` flag.
9
+ - Exposed a writer for `Infobar::Display#position_saved` to allow forcing a
10
+ re-save of the cursor position on the next update.
11
+
12
+ ### Fixes
13
+
14
+ - Replaced carriage return (`\r`) with ANSI save (`\e[s`) and restore (`\e[u`)
15
+ position sequences in `Infobar::Display#update` and `Infobar::Display#clear`
16
+ to prevent bar drift when the user presses Enter mid-render.
17
+ - Updated the `SIGWINCH` handler in `Infobar#install_winch_trap` to reset
18
+ `position_saved` instead of calling `display.clear`, preventing stale cursor
19
+ restores when the saved row is out of bounds after a terminal resize.
20
+ - Modified `Infobar::Display#update` to restore the cursor position after
21
+ `layout_bar` to cancel delayed-wrap on the last visible row.
22
+ - Ensured `Infobar::InputOutput` methods (`print`, `printf`, `putc`, `puts`,
23
+ `gets`, `readline`, `readlines`) call `display.save_position!` after writing
24
+ to keep the bar aligned with the cursor.
25
+
26
+ ### Improvements
27
+
28
+ - Added `cscope.out` to the `skip_files` regex in `.utilsrc` to prevent code
29
+ search tools from indexing cscope database files.
30
+
31
+ ### Build & CI
32
+
33
+ - Added `cscope.out` to `.gitignore` and the `ignore` list in the `Rakefile`.
34
+
35
+ ### Testing
36
+
37
+ - Added a test in `spec/infobar/display_spec.rb` covering the save, clear,
38
+ write, and re-save lifecycle.
39
+ - Updated `spec/infobar/input_output_spec.rb` to expect `save_position!` calls
40
+ for `puts` and `gets`.
41
+ - Updated `spec/infobar/display_spec.rb` and `spec/infobar/config_spec.rb`
42
+ expectations to reflect the new ANSI position sequences.
43
+ - Switched `.busy` specs in `spec/infobar_spec.rb` from `and_call_original` to
44
+ plain stubs for hermetic output and added `finish`/`newline` expectations to
45
+ the `ArgumentError` test.
46
+
47
+ ## 2026-11-13 v0.13.2
48
+
49
+ ### Improvements
50
+
51
+ - Added a GitHub Actions workflow (`.github/workflows/static.yml`) to generate
52
+ YARD documentation and deploy it to GitHub Pages on pushes to `master`.
53
+ - Added a `Documentation` section to `README.md` linking to the hosted API
54
+ documentation.
55
+ - Added comprehensive YARD documentation to all files under `lib/infobar/` and
56
+ `lib/infobar.rb`, including class descriptions, method signatures, parameter
57
+ details, and return types.
58
+ - Replaced the BSD-specific `getconf _NPROCESSORS_ONLN` command with the
59
+ portable `nproc` command in the CI build script.
60
+ - Added status feedback messages (using emoji) to the CI `after` hook to
61
+ indicate test success or failure.
62
+ - Added `doc/` to `.gitignore` and the `Rakefile` ignore lists to exclude
63
+ generated documentation from version control and gem packages.
64
+
65
+ ### Build & CI
66
+
67
+ - Updated the `rubygems_version` in `infobar.gemspec` to **4.0.24**.
68
+ - Raised the `gem_hadar` development dependency requirement to **>= 2.21.0**.
69
+ - Added `git config --global --add safe.directory /work` to the CI build script
70
+ to prevent Git ownership safety warnings during test execution.
71
+ - Registered the new `static.yml` workflow in the `Rakefile` via
72
+ `github_workflows`.
73
+ - Added `.github/workflows/static.yml` to the list of distributed gem files in
74
+ `infobar.gemspec`.
75
+
3
76
  ## 2026-01-29 v0.13.1
4
77
 
5
78
  - Added `newline` method call in `ensure` block of `Infobar` class to ensure
data/README.md CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  Display progress of computations and additional information to the terminal.
6
6
 
7
+ ## Documentation
8
+
9
+ Complete API documentation is available at: [GitHub.io](https://flori.github.io/infobar/)
10
+
7
11
  ## Installation
8
12
 
9
13
  # gem install infobar
data/Rakefile CHANGED
@@ -13,9 +13,9 @@ GemHadar do
13
13
  test_dir 'spec'
14
14
  ignore '.*.sw[pon]', 'pkg', 'Gemfile.lock', 'coverage', '.rvmrc',
15
15
  '.AppleDouble', 'tags', '.DS_Store', 'errors.lst', '.tool-versions',
16
- '.bundle', '.yardoc'
16
+ '.bundle', '.yardoc', 'doc', 'cscope.out'
17
17
  package_ignore '.utilsrc', '.all_images.yml', 'VERSION', '.rspec',
18
- '.gitignore', 'TODO.md'
18
+ '.gitignore', 'TODO.md', 'doc'
19
19
 
20
20
  readme 'README.md'
21
21
  title "#{name.camelize}"
@@ -24,6 +24,10 @@ GemHadar do
24
24
  filename 'CHANGES.md'
25
25
  end
26
26
 
27
+ github_workflows(
28
+ 'static.yml' => {}
29
+ )
30
+
27
31
  required_ruby_version '>= 3.1'
28
32
  dependency 'tins', '~> 1', '>= 1.28.0'
29
33
  dependency 'term-ansicolor', '~> 1.11'
data/infobar.gemspec CHANGED
@@ -1,9 +1,9 @@
1
1
  # -*- encoding: utf-8 -*-
2
- # stub: infobar 0.13.1 ruby lib
2
+ # stub: infobar 0.14.0 ruby lib
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = "infobar".freeze
6
- s.version = "0.13.1".freeze
6
+ s.version = "0.14.0".freeze
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
9
9
  s.require_paths = ["lib".freeze]
@@ -12,17 +12,17 @@ Gem::Specification.new do |s|
12
12
  s.description = "This gem displays progress of computations and additional information to the terminal.".freeze
13
13
  s.email = "flori@ping.de".freeze
14
14
  s.extra_rdoc_files = ["README.md".freeze, "lib/infobar.rb".freeze, "lib/infobar/counter.rb".freeze, "lib/infobar/display.rb".freeze, "lib/infobar/duration.rb".freeze, "lib/infobar/fancy_interface.rb".freeze, "lib/infobar/fifo.rb".freeze, "lib/infobar/frequency.rb".freeze, "lib/infobar/input_output.rb".freeze, "lib/infobar/message.rb".freeze, "lib/infobar/number.rb".freeze, "lib/infobar/rate.rb".freeze, "lib/infobar/spinner.rb".freeze, "lib/infobar/timer.rb".freeze, "lib/infobar/trend.rb".freeze, "lib/infobar/version.rb".freeze]
15
- s.files = ["CHANGES.md".freeze, "COPYING".freeze, "Gemfile".freeze, "README.md".freeze, "Rakefile".freeze, "infobar.gemspec".freeze, "lib/infobar.rb".freeze, "lib/infobar/counter.rb".freeze, "lib/infobar/display.rb".freeze, "lib/infobar/duration.rb".freeze, "lib/infobar/fancy_interface.rb".freeze, "lib/infobar/fifo.rb".freeze, "lib/infobar/frequency.rb".freeze, "lib/infobar/input_output.rb".freeze, "lib/infobar/message.rb".freeze, "lib/infobar/number.rb".freeze, "lib/infobar/rate.rb".freeze, "lib/infobar/spinner.rb".freeze, "lib/infobar/timer.rb".freeze, "lib/infobar/trend.rb".freeze, "lib/infobar/version.rb".freeze, "spec/config/infobar.yml".freeze, "spec/infobar/config_spec.rb".freeze, "spec/infobar/counter_spec.rb".freeze, "spec/infobar/display_spec.rb".freeze, "spec/infobar/duration_spec.rb".freeze, "spec/infobar/fifo_spec.rb".freeze, "spec/infobar/frequency_spec.rb".freeze, "spec/infobar/input_output_spec.rb".freeze, "spec/infobar/message_spec.rb".freeze, "spec/infobar/number_spec.rb".freeze, "spec/infobar/rate_spec.rb".freeze, "spec/infobar/spinner_spec.rb".freeze, "spec/infobar/timer_spec.rb".freeze, "spec/infobar/trend_spec.rb".freeze, "spec/infobar_spec.rb".freeze, "spec/spec_helper.rb".freeze]
15
+ s.files = [".github/workflows/static.yml".freeze, "CHANGES.md".freeze, "COPYING".freeze, "Gemfile".freeze, "README.md".freeze, "Rakefile".freeze, "infobar.gemspec".freeze, "lib/infobar.rb".freeze, "lib/infobar/counter.rb".freeze, "lib/infobar/display.rb".freeze, "lib/infobar/duration.rb".freeze, "lib/infobar/fancy_interface.rb".freeze, "lib/infobar/fifo.rb".freeze, "lib/infobar/frequency.rb".freeze, "lib/infobar/input_output.rb".freeze, "lib/infobar/message.rb".freeze, "lib/infobar/number.rb".freeze, "lib/infobar/rate.rb".freeze, "lib/infobar/spinner.rb".freeze, "lib/infobar/timer.rb".freeze, "lib/infobar/trend.rb".freeze, "lib/infobar/version.rb".freeze, "spec/config/infobar.yml".freeze, "spec/infobar/config_spec.rb".freeze, "spec/infobar/counter_spec.rb".freeze, "spec/infobar/display_spec.rb".freeze, "spec/infobar/duration_spec.rb".freeze, "spec/infobar/fifo_spec.rb".freeze, "spec/infobar/frequency_spec.rb".freeze, "spec/infobar/input_output_spec.rb".freeze, "spec/infobar/message_spec.rb".freeze, "spec/infobar/number_spec.rb".freeze, "spec/infobar/rate_spec.rb".freeze, "spec/infobar/spinner_spec.rb".freeze, "spec/infobar/timer_spec.rb".freeze, "spec/infobar/trend_spec.rb".freeze, "spec/infobar_spec.rb".freeze, "spec/spec_helper.rb".freeze]
16
16
  s.homepage = "https://github.com/flori/infobar".freeze
17
17
  s.rdoc_options = ["--title".freeze, "Infobar".freeze, "--main".freeze, "README.md".freeze]
18
18
  s.required_ruby_version = Gem::Requirement.new(">= 3.1".freeze)
19
- s.rubygems_version = "4.0.3".freeze
19
+ s.rubygems_version = "4.0.24".freeze
20
20
  s.summary = "Gem to display information about computations.".freeze
21
21
  s.test_files = ["spec/infobar/config_spec.rb".freeze, "spec/infobar/counter_spec.rb".freeze, "spec/infobar/display_spec.rb".freeze, "spec/infobar/duration_spec.rb".freeze, "spec/infobar/fifo_spec.rb".freeze, "spec/infobar/frequency_spec.rb".freeze, "spec/infobar/input_output_spec.rb".freeze, "spec/infobar/message_spec.rb".freeze, "spec/infobar/number_spec.rb".freeze, "spec/infobar/rate_spec.rb".freeze, "spec/infobar/spinner_spec.rb".freeze, "spec/infobar/timer_spec.rb".freeze, "spec/infobar/trend_spec.rb".freeze, "spec/infobar_spec.rb".freeze, "spec/spec_helper.rb".freeze]
22
22
 
23
23
  s.specification_version = 4
24
24
 
25
- s.add_development_dependency(%q<gem_hadar>.freeze, [">= 2.17.0".freeze])
25
+ s.add_development_dependency(%q<gem_hadar>.freeze, [">= 2.21.0".freeze])
26
26
  s.add_development_dependency(%q<simplecov>.freeze, [">= 0".freeze])
27
27
  s.add_development_dependency(%q<rspec>.freeze, [">= 0".freeze])
28
28
  s.add_development_dependency(%q<debug>.freeze, [">= 0".freeze])
@@ -1,20 +1,42 @@
1
1
  require 'infobar/timer'
2
2
 
3
+ # A progress counter that tracks items, rates, and elapsed time.
4
+ #
5
+ # Internally uses an +Infobar::Timer+ for Welford-style online
6
+ # mean computation and a +FIFO+ for rate trending.
7
+ #
8
+ # @example Create and advance a counter
9
+ # counter = Infobar::Counter.new
10
+ # counter.reset(total: 10)
11
+ # counter.progress(by: 1)
3
12
  class Infobar::Counter
4
13
  class << self
5
14
  prepend Tins::Delegate
6
15
  end
7
16
 
17
+ # Creates a new Counter in a clean, zeroed state.
8
18
  def initialize
9
19
  reset
10
20
  end
11
21
 
22
+ # @!attribute [r] current
23
+ # @return [Integer] the number of items processed so far
12
24
  attr_reader :current
13
25
 
26
+ # @!attribute [r] total
27
+ # @return [Integer] the total number of items expected
14
28
  attr_reader :total
15
29
 
30
+ # @!attribute [r] as
31
+ # @return [Hash{Object => Integer}] per-category counts keyed
32
+ # by the +as:+ label (+nil+ is the default bucket)
16
33
  attr_reader :as
17
34
 
35
+ # Resets all internal state.
36
+ #
37
+ # @param total [Integer] total number of items
38
+ # @param current [Integer] starting item count
39
+ # @return [Infobar::Counter] self
18
40
  def reset(total: 0, current: 0)
19
41
  @current = current
20
42
  @as = Hash.new(0).update(nil => current)
@@ -31,19 +53,35 @@ class Infobar::Counter
31
53
 
32
54
  delegate :average_time, to: :@timer
33
55
 
56
+ # Whether the counter has been started.
57
+ #
58
+ # @return [Time, nil] the start time, or +nil+ if not started
34
59
  def started?
35
60
  @start
36
61
  end
37
62
 
63
+ # Whether the counter has been explicitly finished.
64
+ #
65
+ # @return [Time, false] the finish time, or +false+
38
66
  def finished?
39
67
  @finished
40
68
  end
41
69
 
70
+ # Marks the counter as finished at the current time.
71
+ #
72
+ # @return [Infobar::Counter] self
42
73
  def finish
43
74
  @finished = Time.now
44
75
  self
45
76
  end
46
77
 
78
+ # Advances the counter by the given amount.
79
+ #
80
+ # No-ops if the counter is finished or +by+ is less than 1.
81
+ #
82
+ # @param by [Integer] number of items to advance
83
+ # @param as [Object, nil] category label for per-category tracking
84
+ # @return [Infobar::Counter] self
47
85
  def progress(by: 1, as: nil)
48
86
  if !finished? && by >= 1
49
87
  now = Time.now
@@ -55,18 +93,30 @@ class Infobar::Counter
55
93
  self
56
94
  end
57
95
 
96
+ # Whether all items have been processed.
97
+ #
98
+ # @return [Boolean]
58
99
  def done?
59
100
  @current >= @total
60
101
  end
61
102
 
103
+ # Number of items remaining.
104
+ #
105
+ # @return [Integer] always >= 0
62
106
  def to_go
63
107
  [ total - current, 0 ].max
64
108
  end
65
109
 
110
+ # Whether there are items remaining.
111
+ #
112
+ # @return [Integer, nil] the remaining count or +nil+ if zero
66
113
  def to_go?
67
114
  to_go.nonzero?
68
115
  end
69
116
 
117
+ # Estimated seconds until completion.
118
+ #
119
+ # @return [Float] 0.0 if finished
70
120
  def time_remaining
71
121
  if @finished
72
122
  0.0
@@ -75,6 +125,9 @@ class Infobar::Counter
75
125
  end
76
126
  end
77
127
 
128
+ # Estimated time of completion.
129
+ #
130
+ # @return [Time] the finish time or projected finish
78
131
  def eta
79
132
  if @finished
80
133
  @finished
@@ -83,6 +136,9 @@ class Infobar::Counter
83
136
  end
84
137
  end
85
138
 
139
+ # Seconds elapsed since the counter started.
140
+ #
141
+ # @return [Float] 0.0 if not started
86
142
  def time_elapsed
87
143
  case
88
144
  when @finished && @start
@@ -94,10 +150,16 @@ class Infobar::Counter
94
150
  end
95
151
  end
96
152
 
153
+ # Total projected duration (elapsed + remaining).
154
+ #
155
+ # @return [Float]
97
156
  def total_time
98
157
  time_elapsed + time_remaining
99
158
  end
100
159
 
160
+ # Progress as a float between 0 and 1.
161
+ #
162
+ # @return [Float] 0.0 when total is zero
101
163
  def progressed
102
164
  if @total.zero?
103
165
  0.0
@@ -106,6 +168,9 @@ class Infobar::Counter
106
168
  end
107
169
  end
108
170
 
171
+ # Multi-line summary of per-category counts.
172
+ #
173
+ # @return [String] one line per category, e.g. +"default: 42"+
109
174
  def to_s
110
175
  n = -> k { k.nil? ? 'default' : k.to_s }
111
176
  width = -@as.keys.map(&n).max_by(&:size).to_i
@@ -2,12 +2,24 @@ require 'term/ansicolor'
2
2
  require 'stringio'
3
3
  require 'infobar/frequency'
4
4
 
5
+ # Terminal renderer for the progress bar.
6
+ #
7
+ # Draws a single-line bar with ANSI colors, respecting a
8
+ # configurable update +Frequency+. Supports per-category styling
9
+ # via +as_styles+ and a global +style+ hash.
10
+ #
11
+ # @example Create a display and update it
12
+ # display = Infobar::Display.new
13
+ # display.update(message: 'Working...', counter: counter)
5
14
  class Infobar::Display
6
15
  class << self
7
16
  prepend Tins::Delegate
8
17
  end
9
18
  include Term::ANSIColor
10
19
 
20
+ # Returns the default style hash for the progress bar.
21
+ #
22
+ # @return [Hash{Symbol => Object}] fill characters and ANSI colors
11
23
  def self.default_style
12
24
  {
13
25
  done_fill: ?░,
@@ -19,31 +31,54 @@ class Infobar::Display
19
31
  }
20
32
  end
21
33
 
34
+ # Creates a Display writing to +$stdout+ / reading from +$stdin+.
22
35
  def initialize
23
36
  self.output = $stdout
24
37
  self.input = $stdin
25
38
  self.frequency = 0.05
26
39
  @show = true
27
- @style = self.class.default_style.dup
40
+ @style = self.class.default_style.dup
41
+ @position_saved = false
28
42
  end
29
43
 
44
+ # @!attribute [w] position_saved
45
+ # Forces the next +update+ to re-save the cursor
46
+ # position.
47
+ attr_writer :position_saved
48
+
49
+ # Sets the update frequency threshold.
50
+ #
51
+ # @param duration [Numeric] minimum seconds between updates
30
52
  def frequency=(duration)
31
53
  @frequency = Infobar::Frequency.new(duration)
32
54
  end
33
55
 
56
+ # @!attribute [r] frequency
57
+ # @return [Infobar::Frequency] the rate-limiting gate
34
58
  attr_reader :frequency
35
59
 
60
+ # @!attribute [rw] show
61
+ # @return [Boolean] whether output is rendered or discarded
36
62
  attr_accessor :show
37
63
 
64
+ # Whether the display is currently visible.
65
+ #
66
+ # @return [Boolean]
38
67
  def show?
39
68
  @show
40
69
  end
41
70
 
71
+ # Sets the output IO, enabling +sync+= if supported.
72
+ #
73
+ # @param io [IO] the destination for bar output
42
74
  def output=(io)
43
75
  @output = io
44
76
  @output.ask_and_send(:respond_to?, :sync=, true)
45
77
  end
46
78
 
79
+ # Returns the active output IO, or +NULL+ when hidden.
80
+ #
81
+ # @return [IO]
47
82
  def output
48
83
  if show?
49
84
  @output
@@ -52,8 +87,13 @@ class Infobar::Display
52
87
  end
53
88
  end
54
89
 
90
+ # @!attribute [w] input
91
+ # Sets the source IO for user input.
55
92
  attr_writer :input
56
93
 
94
+ # Returns the active input IO, or +NULL+ when hidden.
95
+ #
96
+ # @return [IO]
57
97
  def input
58
98
  if show?
59
99
  @input
@@ -62,29 +102,49 @@ class Infobar::Display
62
102
  end
63
103
  end
64
104
 
105
+ # @!attribute [r] style
106
+ # @return [Hash{Symbol => Object}] the current global style
65
107
  attr_reader :style
66
108
 
109
+ # Merges new style options into the current style.
110
+ #
111
+ # @param new_style [Hash{Symbol => Object}] overrides to apply
67
112
  def style=(new_style)
68
113
  @style.update(**new_style)
69
114
  end
70
115
 
116
+ # Per-category style overrides. Lazily initialized.
117
+ #
118
+ # @return [Hash{Symbol => Hash}] category-specific styles
71
119
  def as_styles
72
120
  defined? @as_styles or self.as_styles = {}
73
121
  @as_styles
74
122
  end
75
123
 
124
+ # Sets per-category styles with a default proc falling back
125
+ # to the global +done_*+ entries.
126
+ #
127
+ # @param styles [Hash] category name to style hash mapping
76
128
  def as_styles=(styles)
77
129
  @as_styles = styles.to_h.symbolize_keys_recursive
78
130
  @as_styles.default_proc = proc { style.subhash(/\Adone_/) }
79
131
  end
80
132
 
133
+ # Renders the progress bar if the frequency gate allows.
134
+ #
135
+ # @param message [Infobar::Message, String, Hash, nil] the text
136
+ # to display alongside the bar
137
+ # @param counter [Infobar::Counter, nil] the counter driving the bar
138
+ # @param force [Boolean] bypass the frequency gate
139
+ # @param options [Hash] additional style overrides for this update
81
140
  def update(message: nil, counter: nil, force: false, **options)
82
141
  force and @frequency.reset
83
142
  @frequency.call do
84
- carriage_return
143
+ @position_saved ? (output << restore_position) : save_position!
85
144
  self.style = options
86
145
  message = infobar.convert_to_message(message)
87
146
  layout_bar(message, counter)
147
+ output << restore_position
88
148
  end
89
149
  end
90
150
 
@@ -125,25 +185,55 @@ class Infobar::Display
125
185
 
126
186
  delegate :called, to: :frequency, as: :updates
127
187
 
188
+ # Resets the display to defaults, optionally clearing the line.
189
+ #
190
+ # @param clear [Boolean] whether to blank the current line
191
+ # @return [Infobar::Display] self
128
192
  def reset(clear: true)
193
+ @position_saved = false
129
194
  clear && self.clear
130
195
  self.style = self.class.default_style
131
196
  self
132
197
  end
133
198
 
199
+ # Clears the current bar line and resets the frequency gate.
200
+ #
201
+ # @return [Infobar::Display] self
134
202
  def clear
135
- carriage_return
136
- output << ' ' * columns
137
- carriage_return
203
+ if @position_saved
204
+ output << restore_position
205
+ output << ' ' * columns
206
+ output << restore_position
207
+ else
208
+ carriage_return
209
+ output << ' ' * columns
210
+ carriage_return
211
+ end
212
+ @position_saved = false
138
213
  @frequency.reset
139
214
  self
140
215
  end
141
216
 
217
+ # Saves the current cursor position for subsequent restores.
218
+ #
219
+ # @return [Infobar::Display] self
220
+ def save_position!
221
+ output << save_position
222
+ @position_saved = true
223
+ self
224
+ end
225
+
226
+ # Emits a carriage return to the output.
227
+ #
228
+ # @return [Infobar::Display] self
142
229
  def carriage_return
143
230
  output << ?\r
144
231
  self
145
232
  end
146
233
 
234
+ # Emits a newline to the output.
235
+ #
236
+ # @return [Infobar::Display] self
147
237
  def newline
148
238
  output << $/
149
239
  self
@@ -151,10 +241,17 @@ class Infobar::Display
151
241
 
152
242
  private
153
243
 
244
+ # The Unicode Object Replacement Character used as a
245
+ # temporary fill during bar layout.
246
+ #
247
+ # @return [String]
154
248
  def replace_character
155
249
  "\uFFFC"
156
250
  end
157
251
 
252
+ # Returns the current terminal width in columns.
253
+ #
254
+ # @return [Integer]
158
255
  def columns
159
256
  Tins::Terminal.columns
160
257
  end
@@ -1,4 +1,17 @@
1
+ # A formatted duration string backed by +Tins::Duration.
2
+ #
3
+ # @example Create a default-formatted duration
4
+ # Infobar::Duration.new(42.5).to_s
5
+ #
6
+ # @example Custom format
7
+ # Infobar::Duration.new(42.5, format: '%m:%s').to_s
8
+ # # => "00:42"
1
9
  class Infobar::Duration
10
+ # Initializes a new Duration.
11
+ #
12
+ # @param value [Numeric] the number of seconds
13
+ # @param format [String, nil] a +Tins::Duration#+format string,
14
+ # e.g. +'%D'+, +'%m:%s'+, or +nil+ for the default
2
15
  def initialize(value, format: nil)
3
16
  duration = Tins::Duration.new(value)
4
17
  @string =
@@ -9,6 +22,9 @@ class Infobar::Duration
9
22
  end.to_s
10
23
  end
11
24
 
25
+ # Returns the pre-formatted duration string.
26
+ #
27
+ # @return [String]
12
28
  def to_s
13
29
  @string
14
30
  end
@@ -1,32 +1,61 @@
1
+ # A syntactic sugar module that lets you treat an +Infobar+ instance
2
+ # like a counter with familiar operators.
3
+ #
4
+ # @example Basic usage
5
+ # Infobar(total: 10)
6
+ # +infobar
7
+ # infobar << item
8
+ # infobar.add(1, 2, 3)
1
9
  module Infobar::FancyInterface
10
+ # Resets the counter and display.
11
+ # @return [Infobar] the infobar instance
2
12
  def ~
3
13
  reset
4
14
  end
5
15
 
16
+ # Advances progress by 1 under the +true+ key.
17
+ # @return [Infobar] the infobar instance
6
18
  def +@
7
19
  progress by: 1, as: true
8
20
  end
9
21
 
22
+ # Advances progress by 1 under the +false+ key.
23
+ # @return [Infobar] the infobar instance
10
24
  def -@
11
25
  progress by: 1, as: false
12
26
  end
13
27
 
28
+ # Advances progress by +by+ under the +true+ key.
29
+ # @param by [Integer] the number of items to advance
30
+ # @return [Infobar] the infobar instance
14
31
  def +(by)
15
32
  progress by: by, as: true
16
33
  end
17
34
 
35
+ # Advances progress by +by+ under the +false+ key.
36
+ # @param by [Integer] the number of items to advance
37
+ # @return [Infobar] the infobar instance
18
38
  def -(by)
19
39
  progress by: by, as: false
20
40
  end
21
41
 
42
+ # Enables +infobar + other+ and +other + infobar+ syntax.
43
+ # @param other [Object] the other operand
44
+ # @return [Array] a two-element array for coercion
22
45
  def coerce(other)
23
46
  return self, other
24
47
  end
25
48
 
49
+ # Advances progress by 1 (shorthand for +progress+).
50
+ # @param item [Object] the item being processed (unused)
51
+ # @return [Infobar] the infobar instance
26
52
  def <<(item)
27
53
  progress by: 1
28
54
  end
29
55
 
56
+ # Advances progress by the number of items given.
57
+ # @param items [Array] the items being processed
58
+ # @return [Infobar] the infobar instance
30
59
  def add(*items)
31
60
  progress by: items.size
32
61
  end