Hilbish/lua.go

73 lines
1.5 KiB
Go
Raw Normal View History

2021-03-19 23:03:11 +00:00
package main
import (
2021-03-31 02:37:08 +00:00
"fmt"
"os"
"hilbish/golibs/bait"
"hilbish/golibs/commander"
"hilbish/golibs/fs"
"hilbish/golibs/terminal"
2021-03-31 02:37:08 +00:00
"github.com/yuin/gopher-lua"
2021-03-19 23:03:11 +00:00
)
var minimalconf = `hilbish.prompt '& '`
2021-12-09 03:31:48 +00:00
func luaInit() {
2021-03-31 02:37:08 +00:00
l = lua.NewState()
l.OpenLibs()
// yes this is stupid, i know
l.PreloadModule("hilbish", hilbishLoader)
l.DoString("hilbish = require 'hilbish'")
// Add fs and terminal module module to Lua
l.PreloadModule("fs", fs.Loader)
l.PreloadModule("terminal", terminal.Loader)
2021-03-31 02:37:08 +00:00
cmds := commander.New()
2021-03-31 02:37:08 +00:00
// When a command from Lua is added, register it for use
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
commands[cmdName] = cmd
})
cmds.Events.On("commandDeregister", func(cmdName string) {
delete(commands, cmdName)
})
l.PreloadModule("commander", cmds.Loader)
2021-03-31 02:37:08 +00:00
hooks = bait.New()
l.PreloadModule("bait", hooks.Loader)
2021-03-31 02:37:08 +00:00
// Add Ctrl-C handler
hooks.Em.On("signal.sigint", func() {
if !interactive {
os.Exit(0)
}
})
2021-03-31 02:37:08 +00:00
// Add more paths that Lua can require from
l.DoString("package.path = package.path .. " + requirePaths)
2021-03-31 02:37:08 +00:00
err := l.DoFile("prelude/init.lua")
2021-03-31 02:37:08 +00:00
if err != nil {
err = l.DoFile(preloadPath)
2021-03-31 02:37:08 +00:00
if err != nil {
fmt.Fprintln(os.Stderr,
2021-04-19 02:09:27 +00:00
"Missing preload file, builtins may be missing.")
2021-03-31 02:37:08 +00:00
}
}
}
func runConfig(confpath string) {
2021-04-28 11:26:23 +00:00
if !interactive {
return
}
err := l.DoFile(confpath)
2021-03-31 02:37:08 +00:00
if err != nil {
fmt.Fprintln(os.Stderr, err,
"\nAn error has occured while loading your config! Falling back to minimal default config.")
l.DoString(minimalconf)
2021-03-31 02:37:08 +00:00
}
}