From ab4cb0fb54ce2edef6677dbf43eafabff735c16c Mon Sep 17 00:00:00 2001 From: sammyette Date: Fri, 19 Jul 2024 10:55:28 -0400 Subject: [PATCH] feat: make commanders exit with ctrl c --- .hilbishrc.lua | 7 +++++++ exec.go | 29 ++++++++++++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/.hilbishrc.lua b/.hilbishrc.lua index 249f97e..649f5ab 100644 --- a/.hilbishrc.lua +++ b/.hilbishrc.lua @@ -47,3 +47,10 @@ end) bait.catch('hilbish.notification', function(notif) doNotifyPrompt() end) + +local commander = require 'commander' +commander.register('loop', function() + while true do + -- nothing + end +end) diff --git a/exec.go b/exec.go index 77b72ec..5def9aa 100644 --- a/exec.go +++ b/exec.go @@ -354,7 +354,34 @@ func execHandle(bg bool) interp.ExecHandlerFunc { sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.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 { fmt.Fprintln(os.Stderr, "Error in command:\n" + err.Error()) return interp.NewExitStatus(1)