From 2b335f6a7afaf12953c5cdbc6deea523a8facffd Mon Sep 17 00:00:00 2001 From: NigHThERon <14975428+black-night-heron@users.noreply.github.com> Date: Fri, 7 Jun 2024 02:40:18 +0800 Subject: [PATCH 1/2] fix: only fill the bar for spinners in Finish() --- progressbar.go | 8 +++++++- progressbar_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/progressbar.go b/progressbar.go index cef4629..e3bd67b 100644 --- a/progressbar.go +++ b/progressbar.go @@ -494,7 +494,13 @@ func (p *ProgressBar) Reset() { // Finish will fill the bar to full func (p *ProgressBar) Finish() error { - return p.Set64(p.config.max) + p.lock.Lock() + p.state.currentNum = p.config.max + if !p.config.ignoreLength { + p.state.currentBytes = float64(p.config.max) + } + p.lock.Unlock() + return p.Add(0) } // Exit will exit the bar to keep current state diff --git a/progressbar_test.go b/progressbar_test.go index 4910019..09f7b57 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -93,6 +93,23 @@ func ExampleOptionClearOnFinish() { // Finished } +func TestSpinnerClearOnFinish(t *testing.T) { + buf := strings.Builder{} + bar := NewOptions(-1, OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionClearOnFinish(), OptionSetWriter(&buf)) + bar.Reset() + time.Sleep(1 * time.Second) + bar.Add(10) + time.Sleep(1 * time.Second) + bar.Finish() + result := buf.String() + expect := "" + + "\r- (10 B, 10 B/s, 10 it/s) [1s] " + + "\r \r" + if result != expect { + t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar) + } +} + func ExampleProgressBar_Finish() { bar := NewOptions(100, OptionSetWidth(10), OptionShowCount(), OptionShowBytes(true), OptionShowIts()) bar.Reset() @@ -102,6 +119,24 @@ func ExampleProgressBar_Finish() { // 100% |██████████| (100/100 B, 100 B/s, 100 it/s) } +func TestSpinnerFinish(t *testing.T) { + buf := strings.Builder{} + bar := NewOptions(-1, OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionSetWriter(&buf)) + bar.Reset() + time.Sleep(1 * time.Second) + bar.Add(10) + time.Sleep(1 * time.Second) + bar.Finish() + result := buf.String() + expect := "" + + "\r- (10 B, 10 B/s, 10 it/s) [1s] " + + "\r \r" + + "\r| (10 B, 5 B/s, 5 it/s) [2s] " + if result != expect { + t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar) + } +} + func Example_xOutOfY() { bar := NewOptions(100, OptionSetPredictTime(true)) From d42b00125dc3bb18ad5d34ab3db8a149ee9e44fe Mon Sep 17 00:00:00 2001 From: NigHThERon <14975428+black-night-heron@users.noreply.github.com> Date: Fri, 7 Jun 2024 02:56:21 +0800 Subject: [PATCH 2/2] use non-default width in spinner tests --- progressbar_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/progressbar_test.go b/progressbar_test.go index 09f7b57..7973ee6 100644 --- a/progressbar_test.go +++ b/progressbar_test.go @@ -95,7 +95,7 @@ func ExampleOptionClearOnFinish() { func TestSpinnerClearOnFinish(t *testing.T) { buf := strings.Builder{} - bar := NewOptions(-1, OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionClearOnFinish(), OptionSetWriter(&buf)) + bar := NewOptions(-1, OptionSetWidth(100), OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionClearOnFinish(), OptionSetWriter(&buf)) bar.Reset() time.Sleep(1 * time.Second) bar.Add(10) @@ -121,7 +121,7 @@ func ExampleProgressBar_Finish() { func TestSpinnerFinish(t *testing.T) { buf := strings.Builder{} - bar := NewOptions(-1, OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionSetWriter(&buf)) + bar := NewOptions(-1, OptionSetWidth(100), OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionSetWriter(&buf)) bar.Reset() time.Sleep(1 * time.Second) bar.Add(10)