mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-01 14:47:43 -04:00
Implement the option 'interval for (send) and (sendln)
This commit is contained in:
@@ -2,6 +2,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"time"
|
||||||
@@ -9,6 +11,11 @@ import (
|
|||||||
"github.com/hymkor/gmnlisp"
|
"github.com/hymkor/gmnlisp"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
symTimeout = gmnlisp.NewSymbol("timeout")
|
||||||
|
symInterval = gmnlisp.NewSymbol("interval")
|
||||||
|
)
|
||||||
|
|
||||||
type Global struct {
|
type Global struct {
|
||||||
w *Watcher
|
w *Watcher
|
||||||
term *Term
|
term *Term
|
||||||
@@ -22,14 +29,40 @@ func (g *Global) Close() {
|
|||||||
g.closer = nil
|
g.closer = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Global) send(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
func (g *Global) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||||
io.WriteString(g.term, arg.String())
|
interval := 0
|
||||||
|
for len(args) > 0 {
|
||||||
|
arg := args[0]
|
||||||
|
args = args[1:]
|
||||||
|
if symInterval.Equals(arg, gmnlisp.EQUALP) {
|
||||||
|
if len(args) <= 0 {
|
||||||
|
return nil, errors.New("too few arguments")
|
||||||
|
}
|
||||||
|
_interval, err := gmnlisp.ExpectClass[gmnlisp.Integer](ctx, w, args[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
args = args[1:]
|
||||||
|
interval = int(_interval)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if interval > 0 {
|
||||||
|
for _, c := range arg.String() {
|
||||||
|
fmt.Fprintf(g.term, "%c", c)
|
||||||
|
if interval > 0 {
|
||||||
|
time.Sleep(time.Millisecond * time.Duration(interval))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
io.WriteString(g.term, arg.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
return gmnlisp.Null, nil
|
return gmnlisp.Null, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *Global) sendln(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
func (g *Global) sendln(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||||
io.WriteString(g.term, arg.String())
|
args = append(args, gmnlisp.Node(gmnlisp.String("\r")))
|
||||||
g.term.Write([]byte{'\r'})
|
g.send(ctx, w, args)
|
||||||
return gmnlisp.Null, nil
|
return gmnlisp.Null, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,8 +81,6 @@ func (g *Global) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Nod
|
|||||||
return gmnlisp.Null, nil
|
return gmnlisp.Null, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
var symTimeout = gmnlisp.NewSymbol("timeout")
|
|
||||||
|
|
||||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||||
patterns := []string{}
|
patterns := []string{}
|
||||||
timeOut := 0
|
timeOut := 0
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ func mains(args []string) error {
|
|||||||
|
|
||||||
lisp = lisp.Flet(
|
lisp = lisp.Flet(
|
||||||
gmnlisp.Functions{
|
gmnlisp.Functions{
|
||||||
gmnlisp.NewSymbol("send"): gmnlisp.Function1(g.send),
|
gmnlisp.NewSymbol("send"): &gmnlisp.Function{Min: 1, F: g.send},
|
||||||
gmnlisp.NewSymbol("sendln"): gmnlisp.Function1(g.sendln),
|
gmnlisp.NewSymbol("sendln"): &gmnlisp.Function{Min: 1, F: g.sendln},
|
||||||
gmnlisp.NewSymbol("spawn"): &gmnlisp.Function{Min: 1, F: g.spawn},
|
gmnlisp.NewSymbol("spawn"): &gmnlisp.Function{Min: 1, F: g.spawn},
|
||||||
gmnlisp.NewSymbol("expect*"): gmnlisp.SpecialF(g.expectX),
|
gmnlisp.NewSymbol("expect*"): gmnlisp.SpecialF(g.expectX),
|
||||||
gmnlisp.NewSymbol("expect"): gmnlisp.SpecialF(g.expect),
|
gmnlisp.NewSymbol("expect"): gmnlisp.SpecialF(g.expect),
|
||||||
|
|||||||
Reference in New Issue
Block a user