forked from chelnak/ysmrr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
376 lines (334 loc) · 9.54 KB
/
manager.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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
// Package ysmrr provides a simple interface for creating and managing
// multiple spinners.
package ysmrr
import (
"context"
"io"
"os"
"os/signal"
"runtime"
"sync"
"time"
"github.com/mattn/go-colorable"
"github.com/zioc/ysmrr/pkg/animations"
"github.com/zioc/ysmrr/pkg/colors"
"github.com/zioc/ysmrr/pkg/tput"
)
// SpinnerManager manages spinners
type SpinnerManager interface {
AddSpinner(msg string) *Spinner
GetSpinners() []*Spinner
SetSpinnersCount(count int)
GetWriter() io.Writer
GetAnimation() []string
GetFrameDuration() time.Duration
GetSpinnerColor() colors.Color
GetErrorColor() colors.Color
GetCompleteColor() colors.Color
GetMessageColor() colors.Color
Start()
Stop()
}
type spinnerManager struct {
spinners []*Spinner
mutex sync.RWMutex
chars []string
frameDuration time.Duration
spinnerColor colors.Color
completeColor colors.Color
errorColor colors.Color
messageColor colors.Color
writer io.Writer
context context.Context
done chan bool
hasUpdate chan bool
ticks *time.Ticker
lines int
frame int
tty bool
}
// AddSpinner adds a new spinner to the manager.
func (sm *spinnerManager) AddSpinner(message string) *Spinner {
spinner := sm.createSpinner(message)
sm.mutex.Lock()
defer sm.mutex.Unlock()
sm.spinners = append(sm.spinners, spinner)
return spinner
}
// Create a new spinner with manager options.
func (sm *spinnerManager) createSpinner(message string) *Spinner {
opts := SpinnerOptions{
Message: message,
SpinnerColor: sm.spinnerColor,
CompleteColor: sm.completeColor,
ErrorColor: sm.errorColor,
MessageColor: sm.messageColor,
HasUpdate: sm.hasUpdate,
}
return NewSpinner(opts)
}
// GetSpinners returns the spinners managed by the manager.
func (sm *spinnerManager) GetSpinners() []*Spinner {
sm.mutex.RLock()
defer sm.mutex.RUnlock()
return sm.spinners
}
// SetSpinnersCount defines the amount of spinners managed by the manager.
// It will create empty sprinners or delete last ones to match requested size.
func (sm *spinnerManager) SetSpinnersCount(count int) {
sm.mutex.Lock()
defer sm.mutex.Unlock()
if count > len(sm.spinners) {
for i:=len(sm.spinners); i < count; i++{
sm.spinners = append(sm.spinners, sm.createSpinner(""))
}
} else{
sm.spinners = sm.spinners[:count]
}
}
// Start the rendering loop in a goroutine.
// Creates a interrupt signal if cancel context was not provided.
func (sm *spinnerManager) Start() {
if sm.context == nil {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
go func() {
<-ctx.Done()
sm.Stop()
cancel()
os.Exit(0)
}()
}
sm.ticks = time.NewTicker(sm.frameDuration)
go sm.render()
}
// Stop sends a signal to the render goroutine to stop
// rendering. We then stop the ticker and persist the final
// frame for each spinner.
// Finally the deferred tput command will ensure tat the cursor
// is no longer hidden.
func (sm *spinnerManager) Stop() {
sm.done <- true
sm.ticks.Stop()
// Persist the final frame for each spinner.
sm.mutex.Lock()
defer sm.mutex.Unlock()
for _, s := range sm.spinners {
tput.ClearLine(sm.writer)
s.Print(sm.writer, sm.chars[sm.frame])
}
tput.Cnorm(sm.writer)
}
// GetWriter returns the configured io.Writer.
func (sm *spinnerManager) GetWriter() io.Writer {
return sm.writer
}
// GetAnimation returns the current spinner animation as
// a slice of strings.
func (sm *spinnerManager) GetAnimation() []string {
return sm.chars
}
// GetFrameDuration returns the configured frame duration.
func (sm *spinnerManager) GetFrameDuration() time.Duration {
return sm.frameDuration
}
// GetSpinnerColor returns the configured color of the spinners.
func (sm *spinnerManager) GetSpinnerColor() colors.Color {
return sm.spinnerColor
}
// GetErrorColor returns the configured color of error icon.
func (sm *spinnerManager) GetErrorColor() colors.Color {
return sm.errorColor
}
// GetCompleteColor returns the configured color of completed icon.
func (sm *spinnerManager) GetCompleteColor() colors.Color {
return sm.completeColor
}
// GetMessageColor returns the color of the message.
func (sm *spinnerManager) GetMessageColor() colors.Color {
return sm.messageColor
}
// This is the code that actually renders the spinners.
// Rendering is done in a separate goroutine so that the main
// goroutine can continue to handle signals.
// The render goroutine is called by Start().
//
// Each tick signal calls renderFrame which in turn will print the current
// frame to the writer provided by the manager.
//
// The render method also emits tput strings to the terminal to set the
// correct location of the cursor.
func (sm *spinnerManager) render() {
// Prepare the screen.
tput.Civis(sm.writer)
defer tput.Cnorm(sm.writer)
for {
select {
case <-sm.done:
return
case <-sm.hasUpdate:
sm.renderFrame(false)
case <-sm.ticks.C:
sm.renderFrame(true)
}
tput.Rc(sm.writer)
}
}
func (sm *spinnerManager) renderFrame(animate bool) {
if !sm.tty {
return
}
spinners := sm.GetSpinners()
linesCount := len(spinners)
width, height := tput.TtySize()
if width != 0 {
linesCount = 0
for _, s := range spinners {
linesCount = linesCount + (len([]rune(s.message))+3)/width
if (len([]rune(s.message))+3)%width != 0 {
linesCount = linesCount + 1
}
}
}
if linesCount > sm.lines {
sm.lines = linesCount
} else if height != 0 && sm.lines > height {
sm.lines = height
}
tput.BufScreen(sm.writer, linesCount)
tput.Cuu(sm.writer, linesCount)
tput.Sc(sm.writer)
for _, s := range spinners {
tput.ClearLine(sm.writer)
s.Print(sm.writer, sm.chars[sm.frame])
}
// Clear extra lines if any
for i := linesCount; i < sm.lines; i++ {
tput.ClearLine(sm.writer)
if i+1 < sm.lines {
tput.Write(sm.writer, "\n")
}
}
if animate {
sm.setNextFrame()
}
}
func (sm *spinnerManager) setNextFrame() {
sm.frame += 1
if sm.frame >= len(sm.chars) {
sm.frame = 0
}
}
// NewSpinnerManager is the constructor for the SpinnerManager.
// You can create a new manager with sensible defaults or you can
// pass in your own options using the provided methods.
//
// For example, this will initialize a default manager:
//
// sm := NewSpinnerManager()
//
// Or this will initialize a manager with a custom animation:
//
// sm := NewSpinnerManager(
// WithAnimation(animations.Arrows)
// )
//
// You can pass in multiple options to the constructor:
//
// sm := NewSpinnerManager(
// WithAnimation(animations.Arrows),
// WithFrameDuration(time.Millisecond * 100),
// WithSpinnerColor(colors.Red),
// )
func NewSpinnerManager(options ...managerOption) SpinnerManager {
animationSpeed, animationChars := animations.GetAnimation(animations.Dots)
sm := &spinnerManager{
chars: animationChars,
frameDuration: animationSpeed,
spinnerColor: colors.FgHiGreen,
errorColor: colors.FgHiRed,
completeColor: colors.FgHiGreen,
messageColor: colors.NoColor,
writer: getWriter(),
done: make(chan bool),
hasUpdate: make(chan bool),
tty: tput.Tty(),
lines: 0,
}
for _, option := range options {
option(sm)
}
return sm
}
func getWriter() io.Writer {
// Windows support conveniently provided by github.com/mattn/go-colorable <3.
if runtime.GOOS == "windows" {
return colorable.NewColorableStdout()
} else {
return os.Stdout
}
}
// Option represents a spinner manager option.
type managerOption func(*spinnerManager)
// WithContext sets the context for spinner
func WithContext(context context.Context) managerOption {
return func(sm *spinnerManager) {
sm.context = context
}
}
// WithAnimation sets the animation used for the spinners.
// Available spinner types can be found in the package github.com/zioc/ysmrr/pkg/animations.
// The default spinner animation is the Dots.
func WithAnimation(a animations.Animation) managerOption {
return func(sm *spinnerManager) {
animationSpeed, animationChars := animations.GetAnimation(a)
sm.chars = animationChars
sm.frameDuration = animationSpeed
}
}
// WithFrameDuration sets the duration of each frame.
// The default duration is 250 milliseconds.
func WithFrameDuration(d time.Duration) managerOption {
return func(sm *spinnerManager) {
sm.frameDuration = d
}
}
// WithSpinnerColor sets the color of the spinners.
// Available colors can be found in the package github.com/zioc/ysmrr/pkg/colors.
// The default color is FgHiGreen.
func WithSpinnerColor(c colors.Color) managerOption {
return func(sm *spinnerManager) {
sm.spinnerColor = c
}
}
// WithErrorColor sets the color of the error icon.
// Available colors can be found in the package github.com/zioc/ysmrr/pkg/colors.
// The default color is FgHiRed.
func WithErrorColor(c colors.Color) managerOption {
return func(sm *spinnerManager) {
sm.errorColor = c
}
}
// WithCompleteColor sets the color of the complete icon.
// Available colors can be found in the package github.com/zioc/ysmrr/pkg/colors.
// The default color is FgHiGreen.
func WithCompleteColor(c colors.Color) managerOption {
return func(sm *spinnerManager) {
sm.completeColor = c
}
}
// WithMessageColor sets the color of the message.
// Available colors can be found in the package github.com/zioc/ysmrr/pkg/colors.
// The default color is NoColor.
func WithMessageColor(c colors.Color) managerOption {
return func(sm *spinnerManager) {
sm.messageColor = c
}
}
// WithWriter sets the writer used for the spinners.
// The writer can be anything that implements the io.Writer interface.
// The default writer is os.Stdout.
func WithWriter(w io.Writer) managerOption {
return func(sm *spinnerManager) {
sm.writer = w
}
}