From 8df7d962e60bce44adda674ef8f99a2a76a4a6e7 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sun, 14 Aug 2016 17:44:11 +0900 Subject: [PATCH] Improve rendering time of long lines --- src/terminal.go | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/terminal.go b/src/terminal.go index ff1120a..b6b6156 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -656,6 +656,17 @@ func trimLeft(runes []rune, width int) ([]rune, int32) { return runes, trimmed } +func overflow(runes []rune, max int) bool { + l := 0 + for _, r := range runes { + l += runeWidth(r, l) + if l > max { + return true + } + } + return false +} + func (t *Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, current bool) { var maxe int for _, offset := range item.offsets { @@ -668,17 +679,15 @@ func (t *Terminal) printHighlighted(item *Item, bold bool, col1 int, col2 int, c offsets := item.colorOffsets(col2, bold, current) maxWidth := t.window.Width - 3 maxe = util.Constrain(maxe+util.Min(maxWidth/2-2, t.hscrollOff), 0, len(text)) - fullWidth := displayWidth(text) - if fullWidth > maxWidth { + if overflow(text, maxWidth) { if t.hscroll { // Stri.. - matchEndWidth := displayWidth(text[:maxe]) - if matchEndWidth <= maxWidth-2 { + if !overflow(text[:maxe], maxWidth-2) { text, _ = trimRight(text, maxWidth-2) text = append(text, []rune("..")...) } else { // Stri.. - if matchEndWidth < fullWidth-2 { + if overflow(text[maxe:], 2) { text = append(text[:maxe], []rune("..")...) } // ..ri..