refactoring "complex" example
showcase Meta and OnCompleteMeta
Vladimir Bauer
2 years ago
| 1 | 1 |
|
| 2 | 2 |
go 1.17
|
| 3 | 3 |
|
| 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 |
)
|
| 5 | 8 |
|
| 6 | 9 |
require (
|
| 7 | 10 |
github.com/VividCortex/ewma v1.2.0 // indirect
|
| 8 | 11 |
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
|
| 9 | 14 |
github.com/mattn/go-runewidth v0.0.14 // indirect
|
| 10 | 15 |
github.com/rivo/uniseg v0.4.4 // indirect
|
| 11 | 16 |
golang.org/x/sys v0.7.0 // indirect
|
| 4 | 4 |
"math/rand"
|
| 5 | 5 |
"time"
|
| 6 | 6 |
|
|
7 |
"github.com/fatih/color"
|
| 7 | 8 |
"github.com/vbauerster/mpb/v8"
|
| 8 | 9 |
"github.com/vbauerster/mpb/v8/decor"
|
| 9 | 10 |
)
|
| 10 | 11 |
|
| 11 | 12 |
func main() {
|
| 12 | 13 |
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)
|
| 14 | 21 |
|
| 15 | 22 |
for i := 0; i < numBars; i++ {
|
| 16 | 23 |
task := fmt.Sprintf("Task#%02d:", i)
|
|
| 30 | 37 |
mpb.BarFillerClearOnComplete(),
|
| 31 | 38 |
mpb.PrependDecorators(
|
| 32 | 39 |
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 |
),
|
| 34 | 47 |
decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
|
| 35 | 48 |
),
|
| 36 | 49 |
mpb.AppendDecorators(
|
|
| 59 | 72 |
bar.EwmaIncrInt64(rand.Int63n(5)+1, time.Since(start))
|
| 60 | 73 |
}
|
| 61 | 74 |
}
|
|
75 |
|
|
76 |
func toMetaFunc(c *color.Color) func(string) string {
|
|
77 |
return func(s string) string {
|
|
78 |
return c.Sprint(s)
|
|
79 |
}
|
|
80 |
}
|