rivo / tview

Terminal UI library with rich, interactive widgets — written in Golang

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Out-of-bounds panic when text wrapping is enabled

darkhz opened this issue · comments

When text-wrapping is enabled on an empty textview, i.e. set by (*tview.TextView).SetWrap(true) and ScrollToHighlight() is used,
an out of bounds panic occurs when a region is highlighted, but the lineIndex buffer is empty (textview.go#L1070C21-L1070C21).

Maybe check whether the lineIndex buffer is empty before getting highlights?

if t.wrap && len(lineIndex) > fromHighlight { 				
}
commented

Can you please provide a small program that reproduces this error?

Here is a small example to illustrate the above issue.
I have also edited the first comment above for clarification.

func main() {
	tv := tview.NewTextView()
	tv.SetWrap(true)
	tv.SetRegions(true)
	tv.SetDynamicColors(true)
	tv.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
		if event.Rune() == 'h' {
			tv.Highlight("region")
			tv.ScrollToHighlight()
		}

		return event
	})

        tview.NewApplication().SetRoot(tv, true).Run()
}
commented

Thank you. The latest commit should fix this. There were two issues here:

  • The Highlight method could highlight regions that didn't exist in the text. That is now not possible anymore.
  • One could cause the line index to be reset between a call to Highlight and the next time the text view is drawn. In that case, we have to parse again until all highlighted regions are known. So I added that, too.