rivo / tview

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

TextArea get panic when undo(`<C-z>`) after `SetText` #844

Closed eeeXun closed 1 year ago

eeeXun commented 1 year ago
package main

import (
    "github.com/gdamore/tcell/v2"
    "github.com/rivo/tview"
)

var (
    app      = tview.NewApplication()
    textArea = tview.NewTextArea()
)

func main() {
    textArea.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
        key := event.Key()
        switch key {
        case tcell.KeyCtrlQ:
            textArea.SetText("", true)
        }
        return event
    })

    if err := app.SetRoot(textArea,
        true).EnableMouse(true).Run(); err != nil {
        panic(err)
    }
}

To reproduce

  1. Type something in textArea
  2. Press <C-q>. In above example code, it'll clear text of textArea.
  3. Press <C-z> to undo

Then get the panic

panic: runtime error: index out of range [1] with length 0 [recovered]
        panic: runtime error: index out of range [1] with length 0

goroutine 1 [running]:
github.com/rivo/tview.(*Application).Run.func1()
        /home/xun/go/pkg/mod/github.com/rivo/tview@v0.0.0-20230406072732-e22ce9588bb4/application.go:251 +0x4d
panic({0x548f20, 0xc00048e018})
        /usr/lib/go/src/runtime/panic.go:884 +0x213
github.com/rivo/tview.(*TextArea).InputHandler.func1(0xc000486220, 0x10?)
        /home/xun/go/pkg/mod/github.com/rivo/tview@v0.0.0-20230406072732-e22ce9588bb4/textarea.go:2189 +0x3258
github.com/rivo/tview.(*Box).WrapInputHandler.func1(0xc000152000?, 0xc000147e58?)
        /home/xun/go/pkg/mod/github.com/rivo/tview@v0.0.0-20230406072732-e22ce9588bb4/box.go:167 +0x53
github.com/rivo/tview.(*Application).Run(0xc00014c000)
        /home/xun/go/pkg/mod/github.com/rivo/tview@v0.0.0-20230406072732-e22ce9588bb4/application.go:344 +0x57d
main.main()
        /home/xun/waste/gotest/tui/main.go:24 +0x7e