"feat: add lua libs for filesystem access and custom commands - written in go"

pull/5/head
TorchedSammy 2021-03-20 18:49:15 -04:00
parent 4456d7593b
commit f910c9dea4
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,38 @@
package commander
import (
"github.com/chuckpreslar/emission"
"github.com/yuin/gopher-lua"
)
type Commander struct{
Events *emission.Emitter
}
func New() Commander {
return Commander{
Events: emission.NewEmitter(),
}
}
func (c *Commander) Loader(L *lua.LState) int {
var exports = map[string]lua.LGFunction{
"register": c.register,
}
mod := L.SetFuncs(L.NewTable(), exports)
L.SetField(mod, "__commands", L.NewTable())
L.Push(mod)
return 1
}
func (c *Commander) register(L *lua.LState) int {
cmdName := L.ToString(1)
cmd := L.ToFunction(2)
c.Events.Emit("commandRegister", cmdName, cmd)
return 0
}

27
golibs/fs/fs.go 100644
View File

@ -0,0 +1,27 @@
package fs
import (
"os"
"github.com/yuin/gopher-lua"
)
func Loader(L *lua.LState) int {
mod := L.SetFuncs(L.NewTable(), exports)
L.Push(mod)
return 1
}
var exports = map[string]lua.LGFunction{
"cd": cd,
}
func cd(L *lua.LState) int {
path := L.ToString(1)
os.Chdir(path)
return 0
}