Codebase list golang-github-vbauerster-mpb / ba6a2b0
dynamic total example upd Vladimir Bauer 7 years ago
1 changed file(s) with 7 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
1515 func main() {
1616 p := mpb.New(mpb.WithWidth(64))
1717
18 bar := p.AddBar(0, // by setting total to 0, we indicate that it's dynamic
18 var total int64
19 bar := p.AddBar(total,
1920 mpb.PrependDecorators(decor.Counters(decor.UnitKiB, "% .1f / % .1f")),
2021 mpb.AppendDecorators(decor.Percentage()),
2122 )
2223
23 var written int64
2424 maxSleep := 100 * time.Millisecond
2525 read := makeStream(200)
2626 for {
2727 n, err := read()
28 written += int64(n)
28 total += int64(n)
2929 time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10)
3030 bar.IncrBy(n)
3131 if err == io.EOF {
32 // total is known, final=true
33 bar.SetTotal(total, true)
3234 break
3335 }
34 bar.SetTotal(written+1024, false)
36 // total is unknown, final=false
37 bar.SetTotal(total+2048, false)
3538 }
36
37 // final set total, final=true
38 bar.SetTotal(written, true)
39 // need to increment once, to shutdown the bar
40 bar.IncrBy(0)
4139
4240 p.Wait()
4341 }