dynamic total example upd
Vladimir Bauer
7 years ago
| 15 | 15 | func main() { |
| 16 | 16 | p := mpb.New(mpb.WithWidth(64)) |
| 17 | 17 | |
| 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, | |
| 19 | 20 | mpb.PrependDecorators(decor.Counters(decor.UnitKiB, "% .1f / % .1f")), |
| 20 | 21 | mpb.AppendDecorators(decor.Percentage()), |
| 21 | 22 | ) |
| 22 | 23 | |
| 23 | var written int64 | |
| 24 | 24 | maxSleep := 100 * time.Millisecond |
| 25 | 25 | read := makeStream(200) |
| 26 | 26 | for { |
| 27 | 27 | n, err := read() |
| 28 | written += int64(n) | |
| 28 | total += int64(n) | |
| 29 | 29 | time.Sleep(time.Duration(rand.Intn(10)+1) * maxSleep / 10) |
| 30 | 30 | bar.IncrBy(n) |
| 31 | 31 | if err == io.EOF { |
| 32 | // total is known, final=true | |
| 33 | bar.SetTotal(total, true) | |
| 32 | 34 | break |
| 33 | 35 | } |
| 34 | bar.SetTotal(written+1024, false) | |
| 36 | // total is unknown, final=false | |
| 37 | bar.SetTotal(total+2048, false) | |
| 35 | 38 | } |
| 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) | |
| 41 | 39 | |
| 42 | 40 | p.Wait() |
| 43 | 41 | } |