-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spintron.go
385 lines (329 loc) · 10 KB
/
spintron.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
377
378
379
380
381
382
383
384
385
// Copyright (c) 2021 Brian J. Downs
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package spinner is a simple package to add a spinner / progress indicator to any terminal application.
package spintron
import (
"fmt"
"io"
"os"
"runtime"
"strconv"
"strings"
"sync"
"time"
"unicode/utf8"
logSymbols "github.com/defaltd/log-symbols"
"github.com/fatih/color"
"github.com/mattn/go-isatty"
)
// returns true if the OS is windows and the WT_SESSION env variable is set.
var isWindowsTerminalOnWindows = len(os.Getenv("WT_SESSION")) > 0 && runtime.GOOS == "windows"
// Spinner struct to hold the provided options.
type Spinner struct {
mu *sync.RWMutex
Delay time.Duration // Delay is the speed of the indicator
chars []string // chars holds the chosen character set
Text string // Text shown after the Spinner
lastOutput string // last character(set) written
color func(a ...interface{}) string // default color is white
Writer io.Writer // to make testing better, exported so users have access. Use `WithWriter` to update after initialization.
active bool // active holds the state of the spinner
stopChan chan struct{} // stopChan is a channel used to stop the indicator
HideCursor bool // hideCursor determines if the cursor is visible
PreUpdate func(s *Spinner) // will be triggered before every spinner update
PostUpdate func(s *Spinner) // will be triggered after every spinner update
Symbol string // Symbol for the spinner, show before PrefixText
PrefixText string // PrefixText for the spinner, shown before the spinner and after the Symbol
Padding int // Padding for the spinner
ShowElaspedSeconds bool // ShowElaspedSeconds determines if the spinner should show the elapsed time
secondsElasped int // Number of seconds elapsed since the spinner was started
}
// New provides a pointer to an instance of Spinner with the supplied options.
func New(options Options) *Spinner {
s := &Spinner{
Delay: 100 * time.Millisecond,
chars: CharSets[11],
color: color.New(color.FgCyan).SprintFunc(),
mu: &sync.RWMutex{},
Writer: color.Output,
stopChan: make(chan struct{}, 1),
active: false,
HideCursor: true,
ShowElaspedSeconds: true,
}
if options.Writer != nil {
s.mu.Lock()
s.Writer = options.Writer
s.mu.Unlock()
}
if options.PrefixText != "" {
s.PrefixText = options.PrefixText
}
if options.Symbol != "" {
s.Symbol = options.Symbol
}
if options.HideCursor {
s.HideCursor = options.HideCursor
}
if options.Symbol != "" {
s.Symbol = options.Symbol
}
if options.Color != "" {
s.Color(options.Color)
}
if options.Text != "" {
s.Text = options.Text
}
if options.Delay != 0 {
s.Delay = options.Delay
}
if options.Padding != 0 {
s.Padding = options.Padding
}
if options.DisableElaspedSeconds {
s.ShowElaspedSeconds = false
}
return s
}
// Options contains fields to configure the spinner.
type Options struct {
Color string
Text string
HideCursor bool
Symbol string
PrefixText string
CharacterSet []string
Writer io.Writer
Delay time.Duration
Padding int
DisableElaspedSeconds bool
}
// Start will start the spinner.
func (s *Spinner) Start() {
s.mu.Lock()
if s.active || !isRunningInTerminal() {
s.mu.Unlock()
return
}
if s.HideCursor && !isWindowsTerminalOnWindows {
// hides the cursor
fmt.Fprint(s.Writer, "\033[?25l")
}
s.active = true
s.mu.Unlock()
go func() {
for {
s.secondsElasped += 1
time.Sleep(time.Second * 1)
}
}()
go func() {
for {
for i := 0; i < len(s.chars); i++ {
select {
case <-s.stopChan:
return
default:
s.mu.Lock()
if !s.active {
s.mu.Unlock()
return
}
if !isWindowsTerminalOnWindows {
s.erase()
}
if s.PreUpdate != nil {
s.PreUpdate(s)
}
var fullSymbol string
if s.Symbol != "" {
fullSymbol = s.Symbol + " "
} else {
fullSymbol = ""
}
var fullPrefixText string
if s.PrefixText != "" {
fullPrefixText = s.PrefixText + " "
} else {
fullPrefixText = ""
}
var fullText string
if s.Text != "" {
fullText = " " + s.Text
} else {
fullText = ""
}
var charStyled string
if runtime.GOOS == "windows" {
if s.Writer == os.Stderr {
charStyled = s.chars[i]
} else {
charStyled = s.color(s.chars[i])
}
} else {
charStyled = s.color(s.chars[i])
}
var padding string
if s.Padding > 0 {
padding = strings.Repeat(" ", s.Padding)
}
var elaspedSeconds string
if s.ShowElaspedSeconds {
elaspedSeconds = color.New(color.FgHiBlack).SprintFunc()(" [" + strconv.Itoa(s.secondsElasped) + "s]")
} else {
elaspedSeconds = ""
}
outColor := fmt.Sprintf("\r%s%s%s%s%s%s", padding, fullSymbol, fullPrefixText, charStyled, fullText, elaspedSeconds)
outPlain := fmt.Sprintf("\r%s%s%s%s%s%v", padding, fullSymbol, fullPrefixText, s.chars[i], fullText, s.secondsElasped)
fmt.Fprint(s.Writer, outColor)
s.lastOutput = outPlain
delay := s.Delay
if s.PostUpdate != nil {
s.PostUpdate(s)
}
s.mu.Unlock()
time.Sleep(delay)
}
}
}
}()
}
// Stops the spinner.
func (s *Spinner) Stop() {
s.mu.Lock()
defer s.mu.Unlock()
if s.active {
s.active = false
if s.HideCursor && !isWindowsTerminalOnWindows {
// makes the cursor visible
fmt.Fprint(s.Writer, "\033[?25h")
}
s.erase()
s.stopChan <- struct{}{}
}
}
// Stops the spinner and prits out a message, used later for success, fail, etc.
func (s *Spinner) StopAndPersist(symbol string, text string) {
s.Stop()
var fullSymbol string
if s.Symbol != "" {
fullSymbol = s.Symbol + " "
} else {
fullSymbol = ""
}
var fullText string
if text != "" {
fullText = " " + text
} else {
fullText = ""
}
var padding string
if s.Padding > 0 {
padding = strings.Repeat(" ", s.Padding)
}
fmt.Fprintf(s.Writer, "\r%s%s%s%s\n", padding, fullSymbol, symbol, fullText)
}
// Stops the spinner and prints out a success message.
func (s *Spinner) Succeed(text string) {
s.StopAndPersist(logSymbols.SUCCESS, text)
}
// Stops the spinner and prints out a failure message.
func (s *Spinner) Fail(text string) {
s.StopAndPersist(logSymbols.ERROR, text)
}
// Stops the spinner and prints out an info message.
func (s *Spinner) Info(text string) {
s.StopAndPersist(logSymbols.INFO, text)
}
// Restart will stop and start the indicator.
func (s *Spinner) Restart() {
s.Stop()
s.Start()
}
// Reverse will reverse the order of the slice assigned to the indicator.
func (s *Spinner) Reverse() {
s.mu.Lock()
for i, j := 0, len(s.chars)-1; i < j; i, j = i+1, j-1 {
s.chars[i], s.chars[j] = s.chars[j], s.chars[i]
}
s.mu.Unlock()
}
// Color will set the struct field for the given color to be used. The spinner
// will need to be explicitly restarted.
func (s *Spinner) Color(colors ...string) error {
colorAttributes := make([]color.Attribute, len(colors))
// Verify colours are valid and place the appropriate attribute in the array
for index, c := range colors {
if !validColor(c) {
return errInvalidColor
}
colorAttributes[index] = colorAttributeMap[c]
}
s.mu.Lock()
s.color = color.New(colorAttributes...).SprintFunc()
s.mu.Unlock()
return nil
}
// UpdateSpeed will set the indicator delay to the given value.
func (s *Spinner) UpdateSpeed(d time.Duration) {
s.mu.Lock()
s.Delay = d
s.mu.Unlock()
}
// UpdateCharSet will change the current character set to the given one.
func (s *Spinner) UpdateCharSet(cs []string) {
s.mu.Lock()
s.chars = cs
s.mu.Unlock()
}
// erase deletes written characters on the current line.
// Caller must already hold s.lock.
func (s *Spinner) erase() {
n := utf8.RuneCountInString(s.lastOutput)
if runtime.GOOS == "windows" && !isWindowsTerminalOnWindows {
clearString := "\r" + strings.Repeat(" ", n) + "\r"
fmt.Fprint(s.Writer, clearString)
s.lastOutput = ""
return
}
// Taken from https://en.wikipedia.org/wiki/ANSI_escape_code:
// \r - Carriage return - Moves the cursor to column zero
// \033[K - Erases part of the line. If n is 0 (or missing), clear from
// cursor to the end of the line. If n is 1, clear from cursor to beginning
// of the line. If n is 2, clear entire line. Cursor position does not
// change.
fmt.Fprintf(s.Writer, "\r\033[K")
s.lastOutput = ""
}
// Lock allows for manual control to lock the spinner.
func (s *Spinner) Lock() {
s.mu.Lock()
}
// Unlock allows for manual control to unlock the spinner.
func (s *Spinner) Unlock() {
s.mu.Unlock()
}
// GenerateNumberSequence will generate a slice of integers at the
// provided length and convert them each to a string.
func GenerateNumberSequence(length int) []string {
numSeq := make([]string, length)
for i := 0; i < length; i++ {
numSeq[i] = strconv.Itoa(i)
}
return numSeq
}
// isRunningInTerminal check if stdout file descriptor is terminal
func isRunningInTerminal() bool {
return isatty.IsTerminal(os.Stdout.Fd())
}