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
| 43 | 43 |
i := i
|
| 44 | 44 |
go func() {
|
| 45 | 45 |
task := fmt.Sprintf("Task#%02d:", i)
|
| 46 | |
job := "installing"
|
|
46 |
job := "\x1b[31;1;4minstalling\x1b[0m"
|
| 47 | 47 |
// preparing delayed bars
|
| 48 | 48 |
b := p.AddBar(rand.Int63n(101)+100,
|
| 49 | 49 |
mpb.BarParkTo(bars[i]),
|
|
| 51 | 51 |
mpb.PrependDecorators(
|
| 52 | 52 |
decor.Name(task, decor.WC{W: len(task) + 1, C: decor.DidentRight}),
|
| 53 | 53 |
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 |
),
|
| 55 | 56 |
mpb.AppendDecorators(
|
| 56 | 57 |
decor.OnComplete(decor.Percentage(decor.WC{W: 5}), ""),
|
| 57 | 58 |
),
|
| 3 | 3 |
"fmt"
|
| 4 | 4 |
"time"
|
| 5 | 5 |
"unicode/utf8"
|
|
6 |
|
|
7 |
"github.com/acarl005/stripansi"
|
| 6 | 8 |
)
|
| 7 | 9 |
|
| 8 | 10 |
const (
|
|
| 116 | 118 |
// W represents width and C represents bit set of width related config.
|
| 117 | 119 |
// A decorator should embed WC, to enable width synchronization.
|
| 118 | 120 |
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
|
| 124 | 125 |
}
|
| 125 | 126 |
|
| 126 | 127 |
// FormatMsg formats final message according to WC.W and WC.C.
|
| 127 | 128 |
// Should be called by any Decorator implementation.
|
| 128 | 129 |
func (wc *WC) FormatMsg(msg string) string {
|
|
130 |
max := utf8.RuneCountInString(stripansi.Strip(msg))
|
| 129 | 131 |
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
|
| 132 | 134 |
if (wc.C & DextraSpace) != 0 {
|
| 133 | 135 |
max++
|
| 134 | 136 |
}
|
| 135 | |
return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg)
|
|
137 |
} else {
|
|
138 |
max = (utf8.RuneCountInString(msg) - max) + wc.W
|
| 136 | 139 |
}
|
| 137 | |
return fmt.Sprintf(wc.staticFormat, msg)
|
|
140 |
return fmt.Sprintf(fmt.Sprintf(wc.dynFormat, max), msg)
|
| 138 | 141 |
}
|
| 139 | 142 |
|
| 140 | 143 |
// Init initializes width related config.
|
|
| 144 | 147 |
wc.dynFormat += "-"
|
| 145 | 148 |
}
|
| 146 | 149 |
wc.dynFormat += "%ds"
|
| 147 | |
wc.staticFormat = fmt.Sprintf(wc.dynFormat, wc.W)
|
| 148 | 150 |
if (wc.C & DSyncWidth) != 0 {
|
| 149 | 151 |
// it's deliberate choice to override wsync on each Init() call,
|
| 150 | 152 |
// this way globals like WCSyncSpace can be reused
|
| 1 | 1 |
|
| 2 | 2 |
require (
|
| 3 | 3 |
github.com/VividCortex/ewma v1.1.1
|
|
4 |
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
|
| 4 | 5 |
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708
|
| 5 | 6 |
golang.org/x/sys v0.0.0-20191113165036-4c7a9d0fe056 // indirect
|
| 6 | 7 |
)
|
| 0 | 0 |
github.com/VividCortex/ewma v1.1.1 h1:MnEK4VOv6n0RSY4vtRe3h11qjxL3+t0B8yOL8iMXdcM=
|
| 1 | 1 |
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=
|
| 2 | 4 |
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
| 3 | 5 |
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708 h1:pXVtWnwHkrWD9ru3sDxY/qFK/bfc0egRovX91EjWjf4=
|
| 4 | 6 |
golang.org/x/crypto v0.0.0-20191112222119-e1110fd1c708/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|