Skip to content

Commit

Permalink
Merge pull request #188 from black-night-heron/fix/spinner_finish
Browse files Browse the repository at this point in the history
fix: only fill the bar for spinners in Finish()
  • Loading branch information
schollz authored Jun 6, 2024
2 parents fe6592d + d42b001 commit 1e0fedf
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
8 changes: 7 additions & 1 deletion progressbar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions progressbar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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))

Expand Down

0 comments on commit 1e0fedf

Please sign in to comment.