aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/MichaelMure/go-term-text/truncate.go
blob: b51bb39e3740ec89f5265d2a3b9743f71283d244 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package text

import "github.com/mattn/go-runewidth"

// TruncateMax truncate a line if its length is greater
// than the given length. Otherwise, the line is returned
// as is. If truncating occur, an ellipsis is inserted at
// the end.
// Handle properly terminal color escape code
func TruncateMax(line string, length int) string {
	if length <= 0 {
		return "…"
	}

	l := Len(line)
	if l <= length || l == 0 {
		return line
	}

	cleaned, escapes := ExtractTermEscapes(line)
	truncated := runewidth.Truncate(cleaned, length-1, "")

	return ApplyTermEscapes(truncated, escapes) + "…"
}