forked from alperdrsnn/clime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner.go
More file actions
278 lines (240 loc) · 5.84 KB
/
spinner.go
File metadata and controls
278 lines (240 loc) · 5.84 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package clime
import (
"fmt"
"sync"
"time"
)
type SpinnerStyle struct {
Frames []string
Interval time.Duration
}
var (
SpinnerDots = SpinnerStyle{
Frames: []string{"⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"},
Interval: 80 * time.Millisecond,
}
SpinnerLine = SpinnerStyle{
Frames: []string{"|", "/", "-", "\\"},
Interval: 100 * time.Millisecond,
}
SpinnerArrow = SpinnerStyle{
Frames: []string{"←", "↖", "↑", "↗", "→", "↘", "↓", "↙"},
Interval: 120 * time.Millisecond,
}
SpinnerBounce = SpinnerStyle{
Frames: []string{"⠁", "⠂", "⠄", "⠂"},
Interval: 200 * time.Millisecond,
}
SpinnerClock = SpinnerStyle{
Frames: []string{"🕐", "🕑", "🕒", "🕓", "🕔", "🕕", "🕖", "🕗", "🕘", "🕙", "🕚", "🕛"},
Interval: 100 * time.Millisecond,
}
SpinnerEarth = SpinnerStyle{
Frames: []string{"🌍", "🌎", "🌏"},
Interval: 180 * time.Millisecond,
}
SpinnerMoon = SpinnerStyle{
Frames: []string{"🌑", "🌒", "🌓", "🌔", "🌕", "🌖", "🌗", "🌘"},
Interval: 80 * time.Millisecond,
}
SpinnerRunner = SpinnerStyle{
Frames: []string{"🚶", "🏃"},
Interval: 140 * time.Millisecond,
}
SpinnerPulse = SpinnerStyle{
Frames: []string{"●", "◐", "◑", "◒", "◓", "◔", "◕", "◖", "◗"},
Interval: 100 * time.Millisecond,
}
SpinnerGrowVertical = SpinnerStyle{
Frames: []string{"▁", "▃", "▄", "▅", "▆", "▇", "█", "▇", "▆", "▅", "▄", "▃"},
Interval: 120 * time.Millisecond,
}
)
type Spinner struct {
style SpinnerStyle
color *Color
message string
prefix string
suffix string
running bool
stopCh chan bool
mu sync.RWMutex
hideCursor bool
}
// NewSpinner creates a new spinner with the default style
func NewSpinner() *Spinner {
return &Spinner{
style: SpinnerDots,
color: CyanColor,
stopCh: make(chan bool),
hideCursor: true,
}
}
// WithStyle sets the spinner style
func (s *Spinner) WithStyle(style SpinnerStyle) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.style = style
return s
}
// WithColor sets the spinner color
func (s *Spinner) WithColor(color *Color) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.color = color
return s
}
// WithMessage sets the spinner message
func (s *Spinner) WithMessage(message string) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.message = message
return s
}
// WithPrefix sets a prefix for the spinner
func (s *Spinner) WithPrefix(prefix string) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.prefix = prefix
return s
}
// WithSuffix sets a suffix for the spinner
func (s *Spinner) WithSuffix(suffix string) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.suffix = suffix
return s
}
// HideCursor controls whether to hide the cursor while spinning
func (s *Spinner) HideCursor(hide bool) *Spinner {
s.mu.Lock()
defer s.mu.Unlock()
s.hideCursor = hide
return s
}
// Start starts the spinner animation
func (s *Spinner) Start() *Spinner {
s.mu.Lock()
if s.running {
s.mu.Unlock()
return s
}
s.running = true
s.stopCh = make(chan bool)
s.mu.Unlock()
if s.hideCursor {
HideCursor()
}
go s.animate()
return s
}
// Stop stops the spinner animation
func (s *Spinner) Stop() {
s.mu.Lock()
if !s.running {
s.mu.Unlock()
return
}
s.running = false
close(s.stopCh)
s.mu.Unlock()
ClearLine()
if s.hideCursor {
ShowCursor()
}
}
// Success stops the spinner and shows a success message
func (s *Spinner) Success(message string) {
s.Stop()
fmt.Print(Success.Sprint("✓ ") + message + "\n")
}
// Error stops the spinner and shows an error message
func (s *Spinner) Error(message string) {
s.Stop()
fmt.Print(Error.Sprint("✗ ") + message + "\n")
}
// Warning stops the spinner and shows a warning message
func (s *Spinner) Warning(message string) {
s.Stop()
fmt.Print(Warning.Sprint("⚠ ") + message + "\n")
}
// Info stops the spinner and shows an info message
func (s *Spinner) Info(message string) {
s.Stop()
fmt.Print(Info.Sprint("ℹ ") + message + "\n")
}
// UpdateMessage updates the spinner message while it's running
func (s *Spinner) UpdateMessage(message string) {
s.mu.Lock()
s.message = message
s.mu.Unlock()
}
// IsRunning returns true if the spinner is currently running
func (s *Spinner) IsRunning() bool {
s.mu.RLock()
defer s.mu.RUnlock()
return s.running
}
// animate runs the spinner animation loop
func (s *Spinner) animate() {
ticker := time.NewTicker(s.style.Interval)
defer ticker.Stop()
frameIndex := 0
for {
select {
case <-s.stopCh:
return
case <-ticker.C:
s.mu.RLock()
frame := s.style.Frames[frameIndex]
output := s.buildOutput(frame)
s.mu.RUnlock()
ClearLine()
fmt.Print(output)
frameIndex = (frameIndex + 1) % len(s.style.Frames)
}
}
}
// buildOutput builds the complete spinner output string
func (s *Spinner) buildOutput(frame string) string {
var output string
if s.prefix != "" {
output += s.prefix + " "
}
if s.color != nil {
output += s.color.Sprint(frame)
} else {
output += frame
}
if s.message != "" {
output += " " + s.message
}
if s.suffix != "" {
output += " " + s.suffix
}
return output
}
// ShowSpinner shows a spinner with a message and runs the provided function
func ShowSpinner(message string, fn func() error) error {
s := NewSpinner().WithMessage(message).Start()
defer s.Stop()
err := fn()
if err != nil {
s.Error(fmt.Sprintf("Failed: %v", err))
return err
}
s.Success("Done!")
return nil
}
// ShowSpinnerWithStyle shows a spinner with custom style, message and runs the provided function
func ShowSpinnerWithStyle(style SpinnerStyle, message string, fn func() error) error {
s := NewSpinner().WithStyle(style).WithMessage(message).Start()
defer s.Stop()
err := fn()
if err != nil {
s.Error(fmt.Sprintf("Failed: %v", err))
return err
}
s.Success("Done!")
return nil
}