mirror of https://github.com/Hilbis/Hilbish
96 lines
2.3 KiB
Go
96 lines
2.3 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"hilbish/util"
|
|
"hilbish/golibs/bait"
|
|
"hilbish/golibs/commander"
|
|
"hilbish/golibs/fs"
|
|
"hilbish/golibs/terminal"
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
"github.com/arnodel/golua/lib"
|
|
"github.com/arnodel/golua/lib/debuglib"
|
|
)
|
|
|
|
var minimalconf = `hilbish.prompt '& '`
|
|
|
|
func luaInit() {
|
|
l = rt.New(os.Stdout)
|
|
l.PushContext(rt.RuntimeContextDef{
|
|
MessageHandler: debuglib.Traceback,
|
|
})
|
|
lib.LoadAll(l)
|
|
|
|
lib.LoadLibs(l, hilbishLoader)
|
|
// yes this is stupid, i know
|
|
util.DoString(l, "hilbish = require 'hilbish'")
|
|
|
|
// Add fs and terminal module module to Lua
|
|
lib.LoadLibs(l, fs.Loader)
|
|
lib.LoadLibs(l, terminal.Loader)
|
|
|
|
cmds := commander.New(l)
|
|
// When a command from Lua is added, register it for use
|
|
cmds.Events.On("commandRegister", func(args ...interface{}) {
|
|
cmdName := args[0].(string)
|
|
cmd := args[1].(*rt.Closure)
|
|
|
|
commands[cmdName] = cmd
|
|
})
|
|
cmds.Events.On("commandDeregister", func(args ...interface{}) {
|
|
cmdName := args[0].(string)
|
|
|
|
delete(commands, cmdName)
|
|
})
|
|
lib.LoadLibs(l, cmds.Loader)
|
|
|
|
hooks = bait.New(l)
|
|
hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) {
|
|
fmt.Println("Error in `error` hook handler:", err)
|
|
hooks.Off(event, handler)
|
|
})
|
|
|
|
lib.LoadLibs(l, hooks.Loader)
|
|
|
|
// Add Ctrl-C handler
|
|
hooks.On("signal.sigint", func(...interface{}) {
|
|
if !interactive {
|
|
os.Exit(0)
|
|
}
|
|
})
|
|
|
|
lr.rl.RawInputCallback = func(r []rune) {
|
|
hooks.Emit("hilbish.rawInput", string(r))
|
|
}
|
|
|
|
// Add more paths that Lua can require from
|
|
err := util.DoString(l, "package.path = package.path .. " + requirePaths)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, "Could not add Hilbish require paths! Libraries will be missing. This shouldn't happen.")
|
|
}
|
|
|
|
err1 := util.DoFile(l, "nature/init.lua")
|
|
if err1 != nil {
|
|
err2 := util.DoFile(l, preloadPath)
|
|
if err2 != nil {
|
|
fmt.Fprintln(os.Stderr, "Missing nature module, some functionality and builtins will be missing.")
|
|
fmt.Fprintln(os.Stderr, "local error:", err1)
|
|
fmt.Fprintln(os.Stderr, "global install error:", err2)
|
|
}
|
|
}
|
|
}
|
|
|
|
func runConfig(confpath string) {
|
|
if !interactive {
|
|
return
|
|
}
|
|
err := util.DoFile(l, confpath)
|
|
if err != nil {
|
|
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
|
|
util.DoString(l, minimalconf)
|
|
}
|
|
}
|