mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-05 08:37:49 -04:00
Support aborting with Ctrl-C
This commit is contained in:
@@ -16,6 +16,8 @@ var (
|
|||||||
symInterval = gmnlisp.NewSymbol("interval")
|
symInterval = gmnlisp.NewSymbol("interval")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrCtrlC = errors.New("^C")
|
||||||
|
|
||||||
type Global struct {
|
type Global struct {
|
||||||
w *Watcher
|
w *Watcher
|
||||||
term *Term
|
term *Term
|
||||||
@@ -73,7 +75,7 @@ func (g *Global) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Nod
|
|||||||
argStrings = append(argStrings, s.String())
|
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 {
|
if err := sh.Start(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -113,6 +115,9 @@ func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node
|
|||||||
} else {
|
} else {
|
||||||
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeOut), patterns...)
|
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeOut), patterns...)
|
||||||
}
|
}
|
||||||
|
if result == EventCtrlC {
|
||||||
|
return nil, ErrCtrlC
|
||||||
|
}
|
||||||
return gmnlisp.Integer(result), nil
|
return gmnlisp.Integer(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +169,10 @@ func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Nod
|
|||||||
} else {
|
} else {
|
||||||
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeoutSec), patterns...)
|
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])
|
return gmnlisp.Progn(ctx, w, actions[result])
|
||||||
} else {
|
} else {
|
||||||
return gmnlisp.Progn(ctx, w, timeoutAct)
|
return gmnlisp.Progn(ctx, w, timeoutAct)
|
||||||
|
|||||||
@@ -50,7 +50,9 @@ func mains(args []string) error {
|
|||||||
}
|
}
|
||||||
lisp = lisp.Let(&gmnlisp.Pair{Key: gmnlisp.NewSymbol("args"), Value: gmnlisp.List(posixArgv...)})
|
lisp = lisp.Let(&gmnlisp.Pair{Key: gmnlisp.NewSymbol("args"), Value: gmnlisp.List(posixArgv...)})
|
||||||
|
|
||||||
_, err = lisp.Interpret(context.Background(), string(script))
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
_, err = lisp.Interpret(ctx, string(script))
|
||||||
|
cancel()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+30
-7
@@ -1,20 +1,37 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
EventCtrlC = -2
|
||||||
|
EventTimeOut = -1
|
||||||
|
)
|
||||||
|
|
||||||
type Watcher struct {
|
type Watcher struct {
|
||||||
ch <-chan string
|
ch <-chan string
|
||||||
lastline string
|
lastline string
|
||||||
|
ctrlc <-chan struct{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWatcher(pty io.ReadWriter) *Watcher {
|
func NewWatcher(pty io.ReadWriter) *Watcher {
|
||||||
pipeline := make(chan string, 1024)
|
pipeline := make(chan string, 1024)
|
||||||
go io.Copy(pty, os.Stdin)
|
ctrlc := make(chan struct{}, 100)
|
||||||
|
go func() {
|
||||||
|
for {
|
||||||
|
var buffer [1024]byte
|
||||||
|
n, _ := os.Stdin.Read(buffer[:])
|
||||||
|
pty.Write(buffer[:n])
|
||||||
|
if bytes.IndexByte(buffer[:n], '\x03') >= 0 {
|
||||||
|
ctrlc <- struct{}{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
go func() {
|
go func() {
|
||||||
for {
|
for {
|
||||||
var buffer [1024]byte
|
var buffer [1024]byte
|
||||||
@@ -32,7 +49,7 @@ func NewWatcher(pty io.ReadWriter) *Watcher {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return &Watcher{ch: pipeline}
|
return &Watcher{ch: pipeline, ctrlc: ctrlc}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (W *Watcher) checkWords(token string, words []string) int {
|
func (W *Watcher) checkWords(token string, words []string) int {
|
||||||
@@ -52,12 +69,16 @@ func (W *Watcher) checkWords(token string, words []string) int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (W *Watcher) Expect(words ...string) int {
|
func (W *Watcher) Expect(words ...string) int {
|
||||||
for token := range W.ch {
|
for {
|
||||||
if found := W.checkWords(token, words); found >= 0 {
|
select {
|
||||||
return found
|
case token := <-W.ch:
|
||||||
|
if found := W.checkWords(token, words); found >= 0 {
|
||||||
|
return found
|
||||||
|
}
|
||||||
|
case <-W.ctrlc:
|
||||||
|
return EventCtrlC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (W *Watcher) ExpectWithTimeout(d time.Duration, words ...string) int {
|
func (W *Watcher) ExpectWithTimeout(d time.Duration, words ...string) int {
|
||||||
@@ -71,7 +92,9 @@ func (W *Watcher) ExpectWithTimeout(d time.Duration, words ...string) int {
|
|||||||
return found
|
return found
|
||||||
}
|
}
|
||||||
case <-timer.C:
|
case <-timer.C:
|
||||||
return -1
|
return EventTimeOut
|
||||||
|
case <-W.ctrlc:
|
||||||
|
return EventCtrlC
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user