Support aborting with Ctrl-C

This commit is contained in:
HAYAMA_Kaoru
2024-12-01 13:09:16 +09:00
parent 5abb509017
commit 1b20e3b846
3 changed files with 43 additions and 10 deletions
+10 -2
View File
@@ -16,6 +16,8 @@ var (
symInterval = gmnlisp.NewSymbol("interval")
)
var ErrCtrlC = errors.New("^C")
type Global struct {
w *Watcher
term *Term
@@ -73,7 +75,7 @@ func (g *Global) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Nod
argStrings = append(argStrings, s.String())
}
sh := g.term.Command(argStrings[0], argStrings[1:]...)
sh := g.term.CommandContext(ctx, argStrings[0], argStrings[1:]...)
if err := sh.Start(); err != nil {
return nil, err
}
@@ -113,6 +115,9 @@ func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node
} else {
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeOut), patterns...)
}
if result == EventCtrlC {
return nil, ErrCtrlC
}
return gmnlisp.Integer(result), nil
}
@@ -164,7 +169,10 @@ func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Nod
} else {
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeoutSec), patterns...)
}
if result >= 0 {
if result == EventCtrlC {
return nil, ErrCtrlC
} else if result >= 0 {
return gmnlisp.Progn(ctx, w, actions[result])
} else {
return gmnlisp.Progn(ctx, w, timeoutAct)