forked from d3mondev/puredns
-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressbar.go
316 lines (248 loc) · 6.77 KB
/
progressbar.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
package progressbar
import (
"fmt"
"io"
"os"
"regexp"
"strings"
"time"
)
// ProgressBar is an asynchronous progress bar that can perform polling updates.
type ProgressBar struct {
template string
updateCB Update
style Style
updateInterval time.Duration
writer io.Writer
doneChan chan bool
finishedChan chan bool
vars map[interface{}]interface{}
current int64
total int64
rate *MovingRate
startTime time.Time
finishTime time.Time
}
// Update is a function that updates the progress bar.
type Update func(bar *ProgressBar)
// New creates a new progress bar from a template string and update function.
func New(update Update, total int64, opts ...Option) *ProgressBar {
// Default options
config := options{
updateInterval: 200 * time.Millisecond,
template: "[ETA {{ eta }}] {{ bar }} {{ current }}/{{ total }} {{ rate }}/s (time: {{ time }})",
writer: os.Stderr,
style: DefaultStyle(),
}
// Apply options
for _, o := range opts {
o.apply(&config)
}
return &ProgressBar{
template: config.template,
updateCB: update,
style: config.style,
updateInterval: config.updateInterval,
writer: config.writer,
vars: make(map[interface{}]interface{}),
total: total,
rate: NewMovingRate(time.Second, 10),
}
}
// Start starts a progress bar.
func (p *ProgressBar) Start() {
// Start the bar
p.rate.Start()
p.startTime = time.Now()
// Create the channels
p.doneChan = make(chan bool)
p.finishedChan = make(chan bool)
// Main loop for the progress bar
go func() {
var last bool
// Update the progress bar every interval
ticker := time.NewTicker(p.updateInterval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
p.updateCB(p)
p.update()
fmt.Fprintf(p.writer, "\r%s", p.Render())
if last {
fmt.Fprintln(p.writer, "")
p.finishedChan <- true
return
}
case <-p.doneChan:
last = true
}
}
}()
}
// Stop signals the progress bar to perform one last update and waits for it to finish.
func (p *ProgressBar) Stop() {
// Signal the progress bar to perform one last update
p.doneChan <- true
// Wait for the progress bar to terminate
<-p.finishedChan
// Stop the rate calculation
p.rate.Stop()
// Record the finish time if it hasn't already been set
if p.finishTime.IsZero() {
p.finishTime = time.Now()
}
}
// Set sets or updates a key value pair on the progress bar.
func (p *ProgressBar) Set(key interface{}, value interface{}) {
p.vars[key] = value
}
// Get returns a key value if it exists, otherwise it returns nil.
func (p *ProgressBar) Get(key interface{}) interface{} {
if val, ok := p.vars[key]; ok {
return val
}
return nil
}
// SetCurrent sets the current progress bar value.
func (p *ProgressBar) SetCurrent(current int64) {
diff := current - p.current
if diff <= 0 {
return
}
p.Increment(diff)
}
// Increment increments the progress bar by the specified value.
func (p *ProgressBar) Increment(val int64) {
p.rate.Sample(float64(val))
p.current = p.current + val
if p.current == p.total {
if p.finishTime.IsZero() {
p.finishTime = time.Now()
}
}
}
// Current returns the current progress bar value.
func (p *ProgressBar) Current() int64 {
return p.current
}
// Total returns the progress bar total value.
func (p *ProgressBar) Total() int64 {
return p.total
}
// Rate returns the current rate.
func (p *ProgressBar) Rate() float64 {
rate, _ := p.rate.Current()
return rate
}
// ETA returns the current estimated time before the task finishes.
func (p *ProgressBar) ETA() (hours, minutes, seconds int) {
current := p.Current()
total := p.Total()
remaining := total - current
rate := p.Rate()
if total == 0 {
return 0, 0, 0
}
if remaining <= 0 || current == total {
return 0, 0, 0
}
if rate == 0.0 {
return 99, 59, 59
}
left := float64(remaining) / rate
return convertTime(left)
}
// Time returns the time since the progress bar has been started until either it is stopped,
// or the current counter reaches the total.
func (p *ProgressBar) Time() (hours, minutes, seconds int) {
// If the progress bar hasn't been started, the time is zero
if p.startTime.IsZero() {
return 0, 0, 0
}
var duration time.Duration
if p.finishTime.IsZero() {
// Still running, take the time since start
duration = time.Since(p.startTime)
} else {
// Finished, take the time since finish
duration = p.finishTime.Sub(p.startTime)
}
return convertTime(duration.Seconds())
}
// Render returns a string containing the progress bar.
func (p *ProgressBar) Render() string {
render := parseVariables(p.template, p.vars)
return render
}
// update updates the default internal values
func (p *ProgressBar) update() {
// Update time
th, tm, ts := p.Time()
p.Set("time", fmt.Sprintf("%02d:%02d:%02d", th, tm, ts))
// Update rate
rate := p.Rate()
p.Set("rate", fmt.Sprintf("%.f", rate))
// Update ETA
eh, em, es := p.ETA()
p.Set("eta", fmt.Sprintf("%02d:%02d:%02d", eh, em, es))
// Update current
current := p.Current()
p.Set("current", fmt.Sprintf("%d", current))
// Update total
total := p.Total()
p.Set("total", fmt.Sprintf("%d", total))
// Update percentage
perc := float64(current) / float64(total) * 100.0
p.Set("percent", fmt.Sprintf("%.f", perc))
// Update bar
p.Set("bar", p.drawBar(perc))
}
// drawBar renders the progress bar
func (p *ProgressBar) drawBar(percent float64) string {
width := 40
barWidth := width - 2
var builder strings.Builder
builder.WriteString(string(p.style.BarPrefixColor))
builder.WriteRune('|')
builder.WriteString(string(p.style.BarFullColor))
setEmptyColor := true
for i := 0; i < barWidth; i++ {
cur := float64(i) / float64(barWidth) * 100.0
if cur < percent {
builder.WriteRune('█')
} else {
if setEmptyColor {
builder.WriteString(string(p.style.BarEmptyColor))
setEmptyColor = false
}
builder.WriteRune('░')
}
}
builder.WriteString(string(p.style.BarSuffixColor))
builder.WriteRune('|')
builder.WriteString(string(ColorReset))
return builder.String()
}
var rxVariableMatch = regexp.MustCompile(`{{\s*([a-zA-Z0-9-_.]+)\s*}}`)
// parseVariables replaces {{ variables }} in a string by their value from the variable map
func parseVariables(str string, vars map[interface{}]interface{}) string {
matches := rxVariableMatch.FindAllStringSubmatch(str, -1)
for _, group := range matches {
expression := group[0]
key := group[1]
if value, ok := vars[key]; ok {
str = strings.Replace(str, expression, value.(string), 1)
}
}
return str
}
// converTime takes a time in seconds and converts it to hours, minutes, seconds.
func convertTime(t float64) (hours, minutes, seconds int) {
hours = int(t / 3600.0)
t -= float64(hours * 3600)
minutes = int(t / 60.0)
t -= float64(minutes * 60)
seconds = int(t)
return hours, minutes, seconds
}