Browse Source

ansi: make SetLength handle non-ascii

Benton Edmondson 1 year ago
parent
commit
0afdf6847c
1 changed files with 6 additions and 4 deletions
  1. 6 4
      ansi/ansi.go

+ 6 - 4
ansi/ansi.go

@@ -312,14 +312,16 @@ func ReplaceLastLine(original, replacement string) string {
 }
 
 func SetLength(text string, length int, ellipsis string) string {
+	text = Squash(Scrub(text))
+	runes := []rune(text)
 	if length == 0 {
 		return ""
 	}
-	if len(text) > length {
-		return text[:length-1] + ellipsis
+	if len(runes) > length {
+		return string(runes[:length-1]) + ellipsis
 	}
-	if len(text) < length {
-		return text + strings.Repeat(" ", length-len(text))
+	if len(runes) < length {
+		return string(runes) + strings.Repeat(" ", length-len(runes))
 	}
 	return text
 }