forked from vbauerster/mpb
/
decorators.go
298 lines (276 loc) · 8.51 KB
/
decorators.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
package decor
import (
"fmt"
"time"
runewidth "github.com/mattn/go-runewidth"
)
const (
// DidentRight specifies identation direction.
// |foo |b | With DidentRight
// | foo| b| Without DidentRight
DidentRight = 1 << iota
// DwidthSync will auto sync max width.
// Makes sense when there're more than one bar
DwidthSync
// DextraSpace adds extra space, makes sense with DwidthSync only.
// When DidentRight bit set, the space will be added to the right,
// otherwise to the left.
DextraSpace
// DSyncSpace is shortcut for DwidthSync|DextraSpace
DSyncSpace = DwidthSync | DextraSpace
)
// Statistics represents statistics of the progress bar.
// Cantains: Total, Current, TimeElapsed, TimePerItemEstimate
type Statistics struct {
ID int
Completed bool
Aborted bool
Total int64
Current int64
StartTime time.Time
TimeElapsed time.Duration
TimePerItemEstimate time.Duration
}
const obviousEta bool = true
// Eta returns exponential-weighted-moving-average ETA estimator
func (s *Statistics) Eta() time.Duration {
var eta time.Duration
if obviousEta {
// Go with the long/slow/stupid ETA
nsec := float64(s.Current) / s.TimeElapsed.Seconds()
eta = time.Duration(float64(s.Total-s.Current)/nsec) * time.Second
} else {
dur := time.Duration(s.Total - s.Current)
eta = dur * s.TimePerItemEstimate
}
return eta
}
// DecoratorFunc is a function that can be prepended and appended to the progress bar
type DecoratorFunc func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string
// Name deprecated, use StaticName instead
func Name(name string, minWidth int, conf byte) DecoratorFunc {
return StaticName(name, minWidth, conf)
}
// StaticName to be used, when there is no plan to change the name during whole
// life of a progress rendering process
func StaticName(name string, minWidth int, conf byte) DecoratorFunc {
nameFn := func(s *Statistics) string {
return name
}
return DynamicName(nameFn, minWidth, conf)
}
// DynamicName to be used, when there is a plan to change the name once or
// several times during progress rendering process. If there're more than one
// bar, and you'd like to synchronize column width, conf param should have
// DwidthSync bit set.
func DynamicName(nameFn func(*Statistics) string, minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
name := nameFn(s)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(name)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), name)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), name)
}
}
// Counters provides basic counters decorator.
// Accepts pairFormat string, something like "%s / %s" to be used in
// fmt.Sprintf(pairFormat, current, total) and one of (Unit_KiB/Unit_kB)
// constant. If there're more than one bar, and you'd like to synchronize column
// width, conf param should have DwidthSync bit set.
func CountersString(s *Statistics, pairFormat string, unit Units) string {
current := Format(s.Current).To(unit)
total := Format(s.Total).To(unit)
str := fmt.Sprintf(pairFormat, current, total)
return str
}
func Counters(pairFormat string, unit Units, minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := CountersString(s, pairFormat, unit)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(str)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), str)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
}
}
// Nsec provides basic Num/sec decorator.
// Accepts string, something like "%s/s" to be used in
// fmt.Sprintf(nsecformat, current) and one of (Unit_KiB/Unit_kB)
// constant. If there're more than one bar, and you'd like to synchronize column
// width, conf param should have DwidthSync bit set.
func NsecString(s *Statistics, nsecformat string, unit Units) string {
var nsec float64
if s.Current > 0 {
if obviousEta {
nsec = float64(s.Current) / s.TimeElapsed.Seconds()
} else {
nsec = time.Duration(time.Second).Seconds()
nsec /= s.TimePerItemEstimate.Seconds()
if s.Current == s.Total {
nsec = float64(s.Current) / s.TimeElapsed.Seconds()
}
}
}
current := FormatF(nsec).To(unit)
str := fmt.Sprintf(nsecformat, current)
return str
}
func Nsec(nsecformat string, unit Units, minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := NsecString(s, nsecformat, unit)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(str)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), str)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
}
}
// ETA provides exponential-weighted-moving-average ETA decorator, shows the
// elapsed time after the progress has finished.
// If there're more than one bar, and you'd like to synchronize column width,
// conf param should have DwidthSync bit set.
func ETAString(s *Statistics) string {
var dur time.Duration
if s.Current == s.Total {
dur = s.TimeElapsed
} else {
dur = s.Eta()
}
var str string
secs := int(dur.Seconds()) % 60
if s.Current == 0 {
str = "∞:??"
} else if dur.Minutes() > 999 {
str = fmt.Sprintf("∞:%02d", secs)
} else {
str = fmt.Sprintf("%d:%02d", int(dur.Minutes()), secs)
}
return str
}
func ETA(minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := ETAString(s)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(str)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), str)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
}
}
// Elapsed provides elapsed time decorator.
// If there're more than one bar, and you'd like to synchronize column width,
// conf param should have DwidthSync bit set.
func ElapsedString(s *Statistics) string {
str := fmt.Sprint(time.Duration(s.TimeElapsed.Seconds()) * time.Second)
return str
}
func Elapsed(minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := ElapsedString(s)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(str)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), str)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
}
}
// Percentage provides percentage decorator.
// If there're more than one bar, and you'd like to synchronize column width,
// conf param should have DwidthSync bit set.
func PercentageString(s *Statistics) string {
str := " "
if s.Current > 0 && s.Current < s.Total {
// Don't round up to 100%
pc := (100 * s.Current) / s.Total
str = fmt.Sprintf("%2d%%", pc)
}
return str
}
func Percentage(minWidth int, conf byte) DecoratorFunc {
format := "%%"
if (conf & DidentRight) != 0 {
format += "-"
}
format += "%ds"
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := PercentageString(s)
if (conf & DwidthSync) != 0 {
myWidth <- runewidth.StringWidth(str)
max := <-maxWidth
if (conf & DextraSpace) != 0 {
max++
}
return fmt.Sprintf(fmt.Sprintf(format, max), str)
}
return fmt.Sprintf(fmt.Sprintf(format, minWidth), str)
}
}
func DefDataPreBar(unit Units) DecoratorFunc {
return func(s *Statistics, myWidth chan<- int, maxWidth <-chan int) string {
str := NsecString(s, "%s/s ", unit)
str += CountersString(s, "%s%.0s", unit)
pc := PercentageString(s)
if pc != "" {
str += " "
str += pc
}
return str
}
}
func CalcPercentage(total, current int64, width, fill int) (int, int) {
if total == 0 || current > total {
return 0, 0
}
num := float64(width) * float64(current) / float64(total)
if fill > 0 {
rem := num - float64(int(num))
return int(num), int(rem / (1.0 / float64(fill)))
}
return int(round(num, 1)), 0
}