2021-03-19 23:03:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-31 02:37:08 +00:00
|
|
|
"fmt"
|
2021-05-01 19:35:43 +00:00
|
|
|
"os"
|
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
"hilbish/golibs/bait"
|
2022-03-28 02:14:53 +00:00
|
|
|
/*
|
2021-04-28 22:57:28 +00:00
|
|
|
"hilbish/golibs/commander"
|
2021-04-28 23:22:11 +00:00
|
|
|
"hilbish/golibs/fs"
|
2021-10-17 20:10:57 +00:00
|
|
|
"hilbish/golibs/terminal"
|
2022-03-28 02:14:53 +00:00
|
|
|
*/
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
|
|
|
"github.com/arnodel/golua/lib"
|
2021-03-19 23:03:11 +00:00
|
|
|
)
|
|
|
|
|
2022-01-27 21:01:27 +00:00
|
|
|
var minimalconf = `hilbish.prompt '& '`
|
2021-04-05 00:31:32 +00:00
|
|
|
|
2021-12-09 03:31:48 +00:00
|
|
|
func luaInit() {
|
2022-03-28 02:14:53 +00:00
|
|
|
l = rt.New(os.Stdout)
|
|
|
|
lib.LoadAll(l)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2022-03-28 02:14:53 +00:00
|
|
|
lib.LoadLibs(l, hilbishLoader)
|
2021-05-16 21:13:28 +00:00
|
|
|
// yes this is stupid, i know
|
2022-03-28 02:14:53 +00:00
|
|
|
chunk, _ := l.CompileAndLoadLuaChunk("", []byte("hilbish = require 'hilbish'"), rt.TableValue(l.GlobalEnv()))
|
|
|
|
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
fmt.Println("hsh load", err)
|
2021-05-16 19:53:21 +00:00
|
|
|
|
2021-10-17 20:10:57 +00:00
|
|
|
// Add fs and terminal module module to Lua
|
2022-03-28 02:14:53 +00:00
|
|
|
/* l.PreloadModule("fs", fs.Loader)
|
2021-10-17 20:10:57 +00:00
|
|
|
l.PreloadModule("terminal", terminal.Loader)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
cmds := commander.New()
|
2021-03-31 02:37:08 +00:00
|
|
|
// When a command from Lua is added, register it for use
|
2021-06-12 01:37:10 +00:00
|
|
|
cmds.Events.On("commandRegister", func(cmdName string, cmd *lua.LFunction) {
|
|
|
|
commands[cmdName] = cmd
|
|
|
|
})
|
2021-06-12 14:30:47 +00:00
|
|
|
cmds.Events.On("commandDeregister", func(cmdName string) {
|
|
|
|
delete(commands, cmdName)
|
|
|
|
})
|
2021-04-28 22:57:28 +00:00
|
|
|
l.PreloadModule("commander", cmds.Loader)
|
2022-03-28 02:14:53 +00:00
|
|
|
*/
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
hooks = bait.New()
|
2022-03-28 02:14:53 +00:00
|
|
|
// l.PreloadModule("bait", hooks.Loader)
|
2021-11-22 20:13:05 +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
|
2022-03-28 02:14:53 +00:00
|
|
|
chunk, _ = l.CompileAndLoadLuaChunk("", []byte("package.path = package.path .. " + requirePaths), rt.TableValue(l.GlobalEnv()))
|
|
|
|
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
fmt.Println("package path", err)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2022-03-28 02:14:53 +00:00
|
|
|
data, err := os.ReadFile("prelude/init.lua")
|
2021-03-31 02:37:08 +00:00
|
|
|
if err != nil {
|
2022-03-28 02:14:53 +00:00
|
|
|
data, err = os.ReadFile(preloadPath)
|
2021-03-31 02:37:08 +00:00
|
|
|
if err != nil {
|
2022-03-28 02:14:53 +00:00
|
|
|
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
|
2021-03-31 02:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 02:14:53 +00:00
|
|
|
chunk, _ = l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
|
|
|
|
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
fmt.Println("prelude", err)
|
2021-05-01 18:17:14 +00:00
|
|
|
}
|
2022-03-28 02:14:53 +00:00
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func runConfig(confpath string) {
|
2021-04-28 11:26:23 +00:00
|
|
|
if !interactive {
|
|
|
|
return
|
|
|
|
}
|
2022-03-28 02:14:53 +00:00
|
|
|
data, err := os.ReadFile(confpath)
|
2021-03-31 02:37:08 +00:00
|
|
|
if err != nil {
|
2022-03-28 02:14:53 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
|
|
|
|
chunk, _ := l.CompileAndLoadLuaChunk("", []byte(minimalconf), rt.TableValue(l.GlobalEnv()))
|
|
|
|
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
fmt.Println(err)
|
2021-03-31 02:37:08 +00:00
|
|
|
}
|
2022-03-28 02:14:53 +00:00
|
|
|
chunk, _ := l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
|
|
|
|
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
|
|
|
|
fmt.Println("config", err)
|
2021-03-31 02:37:08 +00:00
|
|
|
}
|