2021-03-19 23:03:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-31 02:37:08 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
lfs "hilbish/golibs/fs"
|
|
|
|
cmds "hilbish/golibs/commander"
|
|
|
|
hooks "hilbish/golibs/bait"
|
|
|
|
|
2021-03-19 23:03:11 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
)
|
|
|
|
|
2021-03-31 02:37:08 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-19 23:03:11 +00:00
|
|
|
func hshprompt(L *lua.LState) int {
|
|
|
|
prompt = L.ToString(1)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2021-03-20 22:51:05 +00:00
|
|
|
|
2021-03-21 21:19:51 +00:00
|
|
|
func hshalias(L *lua.LState) int {
|
|
|
|
alias := L.ToString(1)
|
|
|
|
source := L.ToString(2)
|
|
|
|
|
|
|
|
aliases[alias] = source
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|