mirror of https://github.com/Hilbis/Hilbish
feat: use commanding table to store custom commands, load preload file and set require path in go
parent
a3d8f56d78
commit
adb3c0df16
|
@ -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
15
main.go
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue