1package text
2
3import "github.com/mattn/go-runewidth"
4
5// TruncateMax truncate a line if its length is greater
6// than the given length. Otherwise, the line is returned
7// as is. If truncating occur, an ellipsis is inserted at
8// the end.
9// Handle properly terminal color escape code
10func TruncateMax(line string, length int) string {
11 if length <= 0 {
12 return "…"
13 }
14
15 l := Len(line)
16 if l <= length || l == 0 {
17 return line
18 }
19
20 cleaned, escapes := ExtractTermEscapes(line)
21 truncated := runewidth.Truncate(cleaned, length-1, "")
22
23 return ApplyTermEscapes(truncated, escapes) + "…"
24}