forked from abiosoft/ishell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progress.go
295 lines (257 loc) · 7.05 KB
/
progress.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
package ishell
import (
"fmt"
"io"
"sync"
"time"
"unicode/utf8"
)
// ProgressDisplay handles the display string for
// a progress bar.
type ProgressDisplay interface {
// Determinate returns the strings to display
// for percents 0 to 100.
Determinate() [101]string
// Indeterminate returns the strings to display
// at interval.
Indeterminate() []string
}
// ProgressBar is an ishell progress bar.
type ProgressBar interface {
// Display sets the display of the progress bar.
Display(ProgressDisplay)
// Indeterminate sets the progress bar type
// to indeterminate if true or determinate otherwise.
Indeterminate(bool)
// Interval sets the time between transitions for indeterminate
// progress bar.
Interval(time.Duration)
// SetProgress sets the progress stage of the progress bar.
// percent is from between 1 and 100.
Progress(percent int)
// Prefix sets the prefix for the output. The text to place before
// the display.
Prefix(string)
// Suffix sets the suffix for the output. The text to place after
// the display.
Suffix(string)
// Final sets the string to show after the progress bar is done.
Final(string)
// Start starts the progress bar.
Start()
// Stop stops the progress bar.
Stop()
}
const progressInterval = time.Millisecond * 100
type progressBarImpl struct {
display ProgressDisplay
indeterminate bool
interval time.Duration
iterator iterator
percent int
prefix string
suffix string
final string
writer io.Writer
writtenLen int
running bool
wait chan struct{}
wMutex sync.Mutex
sync.Mutex
}
func newProgressBar(s *Shell) ProgressBar {
display := simpleProgressDisplay{}
return &progressBarImpl{
interval: progressInterval,
writer: s.writer,
display: display,
iterator: &stringIterator{set: display.Indeterminate()},
indeterminate: true,
}
}
func (p *progressBarImpl) Display(display ProgressDisplay) {
p.display = display
}
func (p *progressBarImpl) Indeterminate(b bool) {
p.indeterminate = b
}
func (p *progressBarImpl) Interval(t time.Duration) {
p.interval = t
}
func (p *progressBarImpl) Progress(percent int) {
if percent < 0 {
percent = 0
} else if percent > 100 {
percent = 100
}
p.percent = percent
p.indeterminate = false
p.refresh()
}
func (p *progressBarImpl) Prefix(prefix string) {
p.prefix = prefix
}
func (p *progressBarImpl) Suffix(suffix string) {
p.suffix = suffix
}
func (p *progressBarImpl) Final(s string) {
p.final = s
}
func (p *progressBarImpl) write(s string) error {
p.erase(p.writtenLen)
p.writtenLen = utf8.RuneCountInString(s)
_, err := p.writer.Write([]byte(s))
return err
}
func (p *progressBarImpl) erase(n int) {
for i := 0; i < n; i++ {
p.writer.Write([]byte{'\b'})
}
}
func (p *progressBarImpl) done() {
p.wMutex.Lock()
defer p.wMutex.Unlock()
p.erase(p.writtenLen)
fmt.Fprintln(p.writer, p.final)
}
func (p *progressBarImpl) output() string {
p.Lock()
defer p.Unlock()
var display string
if p.indeterminate {
display = p.iterator.next()
} else {
display = p.display.Determinate()[p.percent]
}
return fmt.Sprintf("%s%s%s ", p.prefix, display, p.suffix)
}
func (p *progressBarImpl) refresh() {
p.wMutex.Lock()
defer p.wMutex.Unlock()
p.write(p.output())
}
func (p *progressBarImpl) Start() {
p.Lock()
p.running = true
p.wait = make(chan struct{})
p.Unlock()
go func() {
for {
var running, indeterminate bool
p.Lock()
running = p.running
indeterminate = p.indeterminate
p.Unlock()
if !running {
break
}
time.Sleep(p.interval)
if indeterminate {
p.refresh()
}
}
p.done()
close(p.wait)
}()
}
func (p *progressBarImpl) Stop() {
p.Lock()
p.running = false
p.Unlock()
<-p.wait
}
// ProgressDisplayCharSet is the character set for
// a progress bar.
type ProgressDisplayCharSet []string
// Determinate satisfies ProgressDisplay interface.
func (p ProgressDisplayCharSet) Determinate() [101]string {
// TODO everything here works but not pleasing to the eyes
// and probably not optimal.
// This should be cleaner.
var set [101]string
for i := range set {
set[i] = p[len(p)-1]
}
// assumption is than len(p) <= 101
step := 101 / len(p)
for i, j := 0, 0; i < len(set) && j < len(p); i, j = i+step, j+1 {
for k := 0; k < step && i+k < len(set); k++ {
set[i+k] = p[j]
}
}
return set
}
// Indeterminate satisfies ProgressDisplay interface.
func (p ProgressDisplayCharSet) Indeterminate() []string {
return p
}
// ProgressDisplayFunc is a convenience function to create a ProgressDisplay.
// percent is -1 for indeterminate and 0-100 for determinate.
type ProgressDisplayFunc func(percent int) string
// Determinate satisfies ProgressDisplay interface.
func (p ProgressDisplayFunc) Determinate() [101]string {
var set [101]string
for i := range set {
set[i] = p(i)
}
return set
}
// Indeterminate satisfies ProgressDisplay interface.
func (p ProgressDisplayFunc) Indeterminate() []string {
// loop through until we get back to the first string
set := []string{p(-1)}
for {
next := p(-1)
if next == set[0] {
break
}
set = append(set, next)
}
return set
}
type iterator interface {
next() string
}
type stringIterator struct {
index int
set []string
}
func (s *stringIterator) next() string {
current := s.set[s.index]
s.index++
if s.index >= len(s.set) {
s.index = 0
}
return current
}
var (
indeterminateCharSet = []string{
"[==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ====]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
"[ ==== ]", "[ ==== ]", "[ ==== ]",
}
determinateCharSet = []string{
"[ ]", "[> ]", "[=> ]",
"[==> ]", "[===> ]", "[====> ]",
"[=====> ]", "[======> ]", "[=======> ]",
"[========> ]", "[=========> ]", "[==========> ]",
"[===========> ]", "[============> ]", "[=============> ]",
"[==============> ]", "[===============> ]", "[================> ]",
"[=================> ]", "[==================> ]", "[===================>]",
}
)
type simpleProgressDisplay struct{}
func (s simpleProgressDisplay) Determinate() [101]string {
return ProgressDisplayCharSet(determinateCharSet).Determinate()
}
func (s simpleProgressDisplay) Indeterminate() []string {
return indeterminateCharSet
}