feat: use commanding table to store custom commands, load preload file and set require path in go

pull/5/head
TorchedSammy 2021-03-21 12:42:04 -04:00
parent a3d8f56d78
commit adb3c0df16
2 changed files with 13 additions and 5 deletions

View File

@ -20,7 +20,8 @@ func (c *Commander) Loader(L *lua.LState) int {
"register": c.register, "register": c.register,
} }
mod := L.SetFuncs(L.NewTable(), exports) mod := L.SetFuncs(L.NewTable(), exports)
L.SetField(mod, "__commands", L.NewTable()) L.SetGlobal("commanding", &lua.LTable{})
L.SetField(L.GetGlobal("commanding"), "__commands", L.NewTable())
L.Push(mod) L.Push(mod)

15
main.go
View File

@ -111,11 +111,10 @@ func main() {
if len(cmdArgs) == 0 { continue } if len(cmdArgs) == 0 { continue }
if commands[cmdArgs[0]] { if commands[cmdArgs[0]] {
fmt.Printf("%+q", cmdArgs[1:])
err := l.CallByParam(lua.P{ err := l.CallByParam(lua.P{
Fn: l.GetField( Fn: l.GetField(
l.GetTable( l.GetTable(
l.GetGlobal("commander"), l.GetGlobal("commanding"),
lua.LString("__commands")), lua.LString("__commands")),
cmdArgs[0]), cmdArgs[0]),
NRet: 0, NRet: 0,
@ -166,7 +165,7 @@ func LuaInit() {
func (cmdName string, cmd *lua.LFunction) { func (cmdName string, cmd *lua.LFunction) {
commands[cmdName] = true commands[cmdName] = true
l.SetField( l.SetField(
l.GetTable(l.GetGlobal("commander"), l.GetTable(l.GetGlobal("commanding"),
lua.LString("__commands")), lua.LString("__commands")),
cmdName, cmdName,
cmd) cmd)
@ -174,7 +173,15 @@ func LuaInit() {
l.PreloadModule("commander", commander.Loader) l.PreloadModule("commander", commander.Loader)
err := l.DoFile(os.Getenv("HOME") + "/.hilbishrc.lua") l.DoString("package.path = package.path .. ';./libs/?/init.lua;/usr/share/hilbish/libs/?/init.lua'")
err := l.DoFile("/usr/share/hilbish/preload.lua")
if err != nil {
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
}
homedir, _ := os.UserHomeDir()
err = l.DoFile(homedir + "/.hilbishrc.lua")
if err != nil { if err != nil {
panic(err) panic(err)
} }