mirror of
https://github.com/hymkor/lispect.git
synced 2026-09-05 08:37:49 -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"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/hymkor/gmnlisp"
|
"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 {
|
func mains(args []string) error {
|
||||||
term, err := NewTerm()
|
term, err := NewTerm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user