use rivo/uniseg to replace utf8.RuneCountInString
Vladimir Bauer
5 years ago
| 5 | 5 | "unicode/utf8" |
| 6 | 6 | |
| 7 | 7 | "github.com/mattn/go-runewidth" |
| 8 | "github.com/rivo/uniseg" | |
| 8 | 9 | "github.com/vbauerster/mpb/v6/decor" |
| 9 | 10 | "github.com/vbauerster/mpb/v6/internal" |
| 10 | 11 | ) |
| 92 | 93 | if !utf8.ValidString(style) { |
| 93 | 94 | panic("invalid bar style") |
| 94 | 95 | } |
| 95 | rcount := utf8.RuneCountInString(style) | |
| 96 | srcFormat := make([][]byte, rcount) | |
| 97 | srcRwidth := make([]int, rcount) | |
| 98 | i := 0 | |
| 99 | for _, r := range style { | |
| 100 | srcRwidth[i] = runewidth.RuneWidth(r) | |
| 101 | srcFormat[i] = []byte(string(r)) | |
| 102 | i++ | |
| 96 | srcFormat := make([][]byte, 0, len(BarDefaultStyle)) | |
| 97 | srcRwidth := make([]int, 0, len(BarDefaultStyle)) | |
| 98 | gr := uniseg.NewGraphemes(style) | |
| 99 | for gr.Next() { | |
| 100 | srcFormat = append(srcFormat, gr.Bytes()) | |
| 101 | srcRwidth = append(srcRwidth, runewidth.StringWidth(gr.Str())) | |
| 103 | 102 | } |
| 104 | 103 | copy(s.format, srcFormat) |
| 105 | 104 | copy(s.rwidth, srcRwidth) |