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

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,43 +236,41 @@ 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()
defer term.mutex.Unlock()
winsize := pty.Winsize{
Cols: uint16(ctx.Width()),
Rows: uint16(ctx.Height()),
}
if term.pty == nil {
term.vterm.SetSize(ctx.Height(), ctx.Width())
tty, err := pty.StartWithSize(term.cmd, &winsize)
term.pty = tty
if err != nil {
term.mutex.Unlock()
term.Close(err)
return
}
term.start <- nil
if term.OnStart != nil {
term.OnStart()
}
}
term.ctx = ctx // gross term.ctx = ctx // gross
rows, cols, err := pty.Getsize(term.pty) if !term.closed {
if err != nil { winsize := pty.Winsize{
return Cols: uint16(ctx.Width()),
} Rows: uint16(ctx.Height()),
if ctx.Width() != cols || ctx.Height() != rows { }
pty.Setsize(term.pty, &winsize)
term.vterm.SetSize(ctx.Height(), ctx.Width()) if term.pty == nil {
return term.vterm.SetSize(ctx.Height(), ctx.Width())
tty, err := pty.StartWithSize(term.cmd, &winsize)
term.pty = tty
if err != nil {
term.Close(err)
return
}
term.start <- nil
if term.OnStart != nil {
term.OnStart()
}
}
rows, cols, err := pty.Getsize(term.pty)
if err != nil {
return
}
if ctx.Width() != cols || ctx.Height() != rows {
pty.Setsize(term.pty, &winsize)
term.vterm.SetSize(ctx.Height(), ctx.Width())
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 {