From d179351a0a3b6a70d967846935f1554dc7769295 Mon Sep 17 00:00:00 2001 From: Oliver <480930+rivo@users.noreply.github.com> Date: Thu, 1 Feb 2024 20:14:08 +0100 Subject: [PATCH] Fixed panic when Highlight is called before the first call to Draw. --- textview.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/textview.go b/textview.go index 8524ee5..2ba9e65 100644 --- a/textview.go +++ b/textview.go @@ -1,6 +1,7 @@ package tview import ( + "math" "strings" "sync" @@ -841,17 +842,24 @@ func (t *TextView) resetIndex() { // parseAhead parses the text buffer starting at the last line in // [TextView.lineIndex] until either the end of the buffer or until stop returns // true for the last complete line that was parsed. If wrapping is enabled, -// width will be used as the available screen width. +// width will be used as the available screen width. If width is 0, it is +// assumed that there is no wrapping. This can happen when this function is +// called before the first time [TextView.Draw] is called. // // There is no guarantee that stop will ever be called. // // The function adds entries to the [TextView.lineIndex] slice and the // [TextView.regions] map and adjusts [TextView.longestLine]. func (t *TextView) parseAhead(width int, stop func(lineNumber int, line *textViewLine) bool) { - if t.text.Len() == 0 || width == 0 { + if t.text.Len() == 0 { return // No text. Nothing to parse. } + // If width is 0, make it infinite. + if width == 0 { + width = math.MaxInt + } + // What kind of tags do we scan for? var options stepOptions if t.styleTags {