Skip to content

Commit 7f7808a

Browse files
committedDec 6, 2024·
Make the TrimAnsiString function a bit simpler
1 parent 1882259 commit 7f7808a

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed
 

‎utils/trim-ansi-string.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ import (
55
)
66

77
func TrimAnsiString(str string, limit int) string {
8-
output := ""
9-
charsToAdd := limit
8+
if limit <= 0 {
9+
return ""
10+
}
1011

11-
if charsToAdd <= 0 {
12-
return output
12+
output, err := strconv.Unquote(`"` + loopOverChars(str, limit) + `"`)
13+
14+
if err != nil {
15+
return str
1316
}
1417

18+
return output
19+
}
20+
21+
func loopOverChars(str string, limit int) string {
22+
charsToAdd := limit
23+
output := ""
24+
1525
escapeLevel := 0
1626
isEscapeClosed := true
1727

@@ -49,11 +59,5 @@ func TrimAnsiString(str string, limit int) string {
4959
}
5060
}
5161

52-
output, err := strconv.Unquote(`"` + output + `"`)
53-
54-
if err != nil {
55-
return str
56-
}
57-
5862
return output
5963
}

0 commit comments

Comments
 (0)
Please sign in to comment.