-
Notifications
You must be signed in to change notification settings - Fork 14
/
progress_bar.go
366 lines (314 loc) · 8.97 KB
/
progress_bar.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
package clif
import (
"fmt"
"strings"
"sync"
"time"
)
type (
ProgressBar interface {
// Done returns whether progress bar is done (position == size)
Done() bool
// Finish sets progress to max and
Finish()
// Increase adds given amount to progress
Increase(amount int) error
// Increment increases progress by one
Increment() error
// Position returns the progress position
Position() int
// Render returns the rendered progress bar in it's current position
Render() string
// RenderWidth returns the width of the rendered output (amount of chars (rune))
RenderWidth() int
// Reset sets position to zero and unsets all timers
Reset()
// Set moves the progress to given position
Set(position int) error
// SetRenderWidth sets total width of rendered output to given amount of characters (runes)
SetRenderWidth(v int) ProgressBar
// SetSize sets the total size of the progress bar
SetSize(v int) ProgressBar
// SetStyle sets the style of the progress bar
SetStyle(style *ProgressBarStyle) ProgressBar
// Style sets the
Style() *ProgressBarStyle
}
ProgressBarSimple struct {
// size is total length of progress
size int
// position is current location in progress
position int
// RenderWidth is the max size of characters
renderWidth int
// style is the rendering style (ASCII, UTF-8) of the progress bar.. i.e. the characters used
style *ProgressBarStyle
// started holds the first time
started time.Time
// stopped holds the time when Start() was called
stopped time.Time
mux *sync.Mutex
}
)
var (
// PbOutOfBoundError is returned when increasing, adding or setting the position
// with a value which is below 0 or beyond size of the progress bar
PbOutOfBoundError = fmt.Errorf("Position is out of bounds")
)
func NewProgressBar(size int) *ProgressBarSimple {
if size == 0 {
if s, err := TermWidth(); err != nil {
size = s
} else {
s = TERM_DEFAULT_WIDTH
}
}
return &ProgressBarSimple{
size: size,
renderWidth: TermWidthCurrent,
mux: new(sync.Mutex),
style: ProgressBarStyleUtf8,
}
}
// Done returns bool whether progress bar is done (at 100%)
func (this *ProgressBarSimple) Done() bool {
this.mux.Lock()
defer this.mux.Unlock()
return this.done()
}
func (this *ProgressBarSimple) done() bool {
return this.position == this.size
}
// Finish ends progress by skipping to the end
func (this *ProgressBarSimple) Finish() {
this.Set(this.size)
}
// Add increases progress by given amount
func (this *ProgressBarSimple) Increase(amount int) error {
this.mux.Lock()
defer this.mux.Unlock()
if this.position+amount > this.size {
return PbOutOfBoundError
}
this.position += amount
this.setTimes()
return nil
}
// Increase adds a one to progress
func (this *ProgressBarSimple) Increment() error {
return this.Increase(1)
}
// Position returns the current position
func (this *ProgressBarSimple) Position() int {
this.mux.Lock()
defer this.mux.Unlock()
return this.position
}
func (this *ProgressBarSimple) Reset() {
this.mux.Lock()
defer this.mux.Unlock()
this.position = 0
this.started = time.Time{}
this.stopped = time.Time{}
}
// Render returns rendered progress bar in current progress position
func (this *ProgressBarSimple) Render() string {
this.mux.Lock()
defer this.mux.Unlock()
this.setTimes()
out := ""
size := this.size
if size == 0 {
size = 1
}
percentage := float32(this.position) * 100 / float32(size)
infoPrefix := this.buildProgressInfo(percentage, size, PROGRESS_BAR_ADDON_PREPEND)
infoSuffix := this.buildProgressInfo(percentage, size, PROGRESS_BAR_ADDON_APPEND)
infoSize := StringLength(infoPrefix) + StringLength(infoSuffix) +
StringLength(string(this.style.LeftBorder)) + StringLength(string(this.style.RightBorder))
width := this.renderWidth - infoSize
if width == 0 {
width = 1
}
out += infoPrefix
out += string(this.style.LeftBorder)
progress := int(percentage * float32(width) / 100)
none := width - progress
if progress > 1 {
if progress < width {
out += strings.Repeat(string(this.style.Progress), progress-1)
} else {
out += strings.Repeat(string(this.style.Progress), progress)
}
}
if progress > 0 && progress < width {
out += string(this.style.Rightmost)
}
diff := width - (none + progress)
if diff != 0 {
none += diff
}
if none > 0 {
out += strings.Repeat(string(this.style.None), none)
}
out += string(this.style.RightBorder)
out += infoSuffix
return out
}
// Render returns rendered progress bar in current progress position
func (this *ProgressBarSimple) RenderWidth() int {
return this.renderWidth
}
// Set moves progress to given position (must be between 0 and size)
func (this *ProgressBarSimple) Set(position int) error {
this.mux.Lock()
defer this.mux.Unlock()
if position < 0 || position > this.size {
return PbOutOfBoundError
}
this.position = position
this.setTimes()
return nil
}
// SetRenderWidth is builder method to set render width (defaults to PB_DEFAULT_RENDER_WIDTH)
func (this *ProgressBarSimple) SetRenderWidth(v int) ProgressBar {
this.renderWidth = v
return this
}
// SetSize is builder method to set size (i.e. max length) of progress
func (this *ProgressBarSimple) SetSize(v int) ProgressBar {
this.size = v
return this
}
// SetStyle sets the rendering (output) style (i.e. the characters used to print the progress bar)
func (this *ProgressBarSimple) SetStyle(v *ProgressBarStyle) ProgressBar {
this.style = v
return this
}
// Style returns the currently used style
func (this *ProgressBarSimple) Style() *ProgressBarStyle {
return this.style
}
func (this *ProgressBarSimple) buildProgressInfo(percentage float32, size int, pos progressBarAddon) string {
// count, elapsed, estimate, percentage
out := []string{"", "", "", ""}
if this.style.Count == pos {
out[0] = this.renderCount(size)
}
if this.style.Elapsed == pos {
out[1] = this.renderElapsed()
}
if this.style.Estimate == pos {
out[2] = this.renderEstimate(size)
}
if this.style.Percentage == pos {
out[3] = this.renderPercentage(percentage)
}
if pos == PROGRESS_BAR_ADDON_APPEND {
return this.style.RenderSuffix(out[0], out[1], out[2], out[3])
} else {
return this.style.RenderPrefix(out[0], out[1], out[2], out[3])
}
}
func (this *ProgressBarSimple) setTimes() {
if this.started.Year() <= 1 {
this.started = time.Now()
}
if this.size == this.position {
if this.stopped.Year() <= 1 {
this.stopped = time.Now()
}
} else {
this.stopped = time.Time{}
}
}
func (this *ProgressBarSimple) renderElapsed() string {
var duration time.Duration
if this.done() {
duration = this.stopped.Sub(this.started)
} else {
duration = time.Now().Sub(this.started)
}
return this.style.RenderElapsed(duration, this)
}
func (this *ProgressBarSimple) renderCount(size int) string {
return this.style.RenderCount(this.position, size, this)
}
func (this *ProgressBarSimple) renderPercentage(percentage float32) string {
return this.style.RenderPercentage(percentage, this)
}
func (this *ProgressBarSimple) renderEstimate(size int) string {
if this.done() {
return this.style.RenderEstimate(time.Duration(0), this)
}
duration := time.Now().Sub(this.started)
var expected uint64
if this.position > 0 {
// dur / pos = total / size
// total = dur * size / pos
expected = uint64(float64(duration.Nanoseconds()) * float64(size) / float64(this.position))
}
remaining := time.Duration(expected - uint64(duration.Nanoseconds()))
return this.style.RenderEstimate(remaining, this)
}
var (
pbYear = float64(365 * 24)
pbWeek = float64(7 * 24)
pbDay = float64(24)
)
func (this *ProgressBarSimple) renderFixedSizeDuration(dur time.Duration) string {
h := dur.Hours()
m := dur.Minutes()
if h > pbYear*10 {
y := int(h / pbYear)
h -= float64(y) * pbYear
w := h / pbWeek
return fmt.Sprintf("%02dy%02dw", y, int(w))
} else if h > pbWeek*10 {
return fmt.Sprintf("%05dw", int(h/pbWeek))
} else if h > pbDay*2 {
d := int(h / pbDay)
h -= pbDay * float64(d)
return fmt.Sprintf("%02dd%02dh", d, int(h))
} else if h > 1 {
o := int(h)
i := m - float64(o)*60
return fmt.Sprintf("%02dh%02dm", o, int(i))
} else if dur.Seconds() < 0 {
return "00m00s"
} else {
i := int(m)
s := dur.Seconds() - float64(i)*60
return fmt.Sprintf("%02dm%02ds", i, int(s))
}
}
func RenderFixedSizeDuration(dur time.Duration) string {
h := dur.Hours()
m := dur.Minutes()
s := dur.Seconds()
if h > pbYear*10 {
y := int(h / pbYear)
h -= float64(y) * pbYear
w := h / pbWeek
return fmt.Sprintf("%02dy%02dw", y, int(w))
} else if h > pbWeek*10 {
return fmt.Sprintf("%05dw", int(h/pbWeek))
} else if h > pbDay*2 {
d := int(h / pbDay)
h -= pbDay * float64(d)
return fmt.Sprintf("%02dd%02dh", d, int(h))
} else if h > 1 {
o := int(h)
i := m - float64(o)*60
return fmt.Sprintf("%02dh%02dm", o, int(i))
} else if s > 99 {
i := int(m)
o := s - float64(i)*60
return fmt.Sprintf("%02dm%02ds", i, int(o))
} else if s < 1 {
return "00m00s"
} else {
ms := (s - float64(int(s))) * 1000
return fmt.Sprintf("%02ds%03d", int(s), int(ms))
}
}