diff --git a/lua.go b/lua.go index f605ebd..b8d0d9b 100644 --- a/lua.go +++ b/lua.go @@ -1,9 +1,63 @@ package main import ( + "fmt" + "os" + lfs "hilbish/golibs/fs" + cmds "hilbish/golibs/commander" + hooks "hilbish/golibs/bait" + "github.com/yuin/gopher-lua" ) +func LuaInit() { + l = lua.NewState() + + l.OpenLibs() + + l.SetGlobal("prompt", l.NewFunction(hshprompt)) + l.SetGlobal("alias", l.NewFunction(hshalias)) + + // Add fs module to Lua + l.PreloadModule("fs", lfs.Loader) + + commander := cmds.New() + // When a command from Lua is added, register it for use + commander.Events.On("commandRegister", + func (cmdName string, cmd *lua.LFunction) { + commands[cmdName] = true + l.SetField( + l.GetTable(l.GetGlobal("commanding"), + lua.LString("__commands")), + cmdName, + cmd) + }) + + l.PreloadModule("commander", commander.Loader) + + bait = hooks.New() + l.PreloadModule("bait", bait.Loader) + + // Add more paths that Lua can require from + 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 { + err = l.DoFile("preload.lua") + if err != nil { + fmt.Fprintln(os.Stderr, + "Missing preload file, builtins may be missing.") + } + } + + homedir, _ := os.UserHomeDir() + // Run config + err = l.DoFile(homedir + "/.hilbishrc.lua") + if err != nil { + panic(err) + } +} + func hshprompt(L *lua.LState) int { prompt = L.ToString(1) diff --git a/main.go b/main.go index 91ecf90..4042128 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,6 @@ import ( "strings" "io" "context" - lfs "hilbish/golibs/fs" - cmds "hilbish/golibs/commander" hooks "hilbish/golibs/bait" "github.com/akamensky/argparse" @@ -304,51 +302,3 @@ func HandleSignals() { }() } -func LuaInit() { - // TODO: Move to lua.go - l = lua.NewState() - - l.OpenLibs() - - l.SetGlobal("prompt", l.NewFunction(hshprompt)) - l.SetGlobal("alias", l.NewFunction(hshalias)) - - // Add fs module to Lua - l.PreloadModule("fs", lfs.Loader) - - commander := cmds.New() - // When a command from Lua is added, register it for use - commander.Events.On("commandRegister", - func (cmdName string, cmd *lua.LFunction) { - commands[cmdName] = true - l.SetField( - l.GetTable(l.GetGlobal("commanding"), - lua.LString("__commands")), - cmdName, - cmd) - }) - - l.PreloadModule("commander", commander.Loader) - - bait = hooks.New() - l.PreloadModule("bait", bait.Loader) - - // Add more paths that Lua can require from - 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 { - err = l.DoFile("preload.lua") - if err != nil { - fmt.Fprintln(os.Stderr, - "Missing preload file, builtins may be missing.") - } - } - - homedir, _ := os.UserHomeDir() - // Run config - err = l.DoFile(homedir + "/.hilbishrc.lua") - if err != nil { - panic(err) - } -}