more panic handling

This commit is contained in:
Timon Ringwald 2022-04-04 15:05:35 +02:00
parent 5ac1347366
commit 01dd57a665
4 changed files with 19 additions and 10 deletions

1
go.mod
View File

@ -3,6 +3,7 @@ module git.tordarus.net/Tordarus/tui
go 1.18
require (
git.tordarus.net/Tordarus/adverr v0.2.0
git.tordarus.net/Tordarus/buf2d v1.1.2
github.com/gdamore/tcell v1.4.0
github.com/mattn/go-runewidth v0.0.7

2
go.sum
View File

@ -1,3 +1,5 @@
git.tordarus.net/Tordarus/adverr v0.2.0 h1:kLYjR2/Vb2GHiSAMvAv+WPNaHR9BRphKanf8H/pCZdA=
git.tordarus.net/Tordarus/adverr v0.2.0/go.mod h1:XRf0+7nhOkIEr0gi9DUG4RvV2KaOFB0fYPDaR1KLenw=
git.tordarus.net/Tordarus/buf2d v1.1.2 h1:mmK3tARa30gCh4WaYoSEF5e7qk0C+1ODhxerUcfXN5M=
git.tordarus.net/Tordarus/buf2d v1.1.2/go.mod h1:XXPpS8nQK0gUI0ki7ftV/qlprsGCRWFVSD4ybvDuUL8=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=

View File

@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"git.tordarus.net/Tordarus/adverr"
"git.tordarus.net/Tordarus/buf2d"
"github.com/gdamore/tcell"
)
@ -65,14 +66,14 @@ func (s *Screen) StopWithError(err error) {
func (s *Screen) onKeyPressed(event *KeyEvent) {
if s.KeyPressed == nil || !s.KeyPressed(event) {
s.Root.OnKeyPressed(event)
go s.Root.OnKeyPressed(event)
}
s.Redraw()
}
func (s *Screen) onMouseClicked(event *MouseEvent) {
if s.MouseClicked == nil || !s.MouseClicked(event) {
s.Root.OnMouseClicked(event)
go s.Root.OnMouseClicked(event)
}
if event.Button != MouseButtonNone {
s.Redraw()
@ -93,25 +94,24 @@ func (s *Screen) Redraw() {
}
func (s *Screen) eventloop() {
defer s.stopOnPanic()
defer s.handlePanic("panicked while handling event")
for evt := s.scr.PollEvent(); evt != nil; evt = s.scr.PollEvent() {
switch event := evt.(type) {
case *tcell.EventResize:
go s.Redraw()
s.Redraw()
case *tcell.EventKey:
go s.onKeyPressed(event)
s.onKeyPressed(event)
case *tcell.EventMouse:
go s.onMouseClicked(convertMouseEvent(event))
s.onMouseClicked(convertMouseEvent(event))
default:
s.StopWithError(errors.New(fmt.Sprintf("%#v", event)))
}
}
s.StopWithError(errors.New("unknown error occured"))
}
func (s *Screen) drawloop() {
defer s.stopOnPanic()
defer s.handlePanic("panicked while redrawing")
for range s.redrawCh {
w, h := s.scr.Size()
@ -126,8 +126,12 @@ func (s *Screen) drawloop() {
}
}
func (s *Screen) stopOnPanic() {
func (s *Screen) handlePanic(msg string) {
if err := recover(); err != nil {
s.StopWithError(fmt.Errorf("%v", err))
if e, ok := err.(error); ok {
s.StopWithError(adverr.Wrap(msg, e))
} else {
s.StopWithError(adverr.Wrap(msg, fmt.Errorf("%v", err)))
}
}
}

View File

@ -71,6 +71,8 @@ func TestScrollView(t *testing.T) {
} else if event.Button == tui.MouseWheelDown {
scrollView.Scroll(1, 0)
return true
} else if event.Button == tui.MouseButtonMiddle {
panic("hi")
}
return false