runecount.go 364 B

1234567891011121314151617
  1. package pb
  2. import (
  3. "github.com/mattn/go-runewidth"
  4. "regexp"
  5. )
  6. // Finds the control character sequences (like colors)
  7. var ctrlFinder = regexp.MustCompile("\x1b\x5b[0-9]+\x6d")
  8. func escapeAwareRuneCountInString(s string) int {
  9. n := runewidth.StringWidth(s)
  10. for _, sm := range ctrlFinder.FindAllString(s, -1) {
  11. n -= runewidth.StringWidth(sm)
  12. }
  13. return n
  14. }