mirror of
https://github.com/hymkor/lispect.git
synced 2026-08-28 04:46:59 -04:00
Move Global method to global.go
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
|
||||
"github.com/hymkor/gmnlisp"
|
||||
)
|
||||
|
||||
type Global struct {
|
||||
w *Watcher
|
||||
term *Term
|
||||
closer []func()
|
||||
}
|
||||
|
||||
func (g *Global) Close() {
|
||||
for i := len(g.closer) - 1; i >= 0; i-- {
|
||||
g.closer[i]()
|
||||
}
|
||||
g.closer = nil
|
||||
}
|
||||
|
||||
func (g *Global) sendln(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
io.WriteString(g.term, arg.String())
|
||||
g.term.Write([]byte{'\r'})
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) 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())
|
||||
}
|
||||
|
||||
sh := g.term.Command(argStrings[0], argStrings[1:]...)
|
||||
if err := sh.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.closer = append(g.closer, func() { sh.Wait() })
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
patterns := make([]string, 0, len(args))
|
||||
for _, arg := range args {
|
||||
patterns = append(patterns, arg.String())
|
||||
}
|
||||
result := g.w.Expect(patterns...)
|
||||
return gmnlisp.Integer(result), nil
|
||||
}
|
||||
@@ -4,55 +4,11 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/hymkor/gmnlisp"
|
||||
)
|
||||
|
||||
type Global struct {
|
||||
w *Watcher
|
||||
term *Term
|
||||
closer []func()
|
||||
}
|
||||
|
||||
func (g *Global) Close() {
|
||||
for i := len(g.closer) - 1; i >= 0; i-- {
|
||||
g.closer[i]()
|
||||
}
|
||||
g.closer = nil
|
||||
}
|
||||
|
||||
func (g *Global) sendln(ctx context.Context, w *gmnlisp.World, arg gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
io.WriteString(g.term, arg.String())
|
||||
g.term.Write([]byte{'\r'})
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) 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())
|
||||
}
|
||||
|
||||
sh := g.term.Command(argStrings[0], argStrings[1:]...)
|
||||
if err := sh.Start(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
g.closer = append(g.closer, func() { sh.Wait() })
|
||||
return gmnlisp.Null, nil
|
||||
}
|
||||
|
||||
func (g *Global) expect(ctx context.Context, w *gmnlisp.World, args []gmnlisp.Node) (gmnlisp.Node, error) {
|
||||
patterns := make([]string, 0, len(args))
|
||||
for _, arg := range args {
|
||||
patterns = append(patterns, arg.String())
|
||||
}
|
||||
result := g.w.Expect(patterns...)
|
||||
return gmnlisp.Integer(result), nil
|
||||
}
|
||||
|
||||
func mains(args []string) error {
|
||||
term, err := NewTerm()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user