-
Notifications
You must be signed in to change notification settings - Fork 17
/
spin.go
71 lines (63 loc) · 1.67 KB
/
spin.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
71
package spin
import "sync"
// Spinner types.
var (
Box1 = `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`
Box2 = `⠋⠙⠚⠞⠖⠦⠴⠲⠳⠓`
Box3 = `⠄⠆⠇⠋⠙⠸⠰⠠⠰⠸⠙⠋⠇⠆`
Box4 = `⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋`
Box5 = `⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁`
Box6 = `⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈`
Box7 = `⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈`
Spin1 = `|/-\`
Spin2 = `◴◷◶◵`
Spin3 = `◰◳◲◱`
Spin4 = `◐◓◑◒`
Spin5 = `▉▊▋▌▍▎▏▎▍▌▋▊▉`
Spin6 = `▌▄▐▀`
Spin7 = `╫╪`
Spin8 = `■□▪▫`
Spin9 = `←↑→↓`
Default = Box1
)
// Spinner is exactly what you think it is.
type Spinner struct {
mu sync.Mutex
frames []rune
length int
pos int
}
// New returns a spinner initialized with Default frames.
func New() *Spinner {
s := &Spinner{}
s.Set(Default)
return s
}
// Set frames to the given string which must not use spaces.
func (s *Spinner) Set(frames string) {
s.mu.Lock()
defer s.mu.Unlock()
s.frames = []rune(frames)
s.length = len(s.frames)
}
// Current returns the current rune in the sequence.
func (s *Spinner) Current() string {
s.mu.Lock()
defer s.mu.Unlock()
r := s.frames[s.pos%s.length]
return string(r)
}
// Next returns the next rune in the sequence.
func (s *Spinner) Next() string {
s.mu.Lock()
defer s.mu.Unlock()
r := s.frames[s.pos%s.length]
s.pos++
return string(r)
}
// Reset the spinner to its initial frame.
func (s *Spinner) Reset() {
s.mu.Lock()
defer s.mu.Unlock()
s.pos = 0
}