runecount.go 330 B

1234567891011121314151617
  1. package pb
  2. import (
  3. "regexp"
  4. "unicode/utf8"
  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 := utf8.RuneCountInString(s)
  10. for _, sm := range ctrlFinder.FindAllString(s, -1) {
  11. n -= len(sm)
  12. }
  13. return n
  14. }