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..7973ee6 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, OptionSetWidth(100), 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, OptionSetWidth(100), 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))