Codebase list golang-github-vbauerster-mpb / 960fe3f example / remove / main.go
960fe3f

Tree @960fe3f (Download .tar.gz)

main.go @960fe3fraw · history · blame

package main

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

	"github.com/vbauerster/mpb"
)

const (
	maxBlockSize = 12
)

func main() {

	var wg sync.WaitGroup
	p := mpb.New(nil).SetWidth(64)
	// p := mpb.New(nil).RefreshRate(100 * time.Millisecond).SetWidth(64)

	name1 := "Bar#1: "
	bar1 := p.AddBar(50).
		PrependName(name1, len(name1)).PrependETA(4).
		AppendPercentage().TrimRightSpace()
	wg.Add(1)
	go func() {
		defer wg.Done()
		blockSize := rand.Intn(maxBlockSize) + 1
		for i := 0; i < 50; i++ {
			sleep(blockSize)
			bar1.Incr(1)
			blockSize = rand.Intn(maxBlockSize) + 1
		}
	}()

	bar2 := p.AddBar(100).
		PrependName("", 0-len(name1)).PrependETA(4).
		AppendPercentage().TrimRightSpace()
	wg.Add(1)
	go func() {
		defer wg.Done()
		blockSize := rand.Intn(maxBlockSize) + 1
		for i := 0; i < 100; i++ {
			sleep(blockSize)
			bar2.Incr(1)
			stat := bar2.GetStatistics()
			if stat.Current > 42 && p.RemoveBar(bar2) {
				break
			}
			blockSize = rand.Intn(maxBlockSize) + 1
		}
	}()

	bar3 := p.AddBar(80).
		PrependName("Bar#3: ", 0).PrependETA(4).
		AppendPercentage().TrimRightSpace()
	wg.Add(1)
	go func() {
		defer wg.Done()
		blockSize := rand.Intn(maxBlockSize) + 1
		for i := 0; i < 80; i++ {
			sleep(blockSize)
			bar3.Incr(1)
			blockSize = rand.Intn(maxBlockSize) + 1
		}
	}()

	wg.Wait()
	p.Stop()
	// p.AddBar(2) // panic: you cannot reuse p, create new one!
	fmt.Println("stop")
}

func sleep(blockSize int) {
	time.Sleep(time.Duration(blockSize) * (50*time.Millisecond + time.Duration(rand.Intn(5*int(time.Millisecond)))))
}