simple example update
Vladimir Bauer
9 years ago
| 2 | 2 | import ( |
| 3 | 3 | "fmt" |
| 4 | 4 | "math/rand" |
| 5 | "runtime" | |
| 6 | 5 | "time" |
| 7 | 6 | |
| 8 | "github.com/vbauerster/uiprogress" | |
| 7 | "github.com/vbauerster/mpb" | |
| 9 | 8 | ) |
| 10 | 9 | |
| 11 | 10 | const ( |
| 12 | 11 | totalItem = 100 |
| 13 | maxBlockSize = 12 | |
| 12 | maxBlockSize = 10 | |
| 14 | 13 | ) |
| 15 | 14 | |
| 16 | 15 | func main() { |
| 17 | runtime.GOMAXPROCS(runtime.NumCPU()) | |
| 18 | decor := func(s *uiprogress.Statistics) string { | |
| 19 | str := fmt.Sprintf("%d/%d", s.Completed, s.Total) | |
| 16 | decor := func(s *mpb.Statistics) string { | |
| 17 | str := fmt.Sprintf("%d/%d", s.Current, s.Total) | |
| 20 | 18 | return fmt.Sprintf("%-7s", str) |
| 21 | 19 | } |
| 22 | 20 | |
| 23 | p := uiprogress.New() | |
| 21 | p := mpb.New() | |
| 24 | 22 | bar := p.AddBar(totalItem).AppendETA().PrependFunc(decor) |
| 23 | // if you omit the following line, bar rendering goroutine may not have a | |
| 24 | // chance to coplete, thus better always use. | |
| 25 | p.Wg.Add(1) | |
| 25 | 26 | |
| 26 | 27 | blockSize := rand.Intn(maxBlockSize) + 1 |
| 27 | for i := 0; i < 100; i += 1 { | |
| 28 | for i := 0; i < 100; i++ { | |
| 28 | 29 | time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond))))) |
| 29 | 30 | bar.Incr(1) |
| 30 | 31 | blockSize = rand.Intn(maxBlockSize) + 1 |