/
spinner.go
59 lines (47 loc) · 1.09 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
package ui
import (
"fmt"
"os"
"time"
"github.com/briandowns/spinner"
)
type TrainSpinner []string
var (
TrainEmojis TrainSpinner = []string{"🚝", "🚅", "🚄", "🚇", "🚞", "🚈", "🚉", "🚂", "🚃", "🚊", "🚋"}
)
type SpinnerCfg struct {
// Message specifies the text label that appears while loading
Message string
// Tokens is a list of emoji to rotate through, during loading
Tokens []string
// Duration is the amount of delay between each spinner "frame"
Duration time.Duration
}
var s = &spinner.Spinner{}
func StartSpinner(cfg *SpinnerCfg) {
if !SupportsANSICodes() {
fmt.Println(cfg.Message)
return
}
if cfg.Tokens == nil {
cfg.Tokens = TrainEmojis
}
if cfg.Duration.Microseconds() == 0 {
cfg.Duration = time.Duration(100) * time.Millisecond
}
s = spinner.New(cfg.Tokens, cfg.Duration)
s.Writer = os.Stdout
if cfg.Message != "" {
s.Suffix = " " + cfg.Message
}
s.Start()
}
func StopSpinner(msg string) {
if msg != "" {
s.FinalMSG = msg + "\n"
}
// NOTE: Running Stop() when not active triggers a nil pointer
if s.Active() {
s.Stop()
}
}