From 75849ff987d603928e35c913882dc7a7b679e57f Mon Sep 17 00:00:00 2001 From: Alex Watt Date: Wed, 23 Aug 2023 05:58:19 -0400 Subject: [PATCH] Fix setting the current progress for an indeterminate bar --- CHANGELOG.md | 6 ++++++ lib/tty/progressbar.rb | 7 ++++++- spec/unit/set_current_spec.rb | 17 +++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6324ea6..d945c60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/lib/tty/progressbar.rb b/lib/tty/progressbar.rb index f176ffc..7cc88c4 100644 --- a/lib/tty/progressbar.rb +++ b/lib/tty/progressbar.rb @@ -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 diff --git a/spec/unit/set_current_spec.rb b/spec/unit/set_current_spec.rb index fd7e5ef..70cf3bd 100644 --- a/spec/unit/set_current_spec.rb +++ b/spec/unit/set_current_spec.rb @@ -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 @@ -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