Improve trim function to handle longer strings

Fix #2258
This commit is contained in:
Junegunn Choi 2020-11-24 19:03:59 +09:00
parent 7acdaf0b43
commit 1efef88b6e
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

View File

@ -656,8 +656,6 @@ func (t *Terminal) displayWidth(runes []rune) int {
const (
minWidth = 4
minHeight = 4
maxDisplayWidthCalc = 1024
)
func calculateSize(base int, size sizeSpec, occupied int, minSize int, pad int) int {
@ -1116,13 +1114,15 @@ func (t *Terminal) displayWidthWithLimit(runes []rune, prefixWidth int, limit in
}
func (t *Terminal) trimLeft(runes []rune, width int) ([]rune, int32) {
if len(runes) > maxDisplayWidthCalc && len(runes) > width {
trimmed := len(runes) - width
return runes[trimmed:], int32(trimmed)
var trimmed int32
// Assume that each rune takes at least one column on screen
if len(runes) > width {
diff := len(runes) - width
trimmed = int32(diff)
runes = runes[diff:]
}
currentWidth := t.displayWidth(runes)
var trimmed int32
for currentWidth > width && len(runes) > 0 {
runes = runes[1:]