forked from caarlos0-graveyard/spin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspin.go
More file actions
85 lines (76 loc) · 1.89 KB
/
spin.go
File metadata and controls
85 lines (76 loc) · 1.89 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Package spin provides a very simple spinner for cli applications.
package spin
import (
"fmt"
"sync/atomic"
"time"
)
// ClearLine go to the beggining of the line and clear it
const ClearLine = "\r\033[K"
// Spinner types.
var (
Box1 = `⠋⠙⠹⠸⠼⠴⠦⠧⠇⠏`
Box2 = `⠋⠙⠚⠞⠖⠦⠴⠲⠳⠓`
Box3 = `⠄⠆⠇⠋⠙⠸⠰⠠⠰⠸⠙⠋⠇⠆`
Box4 = `⠋⠙⠚⠒⠂⠂⠒⠲⠴⠦⠖⠒⠐⠐⠒⠓⠋`
Box5 = `⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠴⠲⠒⠂⠂⠒⠚⠙⠉⠁`
Box6 = `⠈⠉⠋⠓⠒⠐⠐⠒⠖⠦⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈`
Box7 = `⠁⠁⠉⠙⠚⠒⠂⠂⠒⠲⠴⠤⠄⠄⠤⠠⠠⠤⠦⠖⠒⠐⠐⠒⠓⠋⠉⠈⠈`
Spin1 = `|/-\`
Spin2 = `◴◷◶◵`
Spin3 = `◰◳◲◱`
Spin4 = `◐◓◑◒`
Spin5 = `▉▊▋▌▍▎▏▎▍▌▋▊▉`
Spin6 = `▌▄▐▀`
Spin7 = `╫╪`
Spin8 = `■□▪▫`
Spin9 = `←↑→↓`
Spin10 = `⦾⦿`
Default = Box1
)
// Spinner main type
type Spinner struct {
frames []rune
pos int
active uint64
text string
}
// New Spinner with args
func New(text string) *Spinner {
s := &Spinner{
text: ClearLine + text,
}
s.Set(Default)
return s
}
// Set frames to the given string which must not use spaces.
func (s *Spinner) Set(frames string) {
s.frames = []rune(frames)
}
// Start shows the spinner.
func (s *Spinner) Start() *Spinner {
if atomic.LoadUint64(&s.active) > 0 {
return s
}
atomic.StoreUint64(&s.active, 1)
go func() {
for atomic.LoadUint64(&s.active) > 0 {
fmt.Printf(s.text, s.next())
time.Sleep(100 * time.Millisecond)
}
}()
return s
}
// Stop hides the spinner.
func (s *Spinner) Stop() bool {
if x := atomic.SwapUint64(&s.active, 0); x > 0 {
fmt.Printf(ClearLine)
return true
}
return false
}
func (s *Spinner) next() string {
r := s.frames[s.pos%len(s.frames)]
s.pos++
return string(r)
}