File tree Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Expand file tree Collapse file tree 1 file changed +8
-2
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ type Spinner struct {
25
25
stopOnce sync.Once
26
26
Output io.Writer
27
27
NoTty bool
28
+ timer * time.Timer
28
29
}
29
30
30
31
// NewSpinner creates a Spinner with default template and FrameRate
@@ -34,6 +35,7 @@ func New(title string) *Spinner {
34
35
Template : template .Default ,
35
36
FrameRate : DefaultFrameRate ,
36
37
runChan : make (chan struct {}),
38
+ timer : time .NewTimer (DefaultFrameRate ),
37
39
}
38
40
39
41
var stdout interface {} = syscall .Stdout
@@ -76,9 +78,11 @@ func (s *Spinner) SetTemplate(template template.Template) *Spinner {
76
78
77
79
// Stop stops the spinner execution
78
80
func (s * Spinner ) Stop () {
79
- // prevent multiple calls
80
81
s .stopOnce .Do (func () {
81
82
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
+ }
82
86
s .clearLine ()
83
87
})
84
88
}
@@ -105,7 +109,9 @@ func (s *Spinner) animate() {
105
109
case ! s .NoTty :
106
110
fmt .Print (out )
107
111
}
108
- time .Sleep (s .FrameRate )
112
+
113
+ s .timer .Reset (s .FrameRate )
114
+ <- s .timer .C // Wait for timer
109
115
s .clearLine ()
110
116
}
111
117
}
You can’t perform that action at this time.
0 commit comments