Codebase list golang-github-vbauerster-mpb / d2efdb2
refactoring "complex" example showcase Meta and OnCompleteMeta Vladimir Bauer 2 years ago
2 changed file(s) with 27 addition(s) and 3 deletion(s). Raw diff Collapse all Expand all
11
22 go 1.17
33
4 require github.com/vbauerster/mpb/v8 v8.4.0
4 require (
5 github.com/fatih/color v1.15.0
6 github.com/vbauerster/mpb/v8 v8.4.0
7 )
58
69 require (
710 github.com/VividCortex/ewma v1.2.0 // indirect
811 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect
12 github.com/mattn/go-colorable v0.1.13 // indirect
13 github.com/mattn/go-isatty v0.0.17 // indirect
914 github.com/mattn/go-runewidth v0.0.14 // indirect
1015 github.com/rivo/uniseg v0.4.4 // indirect
1116 golang.org/x/sys v0.7.0 // indirect
44 "math/rand"
55 "time"
66
7 "github.com/fatih/color"
78 "github.com/vbauerster/mpb/v8"
89 "github.com/vbauerster/mpb/v8/decor"
910 )
1011
1112 func main() {
1213 numBars := 4
13 p := mpb.New()
14 // to support color in Windows following both options are required
15 p := mpb.New(
16 mpb.WithOutput(color.Output),
17 mpb.WithAutoRefresh(),
18 )
19
20 red, green := color.New(color.FgRed), color.New(color.FgGreen)
1421
1522 for i := 0; i < numBars; i++ {
1623 task := fmt.Sprintf("Task#%02d:", i)
3037 mpb.BarFillerClearOnComplete(),
3138 mpb.PrependDecorators(
3239 decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
33 decor.OnComplete(decor.Name("\x1b[31minstalling\x1b[0m", decor.WCSyncSpaceR), "done!"),
40 decor.OnCompleteMeta(
41 decor.OnComplete(
42 decor.Meta(decor.Name("installing", decor.WCSyncSpaceR), toMetaFunc(red)),
43 "done!",
44 ),
45 toMetaFunc(green),
46 ),
3447 decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
3548 ),
3649 mpb.AppendDecorators(
5972 bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
6073 }
6174 }
75
76 func toMetaFunc(c *color.Color) func(string) string {
77 return func(s string) string {
78 return c.Sprint(s)
79 }
80 }