From c35e6b2b4c98649c9f0cce03aac69fddef29729c Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 17 Oct 2019 12:07:41 +0200 Subject: [PATCH] Avoiding infinite loop on narrow TextViews. Fixes #333 --- textview.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/textview.go b/textview.go index 82b878f..6430a93 100644 --- a/textview.go +++ b/textview.go @@ -11,6 +11,7 @@ import ( "github.com/gdamore/tcell" colorful "github.com/lucasb-eyer/go-colorful" runewidth "github.com/mattn/go-runewidth" + "github.com/rivo/uniseg" ) var ( @@ -599,6 +600,13 @@ func (t *TextView) reindexBuffer(width int) { if t.wrap && len(str) > 0 { for len(str) > 0 { extract := runewidth.Truncate(str, width, "") + if len(extract) == 0 { + // We'll extract at least one grapheme cluster. + gr := uniseg.NewGraphemes(str) + gr.Next() + _, to := gr.Positions() + extract = str[:to] + } if t.wordWrap && len(extract) < len(str) { // Add any spaces from the next line. if spaces := spacePattern.FindStringIndex(str[len(extract):]); spaces != nil && spaces[0] == 0 {