Skip to content

Commit

Permalink
feat: commanders can exit via ctrl c (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
TorchedSammy authored Jul 19, 2024
1 parent 826b078 commit 41e5e3f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ hilbish.run('wc -l', {
### Fixed
- Fix ansi attributes causing issues with text when cut off in greenhouse
- `exec` command should return if no arg presented
- Commanders can now be cancelled by Ctrl-C and wont hang the shell anymore.
See [issue 198](https://github.com/Rosettea/Hilbish/issues/198).

## [2.2.3] - 2024-04-27
### Fixed
Expand Down
30 changes: 29 additions & 1 deletion exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
Expand Down Expand Up @@ -353,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)
Expand Down

0 comments on commit 41e5e3f

Please sign in to comment.