-
Notifications
You must be signed in to change notification settings - Fork 1
/
spinner.go
176 lines (154 loc) · 4.54 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package loading
import (
"fmt"
"time"
)
// TODO: Consier complete message as assignable field in Spinner so it can be
// automatically used when End() is called which is important for further
// implementing the interface, palette will give us both rainbow-type effects
// but also allow us to do like light to dark to fill in bars.
type Spinner struct {
animation []string
palette []string
message string
animationIndex int
paletteIndex int
ticker *time.Ticker
speed int
end chan bool
bar bool
}
func NewSpinner(animation []string) *Spinner {
return &Spinner{
animation: animation,
speed: Average,
bar: false,
palette: []string{""},
ticker: time.NewTicker(time.Millisecond * time.Duration(Average)),
end: make(chan bool),
}
}
func (spinner *Spinner) hasPalette() bool {
return len(spinner.palette) == 0
}
func (spinner *Spinner) LoadingBar(bar bool) *Spinner {
spinner.bar = bar
return spinner
}
func (spinner *Spinner) Animation(frames []string) {
spinner.animation = frames
}
func (spinner *Spinner) Start() { go spinner.Animate() }
func (spinner *Spinner) Cancel() { spinner.Complete("") }
func (spinner *Spinner) End() { spinner.Complete("") }
func (spinner *Spinner) Complete(message string) {
if !spinner.bar {
fmt.Print(EraseLine(2))
fmt.Print(ShowCursor())
fmt.Print(CursorStart(1))
fmt.Printf("%v\n", message)
}
spinner.end <- true
}
func (spinner *Spinner) Animate() {
for {
select {
case <-spinner.end:
return
case <-spinner.ticker.C:
if !spinner.bar {
fmt.Print(spinner.Frame() + spinner.message)
} else {
fmt.Print(spinner.Frame())
}
}
}
}
func (spinner *Spinner) Message(message string) *Spinner {
spinner.message = fmt.Sprintf(" %v", message)
return spinner
}
func (spinner *Spinner) Speed(speed int) *Spinner {
spinner.ticker = time.NewTicker(time.Millisecond * time.Duration(speed))
return spinner
}
func (spinner *Spinner) Palette(palette []string) *Spinner {
spinner.palette = palette
return spinner
}
func (spinner Spinner) totalPalette() int { return len(spinner.animation) }
func (spinner Spinner) totalFrames() int { return len(spinner.animation) }
func (spinner Spinner) firstPalette() string {
if spinner.totalPalette() != 0 {
return spinner.animation[0]
}
return ""
}
func (spinner Spinner) lastPalette() string {
if spinner.totalPalette() != 0 {
return spinner.animation[spinner.totalPalette()-1]
}
return ""
}
func (spinner Spinner) firstFrame() string {
if spinner.totalFrames() != 0 {
return spinner.animation[0]
}
return ""
}
func (spinner Spinner) lastFrame() string {
if spinner.totalFrames() != 0 {
return spinner.animation[spinner.totalFrames()-1]
}
return ""
}
// TODO
// Would like to be able to have a palette to easily pull colors from
// but also the palette concept is what colors we cycle through
//
// TODO
// Create a function to grab the frame for a given 0.1-0.9
//
// This gives us the ability to animate frames to show progress in greater
// resolution
// Two options here for fractional frame:
// - I could take the value, and make it the new maximum, cycling up to
// the fractional percentage value
// - I could just slowly add parts of the animation to make it appear
// like its representing 100% with greater resolution
func (spinner Spinner) FractionalFrameIndex(percentage int) int {
return spinner.totalFrames() * (percentage / 100)
}
func (spinner *Spinner) Frame() string {
if !spinner.bar {
fmt.Print(HideCursor())
fmt.Print(EraseLine(2))
fmt.Print(CursorStart(1))
}
//Text(strings.Join(spinner.animation, "")).sgr(style(Blue, Foreground)).String(),
fmt.Sprintf(
"spinner.animation(%v)\n",
Text("test").sgr(style(Black, Foreground)).String(),
)
spinner.animationIndex = increment(spinner.animationIndex, spinner.totalFrames())
if spinner.hasPalette() {
spinner.paletteIndex = increment(spinner.paletteIndex, spinner.totalPalette())
// TODO: For use with loading bar we can't do simple reset anymore, need
// matching close to not ruin ANSI style on loading bar
return spinner.palette[spinner.paletteIndex] + spinner.animation[spinner.animationIndex] + "\x1b[0m"
} else {
return spinner.animation[spinner.animationIndex]
}
}
func (spinner *Spinner) Increment(skipFrames float64) bool {
spinner.animationIndex = increment(spinner.animationIndex+int(skipFrames), spinner.totalFrames())
return spinner.animationIndex != len(spinner.animation)
}
func increment(tick, max int) int {
tick++
if tick == max {
return 0
} else {
return tick
}
}