diff options
author | Michael Muré <batolettre@gmail.com> | 2019-11-03 14:00:35 +0100 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2019-11-03 14:00:35 +0100 |
commit | f72a9dc62ba20546b2cdeb466434fc1900741a4f (patch) | |
tree | 8b68dc12c312d0a1fe6d5b1a1388cee82d44c634 /vendor/github.com/MichaelMure/go-term-text/truncate.go | |
parent | 809abf9244f64683fe2d9f8489a4dcff0904d5b5 (diff) | |
download | git-bug-f72a9dc62ba20546b2cdeb466434fc1900741a4f.tar.gz |
switch to go-term-text to fix bad underflow for label rendering
Diffstat (limited to 'vendor/github.com/MichaelMure/go-term-text/truncate.go')
-rw-r--r-- | vendor/github.com/MichaelMure/go-term-text/truncate.go | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/vendor/github.com/MichaelMure/go-term-text/truncate.go b/vendor/github.com/MichaelMure/go-term-text/truncate.go new file mode 100644 index 00000000..b51bb39e --- /dev/null +++ b/vendor/github.com/MichaelMure/go-term-text/truncate.go @@ -0,0 +1,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) + "…" +} |