Skip to content

Commit 1e0fedf

Browse files
authored
Merge pull request #188 from black-night-heron/fix/spinner_finish
fix: only fill the bar for spinners in Finish()
2 parents fe6592d + d42b001 commit 1e0fedf

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

progressbar.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,13 @@ func (p *ProgressBar) Reset() {
494494

495495
// Finish will fill the bar to full
496496
func (p *ProgressBar) Finish() error {
497-
return p.Set64(p.config.max)
497+
p.lock.Lock()
498+
p.state.currentNum = p.config.max
499+
if !p.config.ignoreLength {
500+
p.state.currentBytes = float64(p.config.max)
501+
}
502+
p.lock.Unlock()
503+
return p.Add(0)
498504
}
499505

500506
// Exit will exit the bar to keep current state

progressbar_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,23 @@ func ExampleOptionClearOnFinish() {
9393
// Finished
9494
}
9595

96+
func TestSpinnerClearOnFinish(t *testing.T) {
97+
buf := strings.Builder{}
98+
bar := NewOptions(-1, OptionSetWidth(100), OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionClearOnFinish(), OptionSetWriter(&buf))
99+
bar.Reset()
100+
time.Sleep(1 * time.Second)
101+
bar.Add(10)
102+
time.Sleep(1 * time.Second)
103+
bar.Finish()
104+
result := buf.String()
105+
expect := "" +
106+
"\r- (10 B, 10 B/s, 10 it/s) [1s] " +
107+
"\r \r"
108+
if result != expect {
109+
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
110+
}
111+
}
112+
96113
func ExampleProgressBar_Finish() {
97114
bar := NewOptions(100, OptionSetWidth(10), OptionShowCount(), OptionShowBytes(true), OptionShowIts())
98115
bar.Reset()
@@ -102,6 +119,24 @@ func ExampleProgressBar_Finish() {
102119
// 100% |██████████| (100/100 B, 100 B/s, 100 it/s)
103120
}
104121

122+
func TestSpinnerFinish(t *testing.T) {
123+
buf := strings.Builder{}
124+
bar := NewOptions(-1, OptionSetWidth(100), OptionShowCount(), OptionShowBytes(true), OptionShowIts(), OptionSetWriter(&buf))
125+
bar.Reset()
126+
time.Sleep(1 * time.Second)
127+
bar.Add(10)
128+
time.Sleep(1 * time.Second)
129+
bar.Finish()
130+
result := buf.String()
131+
expect := "" +
132+
"\r- (10 B, 10 B/s, 10 it/s) [1s] " +
133+
"\r \r" +
134+
"\r| (10 B, 5 B/s, 5 it/s) [2s] "
135+
if result != expect {
136+
t.Errorf("Render miss-match\nResult: '%s'\nExpect: '%s'\n%+v", result, expect, bar)
137+
}
138+
}
139+
105140
func Example_xOutOfY() {
106141
bar := NewOptions(100, OptionSetPredictTime(true))
107142

0 commit comments

Comments
 (0)