mirror of
https://github.com/hymkor/lispect.git
synced 2026-08-27 20:36:34 -04:00
Rename Global to Env for clarity and better library usability
This commit is contained in:
@@ -19,20 +19,20 @@ var (
|
||||
|
||||
var ErrCtrlC = errors.New("^C")
|
||||
|
||||
type Global struct {
|
||||
type Env struct {
|
||||
w *Watcher
|
||||
term *Term
|
||||
closer []func()
|
||||
}
|
||||
|
||||
func (g *Global) Close() {
|
||||
for i := len(g.closer) - 1; i >= 0; i-- {
|
||||
g.closer[i]()
|
||||
func (env *Env) Close() {
|
||||
for i := len(env.closer) - 1; i >= 0; i-- {
|
||||
env.closer[i]()
|
||||
}
|
||||
g.closer = nil
|
||||
env.closer = nil
|
||||
}
|
||||
|
||||
func (g *Global) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
interval := 0
|
||||
for len(args) > 0 {
|
||||
arg := args[0]
|
||||
@@ -51,43 +51,43 @@ func (g *Global) send(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node
|
||||
}
|
||||
if interval > 0 {
|
||||
for _, c := range arg.String() {
|
||||
fmt.Fprintf(g.term, "%c", c)
|
||||
fmt.Fprintf(env.term, "%c", c)
|
||||
if interval > 0 {
|
||||
time.Sleep(time.Millisecond * time.Duration(interval))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
io.WriteString(g.term, arg.String())
|
||||
io.WriteString(env.term, arg.String())
|
||||
}
|
||||
}
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) sendln(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) sendln(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
args = append(args, gmnlisp.Node(gmnlisp.String("\r")))
|
||||
g.send(ctx, w, args)
|
||||
env.send(ctx, w, args)
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) spawn(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
|
||||
argStrings := make([]string, 0, len(args))
|
||||
for _, s := range args {
|
||||
argStrings = append(argStrings, s.String())
|
||||
}
|
||||
|
||||
cmd := g.term.CommandContext(ctx, argStrings[0], argStrings[1:]...)
|
||||
cmd := env.term.CommandContext(ctx, argStrings[0], argStrings[1:]...)
|
||||
if err := cmd.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.closer = append(g.closer, func() { cmd.Wait() })
|
||||
env.closer = append(env.closer, func() { cmd.Wait() })
|
||||
if p := cmd.Process; p != nil {
|
||||
return gmnlisp.Integer(p.Pid), nil
|
||||
}
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
patterns := []string{}
|
||||
timeOut := 0
|
||||
|
||||
@@ -115,9 +115,9 @@ func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node
|
||||
}
|
||||
var result int
|
||||
if timeOut == 0 {
|
||||
result = g.w.Expect(patterns...)
|
||||
result = env.w.Expect(patterns...)
|
||||
} else {
|
||||
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeOut), patterns...)
|
||||
result = env.w.ExpectWithTimeout(time.Second*time.Duration(timeOut), patterns...)
|
||||
}
|
||||
if result == EventCtrlC {
|
||||
return nil, ErrCtrlC
|
||||
@@ -125,7 +125,7 @@ func (g *Global) expect(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node
|
||||
return gmnlisp.Integer(result), nil
|
||||
}
|
||||
|
||||
func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
|
||||
patterns := []string{}
|
||||
actions := []gmnlisp.Node{}
|
||||
@@ -169,9 +169,9 @@ func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Nod
|
||||
|
||||
var result int
|
||||
if timeoutSec == 0 {
|
||||
result = g.w.Expect(patterns...)
|
||||
result = env.w.Expect(patterns...)
|
||||
} else {
|
||||
result = g.w.ExpectWithTimeout(time.Second*time.Duration(timeoutSec), patterns...)
|
||||
result = env.w.ExpectWithTimeout(time.Second*time.Duration(timeoutSec), patterns...)
|
||||
}
|
||||
|
||||
if result == EventCtrlC {
|
||||
@@ -184,7 +184,7 @@ func (g *Global) expectX(ctx context.Context, w *gmnlisp.World, node gmnlisp.Nod
|
||||
}
|
||||
}
|
||||
|
||||
func (g *Global) getenv(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) getenv(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
name, err := gmnlisp.ExpectClass[gmnlisp.String](ctx, w, arg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -196,7 +196,7 @@ func (g *Global) getenv(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node)
|
||||
return gmnlisp.String(value), nil
|
||||
}
|
||||
|
||||
func (g *Global) setenv(ctx context.Context, w *gmnlisp.World, __key, __val gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) setenv(ctx context.Context, w *gmnlisp.World, __key, __val gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
_key, err := gmnlisp.ExpectClass[gmnlisp.String](ctx, w, __key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -215,7 +215,7 @@ func (g *Global) setenv(ctx context.Context, w *gmnlisp.World, __key, __val gmnl
|
||||
return gmnlisp.Null, os.Setenv(key, val)
|
||||
}
|
||||
|
||||
func (g *Global) wait(ctx context.Context, w *gmnlisp.World, pidNode gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
func (env *Env) wait(ctx context.Context, w *gmnlisp.World, pidNode gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
pidInteger, err := gmnlisp.ExpectClass[gmnlisp.Integer](ctx, w, pidNode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -34,11 +34,11 @@ func RunString(script string, args []string) error {
|
||||
|
||||
watcher := NewWatcher(term)
|
||||
|
||||
g := &Global{
|
||||
env := &Env{
|
||||
term: term,
|
||||
w: watcher,
|
||||
}
|
||||
defer g.Close()
|
||||
defer env.Close()
|
||||
|
||||
gmnlisp.NewLineOnFormat = []byte{'\r', '\n'}
|
||||
|
||||
@@ -46,14 +46,14 @@ func RunString(script string, args []string) error {
|
||||
|
||||
lisp = lisp.Flet(
|
||||
gmnlisp.Functions{
|
||||
gmnlisp.NewSymbol("send"): &gmnlisp.Function{Min: 1, F: g.send},
|
||||
gmnlisp.NewSymbol("sendln"): &gmnlisp.Function{Min: 1, F: g.sendln},
|
||||
gmnlisp.NewSymbol("spawn"): &gmnlisp.Function{Min: 1, F: g.spawn},
|
||||
gmnlisp.NewSymbol("expect*"): gmnlisp.SpecialF(g.expectX),
|
||||
gmnlisp.NewSymbol("expect"): gmnlisp.SpecialF(g.expect),
|
||||
gmnlisp.NewSymbol("getenv"): gmnlisp.Function1(g.getenv),
|
||||
gmnlisp.NewSymbol("setenv"): gmnlisp.Function2(g.setenv),
|
||||
gmnlisp.NewSymbol("wait"): gmnlisp.Function1(g.wait),
|
||||
gmnlisp.NewSymbol("send"): &gmnlisp.Function{Min: 1, F: env.send},
|
||||
gmnlisp.NewSymbol("sendln"): &gmnlisp.Function{Min: 1, F: env.sendln},
|
||||
gmnlisp.NewSymbol("spawn"): &gmnlisp.Function{Min: 1, F: env.spawn},
|
||||
gmnlisp.NewSymbol("expect*"): gmnlisp.SpecialF(env.expectX),
|
||||
gmnlisp.NewSymbol("expect"): gmnlisp.SpecialF(env.expect),
|
||||
gmnlisp.NewSymbol("getenv"): gmnlisp.Function1(env.getenv),
|
||||
gmnlisp.NewSymbol("setenv"): gmnlisp.Function2(env.setenv),
|
||||
gmnlisp.NewSymbol("wait"): gmnlisp.Function1(env.wait),
|
||||
})
|
||||
posixArgv := []gmnlisp.Node{}
|
||||
for _, s := range args[1:] {
|
||||
|
||||
Reference in New Issue
Block a user