From cdd31e1c4f1e08f010a9694f19456bde84c6c05e Mon Sep 17 00:00:00 2001 From: sammyette Date: Wed, 2 Apr 2025 15:13:06 -0400 Subject: [PATCH] refactor: move sink to util --- api.go | 3 +-- golibs/snail/lua.go | 3 +-- golibs/snail/snail.go | 7 +++---- {sink => util}/sink.go | 14 ++++++-------- 4 files changed, 11 insertions(+), 16 deletions(-) rename {sink => util}/sink.go (95%) diff --git a/api.go b/api.go index 8c14936..8602942 100644 --- a/api.go +++ b/api.go @@ -23,7 +23,6 @@ import ( "syscall" "time" - "hilbish/sink" "hilbish/util" rt "github.com/arnodel/golua/runtime" @@ -133,7 +132,7 @@ func hilbishLoad(rtm *rt.Runtime) (rt.Value, func()) { pluginModule := moduleLoader(rtm) mod.Set(rt.StringValue("module"), rt.TableValue(pluginModule)) - sinkModule := sink.Loader(l) + sinkModule := util.SinkLoader(l) mod.Set(rt.StringValue("sink"), rt.TableValue(sinkModule)) return rt.TableValue(mod), nil diff --git a/golibs/snail/lua.go b/golibs/snail/lua.go index a8abf0a..0095198 100644 --- a/golibs/snail/lua.go +++ b/golibs/snail/lua.go @@ -6,7 +6,6 @@ import ( "io" "strings" - "hilbish/sink" "hilbish/util" rt "github.com/arnodel/golua/runtime" @@ -139,7 +138,7 @@ func handleStream(v rt.Value, strms *util.Streams, errStream, inStream bool) err varstrm = f.Handle() } - if f, ok := val.(*sink.Sink); ok { + if f, ok := val.(*util.Sink); ok { varstrm = f.Rw } diff --git a/golibs/snail/snail.go b/golibs/snail/snail.go index ddfef49..b09af3b 100644 --- a/golibs/snail/snail.go +++ b/golibs/snail/snail.go @@ -13,7 +13,6 @@ import ( "strings" "time" - "hilbish/sink" "hilbish/util" rt "github.com/arnodel/golua/runtime" @@ -118,9 +117,9 @@ func (s *snail) Run(cmd string, strms *util.Streams) (bool, io.Writer, io.Writer cmds[k.AsString()] = v.AsTable().Get(rt.StringValue("exec")).AsClosure() }) if cmd := cmds[args[0]]; cmd != nil { - stdin := sink.NewSinkInput(s.runtime, hc.Stdin) - stdout := sink.NewSinkOutput(s.runtime, hc.Stdout) - stderr := sink.NewSinkOutput(s.runtime, hc.Stderr) + stdin := util.NewSinkInput(s.runtime, hc.Stdin) + stdout := util.NewSinkOutput(s.runtime, hc.Stdout) + stderr := util.NewSinkOutput(s.runtime, hc.Stderr) sinks := rt.NewTable() sinks.Set(rt.StringValue("in"), rt.UserDataValue(stdin.UserData)) diff --git a/sink/sink.go b/util/sink.go similarity index 95% rename from sink/sink.go rename to util/sink.go index 2b17373..69fc594 100644 --- a/sink/sink.go +++ b/util/sink.go @@ -1,4 +1,4 @@ -package sink +package util import ( "bufio" @@ -8,8 +8,6 @@ import ( "os" "strings" - "hilbish/util" - rt "github.com/arnodel/golua/runtime" ) @@ -25,11 +23,11 @@ type Sink struct{ autoFlush bool } -func Loader(rtm *rt.Runtime) *rt.Table { +func SinkLoader(rtm *rt.Runtime) *rt.Table { sinkMeta := rt.NewTable() sinkMethods := rt.NewTable() - sinkFuncs := map[string]util.LuaExport{ + sinkFuncs := map[string]LuaExport{ "flush": {luaSinkFlush, 1, false}, "read": {luaSinkRead, 1, false}, "readAll": {luaSinkReadAll, 1, false}, @@ -37,7 +35,7 @@ func Loader(rtm *rt.Runtime) *rt.Table { "write": {luaSinkWrite, 2, false}, "writeln": {luaSinkWriteln, 2, false}, } - util.SetExports(rtm, sinkMethods, sinkFuncs) + SetExports(rtm, sinkMethods, sinkFuncs) sinkIndex := func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { s, _ := sinkArg(c, 0) @@ -66,12 +64,12 @@ func Loader(rtm *rt.Runtime) *rt.Table { sinkMeta.Set(rt.StringValue("__index"), rt.FunctionValue(rt.NewGoFunction(sinkIndex, "__index", 2, false))) rtm.SetRegistry(sinkMetaKey, rt.TableValue(sinkMeta)) - exports := map[string]util.LuaExport{ + exports := map[string]LuaExport{ "new": {luaSinkNew, 0, false}, } mod := rt.NewTable() - util.SetExports(rtm, mod, exports) + SetExports(rtm, mod, exports) return mod }