Codebase list golang-github-vbauerster-mpb / 1ef087d examples / simple / main.go
1ef087d

Tree @1ef087d (Download .tar.gz)

main.go @1ef087draw · history · blame

package main

import (
	"fmt"
	"math/rand"
	"sync"
	"time"

	"github.com/vbauerster/mpb"
	"github.com/vbauerster/mpb/decor"
)

func main() {
	p := mpb.New()
	total := 100
	numBars := 3
	var wg sync.WaitGroup
	wg.Add(numBars)

	for i := 0; i < numBars; i++ {
		name := fmt.Sprintf("Bar#%d:", i)
		bar := p.AddBar(int64(total),
			mpb.PrependDecorators(
				// Name decorator with minWidth and no width sync
				decor.Name(name, len(name), 0),
				// Percentage decorator with DwidthSync and DextraSpace
				decor.Percentage(3, decor.DSyncSpace),
			),
			mpb.AppendDecorators(
				// ETA decorator, with no width sync
				decor.ETA(2, 0),
			),
		)

		go func() {
			defer wg.Done()
			for i := 0; i < total; i++ {
				time.Sleep(time.Duration(rand.Intn(100)) * time.Millisecond)
				bar.Incr(1)
			}
		}()
	}
	wg.Wait() // Wait for goroutines to finish
	p.Stop()  // Stop mpb's rendering goroutine
}