truncate if space rune is double width
Vladimir Bauer
6 years ago
| 48 | 48 | } |
| 49 | 49 | |
| 50 | 50 | type space struct { |
| 51 | space []byte | |
| 52 | width int | |
| 53 | count int | |
| 51 | space []byte | |
| 52 | rwidth int | |
| 53 | count int | |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | // NewBarFiller constucts mpb.BarFiller, to be used with *Progress.Add(...) *Bar method. |
| 112 | 112 | cwidth := int(internal.PercentageRound(stat.Total, stat.Current, width)) |
| 113 | 113 | bb := make([][]byte, cwidth) |
| 114 | 114 | space := &space{ |
| 115 | space: s.format[rSpace], | |
| 116 | width: s.rwidth[rSpace], | |
| 117 | count: width - cwidth, | |
| 115 | space: s.format[rSpace], | |
| 116 | rwidth: s.rwidth[rSpace], | |
| 117 | count: width - cwidth, | |
| 118 | 118 | } |
| 119 | 119 | if cwidth == 0 { |
| 120 | 120 | s.flush(w, space, bb) |
| 152 | 152 | index++ |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | if index != len(bb) { | |
| 155 | if index != len(bb) || space.rwidth > 1 { | |
| 156 | 156 | buf := new(bytes.Buffer) |
| 157 | 157 | s.flush(buf, space, bb[:index]) |
| 158 | 158 | io.WriteString(w, runewidth.Truncate(buf.String(), width, "…")) |
| 168 | 168 | } |
| 169 | 169 | for space.count > 0 { |
| 170 | 170 | w.Write(space.space) |
| 171 | space.count -= space.width | |
| 171 | space.count -= space.rwidth | |
| 172 | 172 | } |
| 173 | 173 | } |
| 174 | 174 | |
| 175 | 175 | func reverseFlush(w io.Writer, space *space, bb [][]byte) { |
| 176 | 176 | for space.count > 0 { |
| 177 | 177 | w.Write(space.space) |
| 178 | space.count -= space.width | |
| 178 | space.count -= space.rwidth | |
| 179 | 179 | } |
| 180 | 180 | for i := 0; i < len(bb); i++ { |
| 181 | 181 | w.Write(bb[i]) |