feat: implement terminal

lua5.4
TorchedSammy 2022-03-29 08:52:26 -04:00
parent e1d4258e07
commit 10f6db20d4
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
3 changed files with 37 additions and 39 deletions

View File

@ -1,5 +1,3 @@
// The fs module provides easy and simple access to filesystem functions and other
// things, and acts an addition to the Lua standard library's I/O and fs functions.
package fs package fs
import ( import (
@ -8,6 +6,7 @@ import (
"strings" "strings"
"hilbish/util" "hilbish/util"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/packagelib"
) )

View File

@ -5,76 +5,78 @@ import (
"hilbish/util" "hilbish/util"
rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
"golang.org/x/term" "golang.org/x/term"
"github.com/yuin/gopher-lua"
) )
var termState *term.State var termState *term.State
var Loader = packagelib.Loader{
func Loader(L *lua.LState) int { Load: loaderFunc,
mod := L.SetFuncs(L.NewTable(), exports) Name: "terminal",
util.Document(L, mod, "The terminal library is a simple and lower level library for certain terminal interactions.")
L.Push(mod)
return 1
} }
var exports = map[string]lua.LGFunction{ func loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
"setRaw": termraw, exports := map[string]util.LuaExport{
"restoreState": termrestoreState, "setRaw": util.LuaExport{termsetRaw, 0, false},
"size": termsize, "restoreState": util.LuaExport{termrestoreState, 0, false},
"saveState": termsaveState, "size": util.LuaExport{termsize, 0, false},
"saveState": util.LuaExport{termsaveState, 0, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
//util.Document(L, mod, "The terminal library is a simple and lower level library for certain terminal interactions.")
return rt.TableValue(mod), nil
} }
// 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: this is not the size in relation to the dimensions of the display // Note: this is not the size in relation to the dimensions of the display
func termsize(L *lua.LState) int { func termsize(t *rt.Thread, c *rt.GoCont) (rt.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 {
L.RaiseError(err.Error()) return nil, err
return 0
} }
dimensions := L.NewTable()
L.SetField(dimensions, "width", lua.LNumber(w))
L.SetField(dimensions, "height", lua.LNumber(h))
L.Push(dimensions) dimensions := rt.NewTable()
return 1 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
} }
// saveState() // saveState()
// Saves the current state of the terminal // Saves the current state of the terminal
func termsaveState(L *lua.LState) int { func termsaveState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
state, err := term.GetState(int(os.Stdin.Fd())) state, err := term.GetState(int(os.Stdin.Fd()))
if err != nil { if err != nil {
L.RaiseError(err.Error()) return nil, err
return 0
} }
termState = state termState = state
return 0 return c.Next(), nil
} }
// restoreState() // restoreState()
// Restores the last saved state of the terminal // Restores the last saved state of the terminal
func termrestoreState(L *lua.LState) int { func termrestoreState(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
err := term.Restore(int(os.Stdin.Fd()), termState) err := term.Restore(int(os.Stdin.Fd()), termState)
if err != nil { if err != nil {
L.RaiseError(err.Error()) return nil, err
} }
return 0 return c.Next(), nil
} }
// setRaw() // setRaw()
// Puts the terminal in raw mode // Puts the terminal in raw mode
func termraw(L *lua.LState) int { func termsetRaw(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
_, err := term.MakeRaw(int(os.Stdin.Fd())) _, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil { if err != nil {
L.RaiseError(err.Error()) return nil, err
} }
return 0 return c.Next(), nil
} }

7
lua.go
View File

@ -8,9 +8,8 @@ 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"
*/
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib" "github.com/arnodel/golua/lib"
) )
@ -27,9 +26,7 @@ func luaInit() {
// Add fs and terminal module module to Lua // Add fs and terminal module module to Lua
lib.LoadLibs(l, fs.Loader) lib.LoadLibs(l, fs.Loader)
/* lib.LoadLibs(l, terminal.Loader)
l.PreloadModule("terminal", terminal.Loader)
*/
cmds := commander.New() cmds := commander.New()
// When a command from Lua is added, register it for use // When a command from Lua is added, register it for use