refactor: rewrite terminal library with moonlight

midnight-edition
sammyette 2024-07-20 11:59:14 -04:00
parent 80e6dedf9e
commit f37f5b7ddc
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 20 additions and 25 deletions

View File

@ -5,52 +5,47 @@ package terminal
import ( import (
"os" "os"
"hilbish/util" "hilbish/moonlight"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
"golang.org/x/term" "golang.org/x/term"
) )
var termState *term.State var termState *term.State
var Loader = packagelib.Loader{
Load: loaderFunc,
Name: "terminal",
}
func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { func Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]util.LuaExport{ exports := map[string]moonlight.Export{
"setRaw": util.LuaExport{termsetRaw, 0, false}, "setRaw": {termsetRaw, 0, false},
"restoreState": util.LuaExport{termrestoreState, 0, false}, "restoreState": {termrestoreState, 0, false},
"size": util.LuaExport{termsize, 0, false}, "size": {termsize, 0, false},
"saveState": util.LuaExport{termsaveState, 0, false}, "saveState": {termsaveState, 0, false},
} }
mod := rt.NewTable() mod := moonlight.NewTable()
util.SetExports(rtm, mod, exports) rtm.SetExports(mod, exports)
return rt.TableValue(mod), nil return moonlight.TableValue(mod)
} }
// size() // size()
// Gets the dimensions of the terminal. Returns a table with `width` and `height` // 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. // 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())) w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil { if err != nil {
return nil, err return nil, err
} }
dimensions := rt.NewTable() dimensions := moonlight.NewTable()
dimensions.Set(rt.StringValue("width"), rt.IntValue(int64(w))) dimensions.Set(rt.StringValue("width"), rt.IntValue(int64(w)))
dimensions.Set(rt.StringValue("height"), rt.IntValue(int64(h))) 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() // saveState()
// Saves the current state of the terminal. // 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())) state, err := term.GetState(int(os.Stdin.Fd()))
if err != nil { if err != nil {
return nil, err return nil, err
@ -62,7 +57,7 @@ func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// restoreState() // restoreState()
// Restores the last saved state of the terminal // 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) err := term.Restore(int(os.Stdin.Fd()), termState)
if err != nil { if err != nil {
return nil, err return nil, err
@ -73,7 +68,7 @@ func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
// setRaw() // setRaw()
// Puts the terminal into raw mode. // 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())) _, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil { if err != nil {
return nil, err return nil, err

8
lua.go
View File

@ -8,7 +8,7 @@ import (
"hilbish/golibs/bait" "hilbish/golibs/bait"
"hilbish/golibs/commander" "hilbish/golibs/commander"
"hilbish/golibs/fs" "hilbish/golibs/fs"
//"hilbish/golibs/terminal" "hilbish/golibs/terminal"
"hilbish/moonlight" "hilbish/moonlight"
) )
@ -26,9 +26,9 @@ func luaInit() {
// Add fs and terminal module module to Lua // Add fs and terminal module module to Lua
f := fs.New(runner) f := fs.New(runner)
l.LoadLibrary(f.Loader, "fs") l.LoadLibrary(f.Loader, "fs")
/*
lib.LoadLibs(l, terminal.Loader) l.LoadLibrary(terminal.Loader, "terminal")
*/
cmds = commander.New(l) cmds = commander.New(l)
l.LoadLibrary(cmds.Loader, "commander") l.LoadLibrary(cmds.Loader, "commander")