feat: make commanders exit with ctrl c

pull/313/head
sammyette 2024-07-19 10:55:28 -04:00
parent 62dc2e8b02
commit ab4cb0fb54
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 35 additions and 1 deletions

View File

@ -47,3 +47,10 @@ end)
bait.catch('hilbish.notification', function(notif) bait.catch('hilbish.notification', function(notif)
doNotifyPrompt() doNotifyPrompt()
end) end)
local commander = require 'commander'
commander.register('loop', function()
while true do
-- nothing
end
end)

29
exec.go
View File

@ -354,7 +354,34 @@ func execHandle(bg bool) interp.ExecHandlerFunc {
sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.ud)) sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.ud))
sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud)) sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud))
luaexitcode, err := rt.Call1(l.MainThread(), rt.FunctionValue(cmd), rt.TableValue(luacmdArgs), rt.TableValue(sinks)) t := rt.NewThread(l)
sig := make(chan os.Signal)
exit := make(chan bool)
luaexitcode := rt.IntValue(63)
var err error
go func() {
defer func() {
if r := recover(); r != nil {
exit <- true
}
}()
signal.Notify(sig, os.Interrupt)
select {
case <-sig:
t.KillContext()
return
}
}()
go func() {
luaexitcode, err = rt.Call1(t, rt.FunctionValue(cmd), rt.TableValue(luacmdArgs), rt.TableValue(sinks))
exit <- true
}()
<-exit
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, "Error in command:\n" + err.Error()) fmt.Fprintln(os.Stderr, "Error in command:\n" + err.Error())
return interp.NewExitStatus(1) return interp.NewExitStatus(1)