/
spinner.go
205 lines (157 loc) · 4.74 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
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
// Package spinner provides methods for creating spinner animation for
// long-running tasks
package spinner
// ////////////////////////////////////////////////////////////////////////////////// //
// //
// Copyright (c) 2024 ESSENTIAL KAOS //
// Apache License, Version 2.0 <https://www.apache.org/licenses/LICENSE-2.0> //
// //
// ////////////////////////////////////////////////////////////////////////////////// //
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/essentialkaos/ek/v12/fmtc"
"github.com/essentialkaos/ek/v12/strutil"
"github.com/essentialkaos/ek/v12/timeutil"
)
// ////////////////////////////////////////////////////////////////////////////////// //
const (
_ACTION_DONE uint8 = iota
_ACTION_ERROR
_ACTION_SKIP
)
// ////////////////////////////////////////////////////////////////////////////////// //
// SpinnerColorTag is spinner animation color tag (see fmtc package)
var SpinnerColorTag = "{y}"
// OkColorTag is check color tag (see fmtc package)
var OkColorTag = "{g}"
// ErrColorTag is cross color tag (see fmtc package)
var ErrColorTag = "{r}"
// SkipColorTag is skipped action color tag (see fmtc package)
var SkipColorTag = "{s-}"
// TimeColorTag is time color tag (see fmtc package)
var TimeColorTag = "{s-}"
// OkSymbol contains symbol for action with no problems
var OkSymbol = "✔ "
// ErrSymbol contains symbol for action with problems
var ErrSymbol = "✖ "
// SkipSymbol contains symbol for skipped action
var SkipSymbol = "✔ "
// DisableAnimation is global animation off switch flag
var DisableAnimation = false
// ////////////////////////////////////////////////////////////////////////////////// //
var spinnerFrames = []string{"⠒", "⠲", "⠴", "⠤", "⠦", "⠇", "⠋", "⠉", "⠙", "⠸"}
var framesDelay = []time.Duration{
105 * time.Millisecond,
95 * time.Millisecond,
75 * time.Millisecond,
55 * time.Millisecond,
35 * time.Millisecond,
55 * time.Millisecond,
75 * time.Millisecond,
75 * time.Millisecond,
75 * time.Millisecond,
95 * time.Millisecond,
}
var desc string
var start time.Time
var isActive = &atomic.Bool{}
var isHidden = &atomic.Bool{}
var mu = &sync.RWMutex{}
// ////////////////////////////////////////////////////////////////////////////////// //
// Show shows spinner with given task description
func Show(message string, args ...any) {
if isActive.Load() {
return
}
mu.Lock()
desc = fmt.Sprintf(message, args...)
start = time.Now()
isActive.Store(true)
isHidden.Store(false)
if DisableAnimation {
isHidden.Store(true)
} else {
go showSpinner()
}
mu.Unlock()
}
// Update updates task description
func Update(message string, args ...any) {
if !isActive.Load() || isHidden.Load() {
return
}
mu.Lock()
desc = fmt.Sprintf(message, args...)
mu.Unlock()
}
// Done finishes spinner animation and shows task status
func Done(ok bool) {
if !isActive.Load() {
return
}
if ok {
stopSpinner(_ACTION_DONE)
} else {
stopSpinner(_ACTION_ERROR)
}
}
// Skip finishes spinner animation and mark it as skipped
func Skip() {
if !isActive.Load() {
return
}
stopSpinner(_ACTION_SKIP)
}
// ////////////////////////////////////////////////////////////////////////////////// //
func showSpinner() {
var i int
spinnerColorTag := strutil.B(fmtc.IsTag(SpinnerColorTag), SpinnerColorTag, "{y}")
timeColorTag := strutil.B(fmtc.IsTag(TimeColorTag), TimeColorTag, "{s-}")
for {
mu.RLock()
fmtc.Printf(spinnerColorTag+"%s {!}", spinnerFrames[i])
fmtc.Print(desc + "… ")
fmtc.Printf(timeColorTag+"[%s]{!}", timeutil.ShortDuration(time.Since(start)))
mu.RUnlock()
i++
if i == 10 {
i = 2
}
time.Sleep(framesDelay[i])
fmt.Print("\033[2K\r")
if !isActive.Load() {
isHidden.Store(true)
return
}
}
}
func stopSpinner(action uint8) {
isActive.Store(false)
for range time.NewTicker(time.Millisecond).C {
if isHidden.Load() {
break
}
}
mu.RLock()
timeColorTag := strutil.B(fmtc.IsTag(TimeColorTag), TimeColorTag, "{s-}")
switch action {
case _ACTION_ERROR:
errColorTag := strutil.B(fmtc.IsTag(ErrColorTag), ErrColorTag, "{r}")
fmtc.Printf(errColorTag + ErrSymbol + " {!}")
case _ACTION_SKIP:
skipColorTag := strutil.B(fmtc.IsTag(SkipColorTag), SkipColorTag, "{s-}")
fmtc.Printf(skipColorTag + SkipSymbol + " {!}")
default:
okColorTag := strutil.B(fmtc.IsTag(OkColorTag), OkColorTag, "{g}")
fmtc.Printf(okColorTag + OkSymbol + " {!}")
}
fmtc.Print(desc + " ")
fmtc.Printf(timeColorTag+"(%s){!}\n", timeutil.ShortDuration(time.Since(start), true))
mu.RUnlock()
mu.Lock()
desc, start = "", time.Time{}
mu.Unlock()
}