rivo / tview

Terminal UI library with rich, interactive widgets — written in Golang
MIT License
10.33k stars 545 forks source link

Panic in MouseHandler for TextView with borders #959

Closed Macmod closed 2 months ago

Macmod commented 3 months ago

It seems that https://github.com/rivo/tview/blob/master/textview.go#L1386 is missing a bounds check for when y+t.lineOffset is negative, which leads to a panic in this scenario, when the user clicks on the top border instead of the text itself:

Sample

package main

import (
        "fmt"
        "log"

        "github.com/rivo/tview"
)

func main() {
        tabs := tview.NewTextView().SetRegions(true)
        tabs.SetBorder(true)

        fmt.Fprintf(tabs, `["%s"][white]%s[black][""] `, "0", "Anything")

        myApp := tview.NewApplication()
        myApp.EnableMouse(true)

        myFlex := tview.NewFlex().SetDirection(tview.FlexRow).
                AddItem(
                        tview.NewFlex().
                                AddItem(tabs, 20, 0, false),
                        3, 0, false,
                )

        if err := myApp.SetRoot(myFlex, true).Run(); err != nil {
                log.Fatal(err)
        }
}

Trace

panic: runtime error: index out of range [-1] [recovered]
        panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:285 +0x45
panic({0x545560?, 0xc00012e000?})
        /usr/local/go/src/runtime/panic.go:914 +0x21f
github.com/rivo/tview.(*TextView).MouseHandler.func1(0x3, 0x18?, 0xc000116310)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/textview.go:1386 +0x32e
github.com/rivo/tview.(*TextView).MouseHandler.(*Box).WrapMouseHandler.func2(0xa000?, 0xc000100000?, 0xc000118318?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Flex).MouseHandler.func1(0x3347?, 0xc0001142a0, 0x18?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3
github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc630?, 0x10?, 0x538760?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Flex).MouseHandler.func1(0x5b78?, 0xc0001142a0, 0x10?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/flex.go:239 +0xc3
github.com/rivo/tview.(*Flex).MouseHandler.(*Box).WrapMouseHandler.func2(0xc600?, 0x43798e?, 0x1?)        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/box.go:226 +0x58
github.com/rivo/tview.(*Application).fireMouseActions.func1(0x6500?)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:522 +0x182
github.com/rivo/tview.(*Application).fireMouseActions(0xc00018e000, 0xc0001142a0)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:559 +0x2c9
github.com/rivo/tview.(*Application).Run(0xc00018e000)
        /home/macmod/go/pkg/mod/github.com/rivo/tview@v0.0.0-20240307173318-e804876934a1/application.go:458 +0xbc5
main.main()
        /home/macmod/Code/tview-issue/test.go:26 +0x397
digitallyserviced commented 3 months ago

@Macmod

963

You should be able to pull/go mod edit -replace my PR's branch, or you can wait for @rivo to merge PR... however, he may be a while.

Here's another reply I made with details about using a diff branch if you dont want to wait for @rivo in your project

rivo commented 2 months ago

Fixed with #963