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-05-08 15:50:09 +00:00
|
|
|
"os/exec"
|
2021-05-01 19:35:43 +00:00
|
|
|
"strings"
|
2021-05-08 15:50:09 +00:00
|
|
|
"syscall"
|
2021-06-11 23:40:08 +00:00
|
|
|
"time"
|
2021-05-01 19:35:43 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
"hilbish/golibs/bait"
|
|
|
|
"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"
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-04-28 22:57:28 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
2021-05-26 00:44:03 +00:00
|
|
|
"layeh.com/gopher-luar"
|
2021-03-19 23:03:11 +00:00
|
|
|
)
|
|
|
|
|
2021-04-05 00:31:32 +00:00
|
|
|
var minimalconf = `
|
2021-06-08 21:40:52 +00:00
|
|
|
lunacolors = require 'lunacolors'
|
|
|
|
prompt(lunacolors.format(
|
2021-04-05 00:31:32 +00:00
|
|
|
'{blue}%u {cyan}%d {green}∆{reset} '
|
|
|
|
))
|
|
|
|
`
|
|
|
|
|
2021-05-01 18:17:14 +00:00
|
|
|
func LuaInit() {
|
2021-03-31 02:37:08 +00:00
|
|
|
l = lua.NewState()
|
|
|
|
l.OpenLibs()
|
|
|
|
|
|
|
|
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-11-22 17:46:39 +00:00
|
|
|
l.SetGlobal("prependPath", l.NewFunction(hshprependPath))
|
2021-05-08 15:50:09 +00:00
|
|
|
l.SetGlobal("exec", l.NewFunction(hshexec))
|
2021-05-26 00:44:03 +00:00
|
|
|
l.SetGlobal("goro", luar.New(l, hshgoroutine))
|
2021-06-11 23:40:08 +00:00
|
|
|
l.SetGlobal("timeout", luar.New(l, hshtimeout))
|
2021-06-12 01:21:41 +00:00
|
|
|
l.SetGlobal("interval", l.NewFunction(hshinterval))
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
// yes this is stupid, i know
|
|
|
|
l.PreloadModule("hilbish", HilbishLoader)
|
|
|
|
l.DoString("hilbish = require 'hilbish'")
|
2021-05-16 19:53:21 +00:00
|
|
|
|
2021-10-17 20:10:57 +00:00
|
|
|
// Add fs and terminal module module to Lua
|
2021-04-28 23:22:11 +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)
|
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
|
|
|
|
2021-11-22 20:13:05 +00:00
|
|
|
// Add Ctrl-C handler
|
|
|
|
hooks.Em.On("signal.sigint", func() {
|
|
|
|
if !interactive {
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
l.SetGlobal("complete", l.NewFunction(hshcomplete))
|
|
|
|
|
2021-03-31 02:37:08 +00:00
|
|
|
// Add more paths that Lua can require from
|
2021-06-08 23:16:37 +00:00
|
|
|
l.DoString("package.path = package.path .. " + requirePaths)
|
2021-03-31 02:37:08 +00:00
|
|
|
|
2021-05-11 22:55:05 +00:00
|
|
|
err := l.DoFile("preload.lua")
|
2021-03-31 02:37:08 +00:00
|
|
|
if err != nil {
|
2021-06-09 01:00:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
2021-05-01 18:17:14 +00:00
|
|
|
}
|
|
|
|
func RunConfig(confpath string) {
|
2021-04-28 11:26:23 +00:00
|
|
|
if !interactive {
|
|
|
|
return
|
|
|
|
}
|
2021-05-01 18:17:14 +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-06-08 21:24:54 +00:00
|
|
|
"\nAn error has occured while loading your config! Falling back to minimal default config.")
|
2021-04-05 00:31:32 +00:00
|
|
|
|
|
|
|
l.DoString(minimalconf)
|
2021-03-31 02:37:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 18:08:42 +00:00
|
|
|
func RunLogin() {
|
|
|
|
if _, err := os.Stat(homedir + "/.hprofile.lua"); os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
2021-05-01 18:17:14 +00:00
|
|
|
if !login {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := l.DoFile(homedir + "/.hprofile.lua")
|
2021-05-01 18:08:42 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, err,
|
|
|
|
"\nAn error has occured while loading your login config!n")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:40:53 +00:00
|
|
|
/* prompt(str)
|
|
|
|
Changes the shell prompt to `str`
|
|
|
|
There are a few verbs that can be used in the prompt text.
|
|
|
|
These will be formatted and replaced with the appropriate values.
|
|
|
|
`%d` - Current working directory
|
|
|
|
`%u` - Name of current user
|
|
|
|
`%h` - Hostname of device */
|
2021-03-19 23:03:11 +00:00
|
|
|
func hshprompt(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
prompt = L.CheckString(1)
|
2021-03-19 23:03:11 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2021-03-20 22:51:05 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// multiprompt(str)
|
|
|
|
// Changes the continued line prompt to `str`
|
2021-04-05 22:07:47 +00:00
|
|
|
func hshmlprompt(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
multilinePrompt = L.CheckString(1)
|
2021-04-05 22:07:47 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// alias(cmd, orig)
|
|
|
|
// Sets an alias of `orig` to `cmd`
|
2021-03-21 21:19:51 +00:00
|
|
|
func hshalias(L *lua.LState) int {
|
2021-05-01 17:42:15 +00:00
|
|
|
alias := L.CheckString(1)
|
|
|
|
source := L.CheckString(2)
|
2021-03-21 21:19:51 +00:00
|
|
|
|
|
|
|
aliases[alias] = source
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
2021-04-28 22:09:10 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// appendPath(dir)
|
|
|
|
// Appends `dir` to $PATH
|
2021-04-28 22:35:04 +00:00
|
|
|
func hshappendPath(L *lua.LState) int {
|
2021-05-16 02:23:21 +00:00
|
|
|
dir := L.CheckString(1)
|
|
|
|
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
|
|
|
|
pathenv := os.Getenv("PATH")
|
2021-04-28 22:09:10 +00:00
|
|
|
|
2021-05-16 02:23:21 +00:00
|
|
|
// if dir isnt already in $PATH, add it
|
|
|
|
if !strings.Contains(pathenv, dir) {
|
2021-11-22 17:45:45 +00:00
|
|
|
os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir)
|
2021-05-16 02:23:21 +00:00
|
|
|
}
|
2021-04-28 22:09:10 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2021-05-08 15:50:09 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// exec(cmd)
|
|
|
|
// Replaces running hilbish with `cmd`
|
2021-05-08 15:50:09 +00:00
|
|
|
func hshexec(L *lua.LState) int {
|
|
|
|
cmd := L.CheckString(1)
|
|
|
|
cmdArgs, _ := splitInput(cmd)
|
|
|
|
cmdPath, err := exec.LookPath(cmdArgs[0])
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
// if we get here, cmdPath will be nothing
|
|
|
|
// therefore nothing will run
|
|
|
|
}
|
|
|
|
|
|
|
|
// syscall.Exec requires an absolute path to a binary
|
|
|
|
// path, args, string slice of environments
|
2021-05-16 20:17:55 +00:00
|
|
|
// TODO: alternative for windows
|
2021-05-08 15:50:09 +00:00
|
|
|
syscall.Exec(cmdPath, cmdArgs, os.Environ())
|
2021-05-16 20:17:55 +00:00
|
|
|
return 0 // random thought: does this ever return?
|
2021-05-08 15:50:09 +00:00
|
|
|
}
|
2021-05-26 00:44:03 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// goro(fn)
|
|
|
|
// Puts `fn` in a goroutine
|
2021-05-26 00:44:03 +00:00
|
|
|
func hshgoroutine(gofunc func()) {
|
|
|
|
go gofunc()
|
|
|
|
}
|
2021-06-11 23:40:08 +00:00
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// timeout(cb, time)
|
|
|
|
// Runs the `cb` function after `time` in milliseconds
|
2021-06-11 23:40:08 +00:00
|
|
|
func hshtimeout(timeoutfunc func(), ms int) {
|
|
|
|
timeout := time.Duration(ms) * time.Millisecond
|
2021-09-19 02:48:28 +00:00
|
|
|
time.Sleep(timeout)
|
|
|
|
timeoutfunc()
|
2021-06-11 23:40:08 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 14:21:05 +00:00
|
|
|
// interval(cb, time)
|
|
|
|
// Runs the `cb` function every `time` milliseconds
|
2021-06-12 01:21:41 +00:00
|
|
|
func hshinterval(L *lua.LState) int {
|
|
|
|
intervalfunc := L.CheckFunction(1)
|
|
|
|
ms := L.CheckInt(2)
|
|
|
|
interval := time.Duration(ms) * time.Millisecond
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
|
|
|
stop := make(chan lua.LValue)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
if err := L.CallByParam(lua.P{
|
|
|
|
Fn: intervalfunc,
|
|
|
|
NRet: 0,
|
|
|
|
Protect: true,
|
|
|
|
}); err != nil {
|
2021-06-12 03:54:18 +00:00
|
|
|
fmt.Fprintln(os.Stderr,
|
|
|
|
"Error in interval function:\n\n", err)
|
2021-06-12 01:21:41 +00:00
|
|
|
}
|
|
|
|
case <-stop:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
L.Push(lua.LChannel(stop))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-11-22 16:24:31 +00:00
|
|
|
// complete(scope, cb)
|
|
|
|
// Registers a completion handler for `scope`.
|
|
|
|
// A `scope` is currently only expected to be `command.<cmd>`,
|
|
|
|
// replacing <cmd> with the name of the command (for example `command.git`).
|
|
|
|
// `cb` must be a function that returns a table of the entries to complete.
|
|
|
|
// Nested tables will be used as sub-completions.
|
|
|
|
func hshcomplete(L *lua.LState) int {
|
|
|
|
scope := L.CheckString(1)
|
|
|
|
cb := L.CheckFunction(2)
|
|
|
|
|
|
|
|
luaCompletions[scope] = cb
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
2021-11-22 17:46:39 +00:00
|
|
|
|
|
|
|
// prependPath(dir)
|
|
|
|
// Prepends `dir` to $PATH
|
|
|
|
func hshprependPath(L *lua.LState) int {
|
|
|
|
dir := L.CheckString(1)
|
|
|
|
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
|
|
|
|
pathenv := os.Getenv("PATH")
|
|
|
|
|
|
|
|
// if dir isnt already in $PATH, add in
|
|
|
|
if !strings.Contains(pathenv, dir) {
|
|
|
|
os.Setenv("PATH", dir + string(os.PathListSeparator) + pathenv)
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|