2021-03-19 23:03:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-31 02:37:08 +00:00
|
|
|
"fmt"
|
2021-04-28 22:57:28 +00:00
|
|
|
"hilbish/golibs/bait"
|
|
|
|
"hilbish/golibs/commander"
|
2021-04-19 02:09:27 +00:00
|
|
|
lfs "hilbish/golibs/fs"
|
|
|
|
"os"
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
2021-03-19 23:03:11 +00:00
|
|
|
)
|
|
|
|
|
2021-04-05 00:31:32 +00:00
|
|
|
var minimalconf = `
|
|
|
|
ansikit = require 'ansikit'
|
2021-04-05 21:26:55 +00:00
|
|
|
prompt(ansikit.format(
|
2021-04-05 00:31:32 +00:00
|
|
|
'{blue}%u {cyan}%d {green}∆{reset} '
|
|
|
|
))
|
|
|
|
`
|
|
|
|
|
2021-04-07 12:40:40 +00:00
|
|
|
func LuaInit(confpath string) {
|
2021-03-31 02:37:08 +00:00
|
|
|
l = lua.NewState()
|
|
|
|
|
|
|
|
l.OpenLibs()
|
|
|
|
|
2021-04-03 17:13:45 +00:00
|
|
|
l.SetGlobal("_ver", lua.LString(version))
|
|
|
|
|
2021-03-31 02:37:08 +00:00
|
|
|
l.SetGlobal("prompt", l.NewFunction(hshprompt))
|
2021-04-05 22:07:47 +00:00
|
|
|
l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt))
|
2021-03-31 02:37:08 +00:00
|
|
|
l.SetGlobal("alias", l.NewFunction(hshalias))
|
2021-04-28 22:35:04 +00:00
|
|
|
l.SetGlobal("appendPath", l.NewFunction(hshappendPath))
|
2021-03-31 02:37:08 +00:00
|
|
|
|
|
|
|
// Add fs module to Lua
|
|
|
|
l.PreloadModule("fs", lfs.Loader)
|
|
|
|
|
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-04-28 22:57:28 +00:00
|
|
|
cmds.Events.On("commandRegister",
|
2021-04-19 02:09:27 +00:00
|
|
|
func(cmdName string, cmd *lua.LFunction) {
|
|
|
|
commands[cmdName] = true
|
|
|
|
l.SetField(
|
|
|
|
l.GetTable(l.GetGlobal("commanding"),
|
|
|
|
lua.LString("__commands")),
|
|
|
|
cmdName,
|
|
|
|
cmd)
|
|
|
|
})
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
l.PreloadModule("commander", cmds.Loader)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
hooks = bait.New()
|
|
|
|
l.PreloadModule("bait", hooks.Loader)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
|
|
|
// Add more paths that Lua can require from
|
2021-04-09 22:22:05 +00:00
|
|
|
l.DoString(`package.path = package.path
|
2021-04-10 03:13:28 +00:00
|
|
|
.. ';./libs/?/init.lua;./?/init.lua;./?/?.lua'
|
|
|
|
.. ';/usr/share/hilbish/libs/?/init.lua;'
|
|
|
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/init.lua;'
|
2021-04-11 21:44:34 +00:00
|
|
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?/?.lua;'
|
2021-04-11 21:09:54 +00:00
|
|
|
.. os.getenv 'HOME' .. '/.local/share/hilbish/libs/?.lua'
|
2021-04-09 22:22:05 +00:00
|
|
|
`)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
|
|
|
err := l.DoFile("/usr/share/hilbish/preload.lua")
|
|
|
|
if err != nil {
|
|
|
|
err = l.DoFile("preload.lua")
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run config
|
2021-04-28 11:26:23 +00:00
|
|
|
if !interactive {
|
|
|
|
return
|
|
|
|
}
|
2021-04-07 12:40:40 +00:00
|
|
|
err = l.DoFile(confpath)
|
2021-03-31 02:37:08 +00:00
|
|
|
if err != nil {
|
2021-04-05 00:31:32 +00:00
|
|
|
fmt.Fprintln(os.Stderr, err,
|
2021-04-19 02:09:27 +00:00
|
|
|
"\nAn error has occured while loading your config! Falling back to minimal default config.\n")
|
2021-04-05 00:31:32 +00:00
|
|
|
|
|
|
|
l.DoString(minimalconf)
|
2021-03-31 02:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-05 22:07:47 +00:00
|
|
|
func hshmlprompt(L *lua.LState) int {
|
|
|
|
multilinePrompt = L.ToString(1)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2021-04-28 22:09:10 +00:00
|
|
|
|
2021-04-28 22:35:04 +00:00
|
|
|
func hshappendPath(L *lua.LState) int {
|
2021-04-28 22:09:10 +00:00
|
|
|
path := L.ToString(1)
|
|
|
|
|
2021-04-28 22:35:04 +00:00
|
|
|
os.Setenv("PATH", os.Getenv("PATH") + ":" + path)
|
2021-04-28 22:09:10 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|