1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
package main
import (
"fmt"
"math/rand"
"time"
"github.com/fatih/color"
"github.com/vbauerster/mpb/v8"
"github.com/vbauerster/mpb/v8/decor"
)
func main() {
numBars := 4
// to support color in Windows following both options are required
p := mpb.New(
mpb.WithOutput(color.Output),
mpb.WithAutoRefresh(),
)
red, green := color.New(color.FgRed), color.New(color.FgGreen)
for i := 0; i < numBars; i++ {
task := fmt.Sprintf("Task#%02d:", i)
queue := make([]*mpb.Bar, 2)
queue[0] = p.AddBar(rand.Int63n(201)+100,
mpb.PrependDecorators(
decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.Name("downloading", decor.WCSyncSpaceR),
decor.CountersNoUnit("%d / %d", decor.WCSyncWidth),
),
mpb.AppendDecorators(
decor.OnComplete(decor.Percentage(decor.WC{W: 5}), "done"),
),
)
queue[1] = p.AddBar(rand.Int63n(101)+100,
mpb.BarQueueAfter(queue[0]), // this bar is queued
mpb.BarFillerClearOnComplete(),
mpb.PrependDecorators(
decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
decor.OnCompleteMeta(
decor.OnComplete(
decor.Meta(decor.Name("installing", decor.WCSyncSpaceR), toMetaFunc(red)),
"done!",
),
toMetaFunc(green),
),
decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
),
mpb.AppendDecorators(
decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
),
)
go func() {
for _, b := range queue {
complete(b)
}
}()
}
p.Wait()
}
func complete(bar *mpb.Bar) {
max := 100 * time.Millisecond
for !bar.Completed() {
// start variable is solely for EWMA calculation
// EWMA's unit of measure is an iteration's duration
start := time.Now()
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
// we need to call EwmaIncrement to fulfill ewma decorator's contract
bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
}
}
func toMetaFunc(c *color.Color) func(string) string {
return func(s string) string {
return c.Sprint(s)
}
}
|