Skip to content

Commit

Permalink
Fix setting the current progress for an indeterminate bar
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcwatt authored and piotrmurach committed Aug 23, 2023
1 parent 185d132 commit 75849ff
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change log

## Unreleased

### Fixed
* Fix setting the current progress for an indeterminate bar by Alex Watt
(@alexcwatt)

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

### Fixed
Expand Down
7 changes: 6 additions & 1 deletion lib/tty/progressbar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,12 @@ 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
17 changes: 17 additions & 0 deletions spec/unit/set_current_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@
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 +53,10 @@
output.rewind
expect(output.read).to eq("\e[1G[==========]\n")
end

it "allows setting progress when the 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 75849ff

Please sign in to comment.