630678cf5c2181dffe3c4d41fa39421f181d7dc2 — Chris Waldon 11 months ago 8432ec5
styledtext: fix possible infinite loop

This commit fixes a condition in which the styledtext display could loop infinitely
if none of the text in a run could fit on a line. The text iterator would detect
zero runes of text prior to the truncator symbol and would fail to make progress,
performing the same shaping with the same runes on the next iteration. This change
ensures that if the truncator is encountered after zero runes, it is treated as
consuming a rune which must be a newline.

Signed-off-by: Chris Waldon <christopher.waldon.dev@gmail.com>
1 files changed, 5 insertions(+), 0 deletions(-)

M styledtext/iterator.go
M styledtext/iterator.go => styledtext/iterator.go +5 -0
@@ 54,6 54,11 @@ type textIterator struct {
// viewport and (if so) updates the iterator's text dimensions to include the glyph.
func (it *textIterator) processGlyph(g text.Glyph, ok bool) (_ text.Glyph, visibleOrBefore bool) {
	if g.Flags&text.FlagTruncator != 0 {
		// If the truncator is the first glyph, force a newline.
		if it.runes == 0 {
			it.runes = 1
			it.hasNewline = true
		return g, false
	it.runes += int(g.Runes)