1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- package main
- import (
- "fmt"
- "math/rand"
- "time"
- "github.com/fatih/color"
- "github.com/vbauerster/mpb/v8"
- "github.com/vbauerster/mpb/v8/decor"
- )
- func process() {
-
- p := mpb.New(
- mpb.WithOutput(color.Output),
- mpb.WithAutoRefresh(),
- )
- red, green := color.New(color.FgRed), color.New(color.FgGreen)
- fmt.Println("应用更新中...")
- task := "windows.zip"
- 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("下载中:", 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]),
- mpb.BarFillerClearOnComplete(),
- mpb.PrependDecorators(
- decor.Name(task, decor.WC{C: decor.DindentRight | decor.DextraSpace}),
- decor.OnCompleteMeta(
- decor.OnComplete(
- decor.Meta(decor.Name("安装中:", decor.WCSyncSpaceR), toMetaFunc(red)),
- "更新完成!",
- ),
- 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 := time.Now()
- time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
-
- 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)
- }
- }
|