-
Notifications
You must be signed in to change notification settings - Fork 7
/
manager.go
362 lines (312 loc) · 8.82 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
// Package ysmrr provides a simple interface for creating and managing
// multiple spinners.
package ysmrr
import (
"io"
"os"
"runtime"
"sync"
"time"
"github.com/chelnak/ysmrr/pkg/animations"
"github.com/chelnak/ysmrr/pkg/colors"
"github.com/chelnak/ysmrr/pkg/tput"
"github.com/mattn/go-colorable"
"github.com/mattn/go-isatty"
"golang.org/x/term"
)
// SpinnerManager manages spinners
type SpinnerManager interface {
AddSpinner(msg string) *Spinner
GetSpinners() []*Spinner
GetWriter() io.Writer
GetAnimation() []string
GetFrameDuration() time.Duration
GetSpinnerColor() colors.Color
GetErrorColor() colors.Color
GetCompleteColor() colors.Color
GetMessageColor() colors.Color
Start()
Stop()
Running() bool
}
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
done chan bool
running bool
hasUpdate chan bool
ticks *time.Ticker
frame int
tty bool
}
// AddSpinner adds a new spinner to the manager.
func (sm *spinnerManager) AddSpinner(message string) *Spinner {
opts := SpinnerOptions{
Message: message,
SpinnerColor: sm.spinnerColor,
CompleteColor: sm.completeColor,
ErrorColor: sm.errorColor,
MessageColor: sm.messageColor,
HasUpdate: sm.hasUpdate,
}
spinner := NewSpinner(opts)
sm.mutex.Lock()
defer sm.mutex.Unlock()
sm.spinners = append(sm.spinners, spinner)
return spinner
}
// GetSpinners returns the spinners managed by the manager.
func (sm *spinnerManager) GetSpinners() []*Spinner {
sm.mutex.RLock()
defer sm.mutex.RUnlock()
return sm.spinners
}
// Start signals that all spinners should start.
func (sm *spinnerManager) Start() {
sm.ticks = time.NewTicker(sm.frameDuration)
sm.running = true
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() {
if !sm.running {
return
}
sm.done <- true
sm.ticks.Stop()
sm.running = false
// 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])
}
}
// Running returns a boolean indicating whether the manager is running.
func (sm *spinnerManager) Running() bool {
return sm.running
}
// 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)
outer:
for {
select {
case <-sm.done:
break outer
case <-sm.hasUpdate:
sm.renderFrame(false)
case <-sm.ticks.C:
sm.renderFrame(true)
}
tput.Rc(sm.writer)
}
}
const defaultHeight = 24
func terminalHeight() int {
if runtime.GOOS == "windows" {
return defaultHeight
}
fd := 0
if !term.IsTerminal(fd) {
return defaultHeight
}
_, height, err := term.GetSize(fd)
if err != nil {
return defaultHeight
}
if height <= 0 {
return defaultHeight
}
return height
}
func (sm *spinnerManager) renderFrame(animate bool) {
if !sm.tty {
return
}
height := terminalHeight()
spinners := sm.GetSpinners()
maxVisible := height - 2
visibleSpinners := spinners
if len(spinners) > maxVisible {
visibleSpinners = spinners[len(spinners)-maxVisible:]
}
tput.BufScreen(sm.writer, len(visibleSpinners))
tput.Cuu(sm.writer, len(visibleSpinners))
tput.Sc(sm.writer)
for _, s := range visibleSpinners {
tput.ClearLine(sm.writer)
s.Print(sm.writer, sm.chars[sm.frame])
}
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: tty(),
}
for _, option := range options {
option(sm)
}
return sm
}
func tty() bool {
return isatty.IsTerminal(os.Stdout.Fd()) || os.Getenv("YSMRR_FORCE_TTY") == "true"
}
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)
// WithAnimation sets the animation used for the spinners.
// Available spinner types can be found in the package github.com/chelnak/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/chelnak/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/chelnak/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/chelnak/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/chelnak/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
}
}