-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinner.go
88 lines (80 loc) · 1.96 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
* Copyright (c) 2023 Brandon Jordan
*/
package ttuy
import (
"fmt"
"time"
)
type SpinnerStyle string
const Ticker SpinnerStyle = "ticker"
const DotDotDot SpinnerStyle = "dotdotdot"
const Throbber SpinnerStyle = "throbber"
const Blinker SpinnerStyle = "blinker"
var ticks = []string{"|", "/", "—", "\\"}
var dots = []string{"", ".", "..", "..."}
var throbs = []string{
Style(bullet, CyanText) + Style(bullet, Dim) + Style(bullet, Dim),
Style(bullet, Dim) + Style(bullet, CyanText) + Style(bullet, Dim),
Style(bullet, Dim) + Style(bullet, Dim) + Style(bullet, CyanText),
}
var blinks = []string{
Style(bullet, Dim),
Style(bullet, CyanText, Blink),
}
var stop = make(chan bool)
// Spinner prints a progress indicator in style until StopSpinner() is called
// You must use a goroutine when running this function (e.g. go Spinner(...))
func Spinner(status string, style SpinnerStyle) {
CursorHide()
fmt.Print(eol)
LinePrev(1)
for {
select {
case <-stop:
return
default:
switch style {
case Ticker:
for i := 0; i < len(ticks); i++ {
ClearLine()
fmt.Print(Style(ticks[i], CyanText))
fmt.Print(" " + Style(status, Bold))
fmt.Print("\r")
time.Sleep(100 * time.Millisecond)
}
case DotDotDot:
for i := 0; i < len(dots); i++ {
ClearLine()
fmt.Print(Style(status, Bold))
fmt.Print(Style(dots[i], Dim))
fmt.Print("\r")
time.Sleep(300 * time.Millisecond)
}
case Throbber:
for i := 0; i < len(throbs); i++ {
ClearLine()
fmt.Print(throbs[i])
fmt.Print(" " + Style(status, Bold))
fmt.Print("\r")
time.Sleep(200 * time.Millisecond)
}
case Blinker:
for i := 0; i < len(blinks); i++ {
ClearLine()
fmt.Print(blinks[i])
fmt.Print(" " + Style(status, Bold))
fmt.Print("\r")
time.Sleep(500 * time.Millisecond)
}
}
}
}
}
// StopSpinner stops the current spinner
func StopSpinner() {
stop <- true
ClearLine()
fmt.Print(eol)
CursorShow()
}