From f37f5b7ddcb7c86acf7e9c020e137ebc973e6f38 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 11:59:14 -0400 Subject: [PATCH] refactor: rewrite terminal library with moonlight --- golibs/terminal/terminal.go | 37 ++++++++++++++++--------------------- lua.go | 8 ++++---- 2 files changed, 20 insertions(+), 25 deletions(-) diff --git a/golibs/terminal/terminal.go b/golibs/terminal/terminal.go index 954a4dd..e1af46d 100644 --- a/golibs/terminal/terminal.go +++ b/golibs/terminal/terminal.go @@ -5,52 +5,47 @@ package terminal import ( "os" - "hilbish/util" + "hilbish/moonlight" rt "github.com/arnodel/golua/runtime" - "github.com/arnodel/golua/lib/packagelib" "golang.org/x/term" ) var termState *term.State -var Loader = packagelib.Loader{ - Load: loaderFunc, - Name: "terminal", -} -func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { - exports := map[string]util.LuaExport{ - "setRaw": util.LuaExport{termsetRaw, 0, false}, - "restoreState": util.LuaExport{termrestoreState, 0, false}, - "size": util.LuaExport{termsize, 0, false}, - "saveState": util.LuaExport{termsaveState, 0, false}, +func Loader(rtm *moonlight.Runtime) moonlight.Value { + exports := map[string]moonlight.Export{ + "setRaw": {termsetRaw, 0, false}, + "restoreState": {termrestoreState, 0, false}, + "size": {termsize, 0, false}, + "saveState": {termsaveState, 0, false}, } - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) + mod := moonlight.NewTable() + rtm.SetExports(mod, exports) - return rt.TableValue(mod), nil + return moonlight.TableValue(mod) } // size() // Gets the dimensions of the terminal. Returns a table with `width` and `height` // NOTE: The size refers to the amount of columns and rows of text that can fit in the terminal. -func termsize(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func termsize(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { w, h, err := term.GetSize(int(os.Stdin.Fd())) if err != nil { return nil, err } - dimensions := rt.NewTable() + dimensions := moonlight.NewTable() dimensions.Set(rt.StringValue("width"), rt.IntValue(int64(w))) dimensions.Set(rt.StringValue("height"), rt.IntValue(int64(h))) - return c.PushingNext1(t.Runtime, rt.TableValue(dimensions)), nil + return mlr.PushNext1(c, moonlight.TableValue(dimensions)), nil } // saveState() // Saves the current state of the terminal. -func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func termsaveState(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { state, err := term.GetState(int(os.Stdin.Fd())) if err != nil { return nil, err @@ -62,7 +57,7 @@ func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // restoreState() // Restores the last saved state of the terminal -func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func termrestoreState(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { err := term.Restore(int(os.Stdin.Fd()), termState) if err != nil { return nil, err @@ -73,7 +68,7 @@ func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // setRaw() // Puts the terminal into raw mode. -func termsetRaw(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { +func termsetRaw(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { _, err := term.MakeRaw(int(os.Stdin.Fd())) if err != nil { return nil, err diff --git a/lua.go b/lua.go index 69dc5b6..9e05af1 100644 --- a/lua.go +++ b/lua.go @@ -8,7 +8,7 @@ import ( "hilbish/golibs/bait" "hilbish/golibs/commander" "hilbish/golibs/fs" - //"hilbish/golibs/terminal" + "hilbish/golibs/terminal" "hilbish/moonlight" ) @@ -26,9 +26,9 @@ func luaInit() { // Add fs and terminal module module to Lua f := fs.New(runner) l.LoadLibrary(f.Loader, "fs") - /* - lib.LoadLibs(l, terminal.Loader) -*/ + + l.LoadLibrary(terminal.Loader, "terminal") + cmds = commander.New(l) l.LoadLibrary(cmds.Loader, "commander")