Fix crash on command not found

This commit is contained in:
Drew DeVault 2019-03-30 16:29:52 -04:00
parent 45b4c8a724
commit fd27a2baf6
1 changed files with 46 additions and 41 deletions

View File

@ -4,7 +4,6 @@ import (
gocolor "image/color" gocolor "image/color"
"os" "os"
"os/exec" "os/exec"
"sync"
"git.sr.ht/~sircmpwn/aerc2/lib/ui" "git.sr.ht/~sircmpwn/aerc2/lib/ui"
@ -96,9 +95,9 @@ type Terminal struct {
cursorPos vterm.Pos cursorPos vterm.Pos
cursorShown bool cursorShown bool
damage []vterm.Rect damage []vterm.Rect
destroyed bool
err error err error
focus bool focus bool
mutex sync.Mutex
onInvalidate func(d ui.Drawable) onInvalidate func(d ui.Drawable)
pty *os.File pty *os.File
start chan interface{} start chan interface{}
@ -196,13 +195,7 @@ func (term *Terminal) Close(err error) {
if term.closed { if term.closed {
return return
} }
term.mutex.Lock()
defer term.mutex.Unlock()
term.err = err term.err = err
if term.vterm != nil {
term.vterm.Close()
term.vterm = nil
}
if term.pty != nil { if term.pty != nil {
term.pty.Close() term.pty.Close()
term.pty = nil term.pty = nil
@ -218,6 +211,20 @@ func (term *Terminal) Close(err error) {
term.ctx.HideCursor() term.ctx.HideCursor()
} }
func (term *Terminal) Destroy() {
if term.destroyed {
return
}
if term.vterm != nil {
term.vterm.Close()
term.vterm = nil
}
if term.ctx != nil {
term.ctx.HideCursor()
}
term.destroyed = true
}
func (term *Terminal) OnInvalidate(cb func(d ui.Drawable)) { func (term *Terminal) OnInvalidate(cb func(d ui.Drawable)) {
term.onInvalidate = cb term.onInvalidate = cb
} }
@ -229,13 +236,13 @@ func (term *Terminal) Invalidate() {
} }
func (term *Terminal) Draw(ctx *ui.Context) { func (term *Terminal) Draw(ctx *ui.Context) {
if term.closed { if term.destroyed {
return return
} }
term.mutex.Lock() term.ctx = ctx // gross
defer term.mutex.Unlock()
if !term.closed {
winsize := pty.Winsize{ winsize := pty.Winsize{
Cols: uint16(ctx.Width()), Cols: uint16(ctx.Width()),
Rows: uint16(ctx.Height()), Rows: uint16(ctx.Height()),
@ -246,7 +253,6 @@ func (term *Terminal) Draw(ctx *ui.Context) {
tty, err := pty.StartWithSize(term.cmd, &winsize) tty, err := pty.StartWithSize(term.cmd, &winsize)
term.pty = tty term.pty = tty
if err != nil { if err != nil {
term.mutex.Unlock()
term.Close(err) term.Close(err)
return return
} }
@ -256,8 +262,6 @@ func (term *Terminal) Draw(ctx *ui.Context) {
} }
} }
term.ctx = ctx // gross
rows, cols, err := pty.Getsize(term.pty) rows, cols, err := pty.Getsize(term.pty)
if err != nil { if err != nil {
return return
@ -267,6 +271,7 @@ func (term *Terminal) Draw(ctx *ui.Context) {
term.vterm.SetSize(ctx.Height(), ctx.Width()) term.vterm.SetSize(ctx.Height(), ctx.Width())
return return
} }
}
screen := term.vterm.ObtainScreen() screen := term.vterm.ObtainScreen()
@ -299,7 +304,7 @@ func (term *Terminal) Draw(ctx *ui.Context) {
} }
} }
if term.focus { if term.focus && !term.closed {
if !term.cursorShown { if !term.cursorShown {
ctx.HideCursor() ctx.HideCursor()
} else { } else {