diff --git a/_examples/complex/main.go b/_examples/complex/main.go index 22e7637..891bd74 100644 --- a/_examples/complex/main.go +++ b/_examples/complex/main.go @@ -44,7 +44,7 @@ i := i go func() { task := fmt.Sprintf("Task#%02d:", i) - job := "installing" + job := "\x1b[31;1;4minstalling\x1b[0m" // preparing delayed bars b := p.AddBar(rand.Int63n(101)+100, mpb.BarParkTo(bars[i]), @@ -52,7 +52,8 @@ mpb.PrependDecorators( decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}), decor.OnComplete(decor.Name(job, decor.WCSyncSpaceR), "done!"), - decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), "")), + decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""), + ), mpb.AppendDecorators( decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""), ), diff --git a/decor/decorator.go b/decor/decorator.go index 5c0d168..52588c0 100644 --- a/decor/decorator.go +++ b/decor/decorator.go @@ -4,6 +4,8 @@ "fmt" "time" "unicode/utf8" + + "github.com/acarl005/stripansi" ) const ( @@ -117,25 +119,26 @@ // W represents width and C represents bit set of width related config. // A decorator should embed WC, to enable width synchronization. type WC struct { - W int - C int - dynFormat string - staticFormat string - wsync chan int + W int + C int + dynFormat string + wsync chan int } // FormatMsg formats final message according to WC.W and WC.C. // Should be called by any Decorator implementation. func (wc *WC) FormatMsg(msg string) string { + max := utf8.RuneCountInString(stripansi.Strip(msg)) if (wc.C & DSyncWidth) != 0 { - wc.wsync <- utf8.RuneCountInString(msg) - max := <-wc.wsync + wc.wsync <- max + max = (utf8.RuneCountInString(msg) - max) + <-wc.wsync if (wc.C & DextraSpace) != 0 { max++ } - return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg) + } else { + max = (utf8.RuneCountInString(msg) - max) + wc.W } - return fmt.Sprintf(wc.staticFormat, msg) + return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg) } // Init initializes width related config. @@ -145,7 +148,6 @@ wc.dynFormat += "-" } wc.dynFormat += "%ds" - wc.staticFormat = fmt.Sprintf(wc.dynFormat, wc.W) if (wc.C & DSyncWidth) != 0 { // it's deliberate choice to override wsync on each Init() call, // this way globals like WCSyncSpace can be reused diff --git a/go.mod b/go.mod index 0c5ce51..9e7287d 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ require ( github.com/VividCortex/ewma v1.1.1 + github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect ) diff --git a/go.sum b/go.sum index 94a9f1a..5a13162 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM= github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA= +github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= +github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4= golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=