Codebase list golang-github-vbauerster-mpb / 8fe6a3a
refactor dynTotal example Vladimir Bauer 4 years ago
1 changed file(s) with 6 addition(s) and 10 deletion(s). Raw diff Collapse all Expand all
1515 func main() {
1616 p := mpb.New(mpb.WithWidth(64))
1717
18 var total int64
1918 // new bar with 'trigger complete event' disabled, because total is zero
20 bar := p.AddBar(total,
19 bar := p.AddBar(0,
2120 mpb.PrependDecorators(decor.Counters(decor.UnitKiB, "% .1f / % .1f")),
2221 mpb.AppendDecorators(decor.Percentage()),
2322 )
2625 read := makeStream(200)
2726 for {
2827 n, err := read()
29 total += int64(n)
3028 if err == io.EOF {
29 // triggering complete event now
30 bar.SetTotal(-1, true)
3131 break
3232 }
33 // while total is unknown,
34 // set it to a positive number which is greater than current total,
35 // to make sure no complete event is triggered by next IncrBy call.
36 bar.SetTotal(total+2048, false)
33 // increment methods won't trigger complete event because bar was constructed with total = 0
3734 bar.IncrBy(n)
35 // following call is not required, it's called to show some progress instead of an empty bar
36 bar.SetTotal(bar.Current()+2048, false)
3837 time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10)
3938 }
40
41 // force bar complete event, note true flag
42 bar.SetTotal(total, true)
4339
4440 p.Wait()
4541 }