Skip to content

Commit a4f8993

Browse files
authored
Merge pull request #1 from kaduartur/fix/sleep_time
[BUG] Fix lines cleaning
2 parents ba15148 + 34268e8 commit a4f8993

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

pkg/spinner/spinner.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ type Spinner struct {
2525
stopOnce sync.Once
2626
Output io.Writer
2727
NoTty bool
28+
timer *time.Timer
2829
}
2930

3031
// NewSpinner creates a Spinner with default template and FrameRate
@@ -34,6 +35,7 @@ func New(title string) *Spinner {
3435
Template: template.Default,
3536
FrameRate: DefaultFrameRate,
3637
runChan: make(chan struct{}),
38+
timer: time.NewTimer(DefaultFrameRate),
3739
}
3840

3941
var stdout interface{} = syscall.Stdout
@@ -76,9 +78,11 @@ func (s *Spinner) SetTemplate(template template.Template) *Spinner {
7678

7779
// Stop stops the spinner execution
7880
func (s *Spinner) Stop() {
79-
// prevent multiple calls
8081
s.stopOnce.Do(func() {
8182
close(s.runChan)
83+
if !s.timer.Stop() { // stop the timer when the spinner was stopped, preventing the next line from being cleared
84+
<-s.timer.C
85+
}
8286
s.clearLine()
8387
})
8488
}
@@ -105,7 +109,9 @@ func (s *Spinner) animate() {
105109
case !s.NoTty:
106110
fmt.Print(out)
107111
}
108-
time.Sleep(s.FrameRate)
112+
113+
s.timer.Reset(s.FrameRate)
114+
<-s.timer.C // Wait for timer
109115
s.clearLine()
110116
}
111117
}

0 commit comments

Comments
 (0)