-
Notifications
You must be signed in to change notification settings - Fork 221
/
spinner.go
70 lines (52 loc) · 1.04 KB
/
spinner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package spinner
import (
"fmt"
"sync"
"github.com/superfly/flyctl/iostreams"
)
func Run(io *iostreams.IOStreams, msg string) (s *Spinner) {
s = &Spinner{
io: io,
msg: msg,
}
s.StartWithMessage(msg)
return
}
type Spinner struct {
mu sync.Mutex
io *iostreams.IOStreams
msg string
}
func (s *Spinner) Set(msg string) string {
s.mu.Lock()
defer s.mu.Unlock()
old := s.msg
s.msg = msg
s.io.ChangeProgressIndicatorMsg(msg)
return old
}
func (s *Spinner) Stop() string {
return s.StopWithMessage("")
}
func (s *Spinner) StopWithSuccess() string {
return s.StopWithMessage(fmt.Sprintf("%s %s", s.msg, s.io.ColorScheme().Green("✓")))
}
func (s *Spinner) StopWithMessage(msg string) string {
s.mu.Lock()
defer s.mu.Unlock()
old := s.msg
s.msg = msg
s.io.StopProgressIndicatorMsg(msg)
return old
}
func (s *Spinner) Start() {
s.StartWithMessage("")
}
func (s *Spinner) StartWithMessage(msg string) string {
s.mu.Lock()
defer s.mu.Unlock()
old := s.msg
s.msg = msg
s.io.StartProgressIndicatorMsg(msg)
return old
}