Codebase list golang-github-vbauerster-mpb / 2e03166
Dynamic total value Vladimir Bauer 8 years ago
2 changed file(s) with 58 addition(s) and 41 deletion(s). Raw diff Collapse all Expand all
5050 etaAlpha float64
5151 total int64
5252 current int64
53 dropRatio int64
5354 trimLeftSpace bool
5455 trimRightSpace bool
5556 completed bool
5657 aborted bool
58 dynamic bool
5759 startTime time.Time
5860 timeElapsed time.Duration
5961 blockStartTime time.Time
6062 timePerItem time.Duration
6163 appendFuncs []decor.DecoratorFunc
6264 prependFuncs []decor.DecoratorFunc
63 simpleSpinner func() byte
6465 refill *refill
6566 bufP, bufB, bufA *bytes.Buffer
6667 }
6768 )
6869
6970 func newBar(ID int, total int64, wg *sync.WaitGroup, cancel <-chan struct{}, options ...BarOption) *Bar {
71 if total <= 0 {
72 total = time.Now().Unix()
73 }
74
7075 s := state{
71 id: ID,
72 total: total,
73 etaAlpha: etaAlpha,
74 }
75
76 // if total <= 0 {
77 // s.simpleSpinner = getSpinner()
78 // }
76 id: ID,
77 total: total,
78 etaAlpha: etaAlpha,
79 dropRatio: 10,
80 }
7981
8082 for _, opt := range options {
8183 opt(&s)
102104 s.prependFuncs = nil
103105 }:
104106 case <-b.quit:
105 return
106107 }
107108 }
108109
113114 s.appendFuncs = nil
114115 }:
115116 case <-b.quit:
116 return
117117 }
118118 }
119119
142142 s.timeElapsed = time.Since(s.startTime)
143143 s.updateTimePerItemEstimate(n)
144144 if s.total > 0 && sum >= s.total {
145 s.current = s.total
146 s.completed = true
145 if s.dynamic {
146 sum -= sum * s.dropRatio / 100
147 } else {
148 s.current = s.total
149 s.completed = true
150 }
147151 return
148152 }
149153 s.current = sum
150154 s.blockStartTime = time.Now()
151155 }:
152156 case <-b.quit:
153 return
154157 }
155158 }
156159
165168 s.refill = &refill{r, till}
166169 }:
167170 case <-b.quit:
168 return
169171 }
170172 }
171173
217219 return <-result
218220 case <-b.done:
219221 return b.cacheState.total
222 }
223 }
224
225 // SetTotal sets total dynamically. The final param indicates the very last set,
226 // in other words you should set it to true when total is determined.
227 // Also you may consider providing your drop ratio via BarDropRatio BarOption func.
228 func (b *Bar) SetTotal(total int64, final bool) {
229 select {
230 case b.ops <- func(s *state) {
231 s.total = total
232 s.dynamic = !final
233 }:
234 case <-b.quit:
220235 }
221236 }
222237
310325 return ch
311326 }
312327
313 func (s *state) updateFormat(format string) {
314 for i, n := 0, 0; len(format) > 0; i++ {
315 s.format[i], n = utf8.DecodeRuneInString(format)
316 format = format[n:]
317 }
318 }
319
320328 func (s *state) updateTimePerItemEstimate(amount int) {
321329 lastBlockTime := time.Since(s.blockStartTime) // shorthand for time.Now().Sub(t)
322330 lastItemEstimate := float64(lastBlockTime) / float64(amount)
401409 }
402410
403411 s.bufB.WriteRune(s.format[rRight])
404 }
405
406 func concatenateBlocks(buf []byte, blocks ...[]byte) []byte {
407 for _, block := range blocks {
408 buf = append(buf, block...)
409 }
410 return buf
411412 }
412413
413414 func newStatistics(s *state) *decor.Statistics {
423424 }
424425 }
425426
426 func getSpinner() func() byte {
427 chars := []byte(`-\|/`)
428 repeat := len(chars) - 1
429 index := repeat
430 return func() byte {
431 if index == repeat {
432 index = -1
433 }
434 index++
435 return chars[index]
436 }
437 }
427 func concatenateBlocks(buf []byte, blocks ...[]byte) []byte {
428 for _, block := range blocks {
429 buf = append(buf, block...)
430 }
431 return buf
432 }
433
434 func (s *state) updateFormat(format string) {
435 for i, n := 0, 0; len(format) > 0; i++ {
436 s.format[i], n = utf8.DecodeRuneInString(format)
437 format = format[n:]
438 }
439 }
55 // if passed to p.AddBar(int64, ...BarOption)
66 type BarOption func(*state)
77
8 // AppendDecorators let you inject decorators to the bar's right side
89 func AppendDecorators(appenders ...decor.DecoratorFunc) BarOption {
910 return func(bs *state) {
1011 bs.appendFuncs = append(bs.appendFuncs, appenders...)
1112 }
1213 }
1314
15 // PrependDecorators let you inject decorators to the bar's left side
1416 func PrependDecorators(prependers ...decor.DecoratorFunc) BarOption {
1517 return func(bs *state) {
1618 bs.prependFuncs = append(bs.prependFuncs, prependers...)
1719 }
1820 }
1921
22 // BarTrimLeft trims left side space of the bar
2023 func BarTrimLeft() BarOption {
2124 return func(bs *state) {
2225 bs.trimLeftSpace = true
2326 }
2427 }
2528
29 // BarTrimRight trims right space of the bar
2630 func BarTrimRight() BarOption {
2731 return func(bs *state) {
2832 bs.trimRightSpace = true
2933 }
3034 }
3135
36 // BarTirm trims both left and right spaces of the bar
3237 func BarTrim() BarOption {
3338 return func(bs *state) {
3439 bs.trimLeftSpace = true
3641 }
3742 }
3843
44 // BarID overwrites internal bar id
3945 func BarID(id int) BarOption {
4046 return func(bs *state) {
4147 bs.id = id
5157 }
5258 }
5359
60 // BarDropRatio sets drop ratio, default is 10. Effective when total is dynamic.
61 // If progress tip reaches total, but total is not final value yet, tip will be
62 // dropped by specified ratio.
63 func BarDropRatio(ratio int64) BarOption {
64 return func(bs *state) {
65 bs.dropRatio = ratio
66 }
67 }
68
5469 func barWidth(w int) BarOption {
5570 return func(bs *state) {
5671 bs.width = w