diff --git a/examples/dynTotal/main.go b/examples/dynTotal/main.go index 91a05e9..18313c9 100644 --- a/examples/dynTotal/main.go +++ b/examples/dynTotal/main.go @@ -16,29 +16,27 @@ func main() { p := mpb.New(mpb.WithWidth(64)) - bar := p.AddBar(0, // by setting total to 0, we indicate that it's dynamic + var total int64 + bar := p.AddBar(total, mpb.PrependDecorators(decor.Counters(decor.UnitKiB, "% .1f / % .1f")), mpb.AppendDecorators(decor.Percentage()), ) - var written int64 maxSleep := 100 * time.Millisecond read := makeStream(200) for { n, err := read() - written += int64(n) + total += int64(n) time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10) bar.IncrBy(n) if err == io.EOF { + // total is known, final=true + bar.SetTotal(total, true) break } - bar.SetTotal(written+1024, false) + // total is unknown, final=false + bar.SetTotal(total+2048, false) } - - // final set total, final=true - bar.SetTotal(written, true) - // need to increment once, to shutdown the bar - bar.IncrBy(0) p.Wait() }