Codebase list golang-github-vbauerster-mpb / 46e92ff
Merge pull request #54 from vbauerster/stripansi Correct width calculation for ansi colored string. Fixes #53 Vladimir Bauer authored 6 years ago GitHub committed 6 years ago
4 changed file(s) with 18 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
4343 i := i
4444 go func() {
4545 task := fmt.Sprintf("Task#%02d:", i)
46 job := "installing"
46 job := "\x1b[31;1;4minstalling\x1b[0m"
4747 // preparing delayed bars
4848 b := p.AddBar(rand.Int63n(101)+100,
4949 mpb.BarParkTo(bars[i]),
5151 mpb.PrependDecorators(
5252 decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
5353 decor.OnComplete(decor.Name(job, decor.WCSyncSpaceR), "done!"),
54 decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), "")),
54 decor.OnComplete(decor.EwmaETA(decor.ET_STYLE_MMSS, 0, decor.WCSyncWidth), ""),
55 ),
5556 mpb.AppendDecorators(
5657 decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
5758 ),
33 "fmt"
44 "time"
55 "unicode/utf8"
6
7 "github.com/acarl005/stripansi"
68 )
79
810 const (
116118 // W represents width and C represents bit set of width related config.
117119 // A decorator should embed WC, to enable width synchronization.
118120 type WC struct {
119 W int
120 C int
121 dynFormat string
122 staticFormat string
123 wsync chan int
121 W int
122 C int
123 dynFormat string
124 wsync chan int
124125 }
125126
126127 // FormatMsg formats final message according to WC.W and WC.C.
127128 // Should be called by any Decorator implementation.
128129 func (wc *WC) FormatMsg(msg string) string {
130 max := utf8.RuneCountInString(stripansi.Strip(msg))
129131 if (wc.C & DSyncWidth) != 0 {
130 wc.wsync <- utf8.RuneCountInString(msg)
131 max := <-wc.wsync
132 wc.wsync <- max
133 max = (utf8.RuneCountInString(msg) - max) + <-wc.wsync
132134 if (wc.C & DextraSpace) != 0 {
133135 max++
134136 }
135 return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg)
137 } else {
138 max = (utf8.RuneCountInString(msg) - max) + wc.W
136139 }
137 return fmt.Sprintf(wc.staticFormat, msg)
140 return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg)
138141 }
139142
140143 // Init initializes width related config.
144147 wc.dynFormat += "-"
145148 }
146149 wc.dynFormat += "%ds"
147 wc.staticFormat = fmt.Sprintf(wc.dynFormat, wc.W)
148150 if (wc.C & DSyncWidth) != 0 {
149151 // it's deliberate choice to override wsync on each Init() call,
150152 // this way globals like WCSyncSpace can be reused
11
22 require (
33 github.com/VividCortex/ewma v1.1.1
4 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
45 golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
56 golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
67 )
00 github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
11 github.com/VividCortex/ewma v1.1.1/go.mod h1:2Tkkvm3sRDVXaiyucHiACn4cqf7DpdyLvmxzcbUokwA=
2 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
3 github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
24 golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
35 golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4=
46 golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=