-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathspin.go
129 lines (116 loc) · 2.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package spin provides a very simple spinner for cli applications.
package spin
import (
"fmt"
"io"
"os"
"sync/atomic"
"time"
)
// ClearLine go to the beginning 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 = `⦾⦿`
Spin11 = `⌜⌝⌟⌞`
Spin12 = `┤┘┴└├┌┬┐`
Spin13 = `⇑⇗⇒⇘⇓⇙⇐⇖`
Spin14 = `☰☱☳☷☶☴`
Spin15 = `䷀䷪䷡䷊䷒䷗䷁䷖䷓䷋䷠䷫`
Default = Box1
)
// Spinner main type
type Spinner struct {
frames []rune
pos int
active uint64
text string
tpf time.Duration
writer io.Writer
}
// Option describes an option to override a default
// when creating a new Spinner.
type Option func(s *Spinner)
// New creates a Spinner object with the provided
// text. By default, the Default spinner frames are
// used, and new frames are rendered every 100 milliseconds.
// Options can be provided to override these default
// settings.
func New(text string, opts ...Option) *Spinner {
s := &Spinner{
text: ClearLine + text,
frames: []rune(Default),
tpf: 100 * time.Millisecond,
writer: os.Stdout,
}
for _, o := range opts {
o(s)
}
return s
}
// WithFrames sets the frames string.
func WithFrames(frames string) Option {
return func(s *Spinner) {
s.Set(frames)
}
}
// WithTimePerFrame sets how long each frame shall
// be shown.
func WithTimePerFrame(d time.Duration) Option {
return func(s *Spinner) {
s.tpf = d
}
}
// WithWriter sets the writer to use for spinner's text.
func WithWriter(w io.Writer) Option {
return func(s *Spinner) {
s.writer = w
}
}
// 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.Fprintf(s.writer, s.text, s.next())
time.Sleep(s.tpf)
}
}()
return s
}
// Stop hides the spinner.
func (s *Spinner) Stop() bool {
if x := atomic.SwapUint64(&s.active, 0); x > 0 {
fmt.Fprintf(s.writer, ClearLine)
return true
}
return false
}
func (s *Spinner) next() string {
r := s.frames[s.pos%len(s.frames)]
s.pos++
return string(r)
}