[GH-ISSUE #844] TextArea get panic when undo(<C-z>) after SetText #613

Closed
opened 2026-03-04 01:06:27 +03:00 by kerem · 0 comments
Owner

Originally created by @eeeXun on GitHub (May 2, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/844

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
Originally created by @eeeXun on GitHub (May 2, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/844 ```go 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 ```
kerem closed this issue 2026-03-04 01:06:28 +03:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#613
No description provided.