Skip to content

Commit

Permalink
ProgressBar#current= supports indeterminate progress bars too
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcwatt committed Aug 27, 2023
1 parent 185d132 commit d02410f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change log

## Unreleased

### Fixed
* Fix ProgressBar#current= for indeterminate progress bar by Alex Watt (@alexcwatt)

## [v0.18.2] - 2021-03-08

### Fixed
Expand Down
6 changes: 5 additions & 1 deletion lib/tty/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,11 @@ def update(options = {})
#
# @api public
def current=(value)
value = [0, [value, total].min].max
unless value.is_a?(Numeric)
raise ArgumentError, "Expected a numeric value, got #{value.inspect} instead."
end

value = [0, [value, total].compact.min].max
advance(value - @current)
end

Expand Down
15 changes: 15 additions & 0 deletions spec/unit/set_current_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@
expect(progress.current).to eq(0)
end

it "doesn't allow nil" do
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
expect { progress.current = nil }.to raise_error(
ArgumentError,
"Expected a numeric value, got nil instead."
)
expect(progress.current).to eq(0)
end

it "cannot backtrack on finished" do
progress = TTY::ProgressBar.new("[:bar]", output: output, total: 10)
progress.current = 10
Expand All @@ -42,4 +51,10 @@
output.rewind
expect(output.read).to eq("\e[1G[==========]\n")
end

it "allows setting progress when total is unknown" do
progress = TTY::ProgressBar.new("[:bar]", output: output, total: nil)
progress.current = 5
expect(progress.current).to eq(5)
end
end

0 comments on commit d02410f

Please sign in to comment.