diff --git a/_examples/complex/go.mod b/_examples/complex/go.mod index c2e3a0c..6e05ec3 100644 --- a/_examples/complex/go.mod +++ b/_examples/complex/go.mod @@ -2,11 +2,16 @@ go 1.17 -require github.com/vbauerster/mpb/v8 v8.4.0 +require ( + github.com/fatih/color v1.15.0 + github.com/vbauerster/mpb/v8 v8.4.0 +) require ( github.com/VividCortex/ewma v1.2.0 // indirect github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.17 // indirect github.com/mattn/go-runewidth v0.0.14 // indirect github.com/rivo/uniseg v0.4.4 // indirect golang.org/x/sys v0.7.0 // indirect diff --git a/_examples/complex/main.go b/_examples/complex/main.go index 3de7ece..e64f18d 100644 --- a/_examples/complex/main.go +++ b/_examples/complex/main.go @@ -5,13 +5,20 @@ "math/rand" "time" + "github.com/fatih/color" "github.com/vbauerster/mpb/v8" "github.com/vbauerster/mpb/v8/decor" ) func main() { numBars := 4 - p := mpb.New() + // 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) @@ -31,7 +38,13 @@ mpb.BarFillerClearOnComplete(), mpb.PrependDecorators( decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}), - decor.OnComplete(decor.Name("\x1b[31minstalling\x1b[0m", decor.WCSyncSpaceR), "done!"), + 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( @@ -60,3 +73,9 @@ 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) + } +}