StringWidth returns the visual width of a byte array indexed from 0 to n (rune index) with a given tabsize
(b []byte, n, tabsize int)
| 213 | // StringWidth returns the visual width of a byte array indexed from 0 to n (rune index) |
| 214 | // with a given tabsize |
| 215 | func StringWidth(b []byte, n, tabsize int) int { |
| 216 | if n <= 0 { |
| 217 | return 0 |
| 218 | } |
| 219 | i := 0 |
| 220 | width := 0 |
| 221 | for len(b) > 0 { |
| 222 | r, _, size := DecodeCharacter(b) |
| 223 | b = b[size:] |
| 224 | |
| 225 | switch r { |
| 226 | case '\t': |
| 227 | ts := tabsize - (width % tabsize) |
| 228 | width += ts |
| 229 | default: |
| 230 | width += runewidth.RuneWidth(r) |
| 231 | } |
| 232 | |
| 233 | i++ |
| 234 | |
| 235 | if i == n { |
| 236 | return width |
| 237 | } |
| 238 | } |
| 239 | return width |
| 240 | } |
| 241 | |
| 242 | // Min takes the min of two ints |
| 243 | func Min(a, b int) int { |