refactor: rewrite terminal library with moonlight

pull/314/merge^2
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 (
"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

8
lua.go
View File

@ -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")