2021-05-16 19:53:21 +00:00
|
|
|
// Here is the core api for the hilbi shell itself
|
|
|
|
// Basically, stuff about the shell itself and other functions
|
|
|
|
// go here.
|
|
|
|
package main
|
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
import (
|
2022-01-26 19:51:52 +00:00
|
|
|
"fmt"
|
2021-05-16 21:13:28 +00:00
|
|
|
"os"
|
2022-01-26 19:51:52 +00:00
|
|
|
"os/exec"
|
2021-09-26 01:39:06 +00:00
|
|
|
"runtime"
|
2021-07-08 10:44:57 +00:00
|
|
|
"strings"
|
2022-01-26 19:51:52 +00:00
|
|
|
"syscall"
|
|
|
|
"time"
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2021-10-16 19:40:08 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
2022-03-02 02:00:46 +00:00
|
|
|
"github.com/maxlandon/readline"
|
2022-03-02 23:11:57 +00:00
|
|
|
"github.com/blackfireio/osinfo"
|
2021-05-16 21:13:28 +00:00
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var exports = map[string]lua.LGFunction {
|
2022-01-26 19:51:52 +00:00
|
|
|
"alias": hlalias,
|
|
|
|
"appendPath": hlappendPath,
|
2022-03-05 15:38:33 +00:00
|
|
|
"complete": hlcomplete,
|
2021-10-16 16:40:53 +00:00
|
|
|
"cwd": hlcwd,
|
2022-01-26 19:51:52 +00:00
|
|
|
"exec": hlexec,
|
2022-03-04 18:40:54 +00:00
|
|
|
"goro": hlgoro,
|
2022-01-26 19:51:52 +00:00
|
|
|
"multiprompt": hlmlprompt,
|
|
|
|
"prependPath": hlprependPath,
|
|
|
|
"prompt": hlprompt,
|
2022-03-02 02:00:46 +00:00
|
|
|
"inputMode": hlinputMode,
|
2022-01-26 19:51:52 +00:00
|
|
|
"interval": hlinterval,
|
2021-10-30 23:53:42 +00:00
|
|
|
"read": hlread,
|
2022-01-26 19:51:52 +00:00
|
|
|
"run": hlrun,
|
|
|
|
"timeout": hltimeout,
|
2022-03-01 22:59:44 +00:00
|
|
|
"which": hlwhich,
|
2021-05-16 21:13:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 17:25:53 +00:00
|
|
|
var greeting string
|
2022-03-02 02:00:46 +00:00
|
|
|
var hshMod *lua.LTable
|
2021-12-31 17:25:53 +00:00
|
|
|
|
2021-12-06 21:21:31 +00:00
|
|
|
func hilbishLoader(L *lua.LState) int {
|
2021-05-16 21:13:28 +00:00
|
|
|
mod := L.SetFuncs(L.NewTable(), exports)
|
2022-03-02 02:00:46 +00:00
|
|
|
hshMod = mod
|
2021-05-16 21:13:28 +00:00
|
|
|
|
|
|
|
host, _ := os.Hostname()
|
2021-09-26 01:39:06 +00:00
|
|
|
username := curuser.Username
|
2021-12-31 17:25:53 +00:00
|
|
|
|
|
|
|
greeting = `Welcome to {magenta}Hilbish{reset}, {cyan}` + curuser.Username + `{reset}.
|
|
|
|
The nice lil shell for {blue}Lua{reset} fanatics!
|
2022-03-06 18:56:04 +00:00
|
|
|
Check out the {blue}{bold}guide{reset} command to get started.
|
2021-12-31 17:25:53 +00:00
|
|
|
`
|
|
|
|
|
2021-09-26 01:39:06 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
|
|
|
|
}
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2021-11-22 23:59:28 +00:00
|
|
|
util.SetField(L, mod, "ver", lua.LString(version), "Hilbish version")
|
|
|
|
util.SetField(L, mod, "user", lua.LString(username), "Username of user")
|
|
|
|
util.SetField(L, mod, "host", lua.LString(host), "Host name of the machine")
|
2021-12-01 22:31:04 +00:00
|
|
|
util.SetField(L, mod, "home", lua.LString(curuser.HomeDir), "Home directory of the user")
|
2021-11-22 23:59:28 +00:00
|
|
|
util.SetField(L, mod, "dataDir", lua.LString(dataDir), "Directory for Hilbish's data files")
|
|
|
|
util.SetField(L, mod, "interactive", lua.LBool(interactive), "If this is an interactive shell")
|
|
|
|
util.SetField(L, mod, "login", lua.LBool(interactive), "Whether this is a login shell")
|
2021-12-31 17:25:53 +00:00
|
|
|
util.SetField(L, mod, "greeting", lua.LString(greeting), "Hilbish's welcome message for interactive shells. It has Lunacolors formatting.")
|
2022-03-02 02:00:46 +00:00
|
|
|
util.SetField(l, mod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
2022-03-06 16:09:10 +00:00
|
|
|
util.SetField(l, hshMod, "exitCode", lua.LNumber(0), "Exit code of last exected command")
|
2021-12-15 00:54:23 +00:00
|
|
|
util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.")
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
// hilbish.userDir table
|
2021-12-14 00:13:17 +00:00
|
|
|
hshuser := L.NewTable()
|
|
|
|
|
2022-02-24 02:19:54 +00:00
|
|
|
util.SetField(L, hshuser, "config", lua.LString(confDir), "User's config directory")
|
2021-12-14 00:13:17 +00:00
|
|
|
util.SetField(L, hshuser, "data", lua.LString(userDataDir), "XDG data directory")
|
|
|
|
util.Document(L, hshuser, "User directories to store configs and/or modules.")
|
|
|
|
L.SetField(mod, "userDir", hshuser)
|
2022-03-05 19:59:00 +00:00
|
|
|
|
|
|
|
// hilbish.os table
|
2022-03-02 23:11:57 +00:00
|
|
|
hshos := L.NewTable()
|
|
|
|
info, _ := osinfo.GetOSInfo()
|
2022-03-05 19:59:00 +00:00
|
|
|
|
2022-03-02 23:11:57 +00:00
|
|
|
util.SetField(L, hshos, "family", lua.LString(info.Family), "Family name of the current OS")
|
|
|
|
util.SetField(L, hshos, "name", lua.LString(info.Name), "Pretty name of the current OS")
|
|
|
|
util.SetField(L, hshos, "version", lua.LString(info.Version), "Version of the current OS")
|
|
|
|
util.Document(L, hshos, "OS info interface")
|
|
|
|
L.SetField(mod, "os", hshos)
|
2021-10-08 00:58:07 +00:00
|
|
|
|
2021-12-15 00:54:23 +00:00
|
|
|
// hilbish.aliases table
|
|
|
|
aliases = NewAliases()
|
|
|
|
aliasesModule := aliases.Loader(L)
|
|
|
|
util.Document(L, aliasesModule, "Alias inferface for Hilbish.")
|
|
|
|
L.SetField(mod, "aliases", aliasesModule)
|
|
|
|
|
2022-01-27 21:02:21 +00:00
|
|
|
// hilbish.history table
|
|
|
|
historyModule := lr.Loader(L)
|
|
|
|
util.Document(L, historyModule, "History interface for Hilbish.")
|
|
|
|
L.SetField(mod, "history", historyModule)
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
// hilbish.completions table
|
|
|
|
hshcomp := L.NewTable()
|
|
|
|
|
|
|
|
util.SetField(L, hshcomp, "files", L.NewFunction(luaFileComplete), "Completer for files")
|
|
|
|
util.SetField(L, hshcomp, "bins", L.NewFunction(luaBinaryComplete), "Completer for executables/binaries")
|
|
|
|
util.Document(L, hshcomp, "Completions interface for Hilbish.")
|
|
|
|
L.SetField(mod, "completion", hshcomp)
|
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
L.Push(mod)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-03-05 19:59:00 +00:00
|
|
|
func luaFileComplete(L *lua.LState) int {
|
|
|
|
query := L.CheckString(1)
|
|
|
|
ctx := L.CheckString(2)
|
|
|
|
fields := L.CheckTable(3)
|
|
|
|
|
|
|
|
var fds []string
|
|
|
|
fields.ForEach(func(k lua.LValue, v lua.LValue) {
|
|
|
|
fds = append(fds, v.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
completions := fileComplete(query, ctx, fds)
|
|
|
|
luaComps := L.NewTable()
|
|
|
|
|
|
|
|
for _, comp := range completions {
|
|
|
|
luaComps.Append(lua.LString(comp))
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(luaComps)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func luaBinaryComplete(L *lua.LState) int {
|
|
|
|
query := L.CheckString(1)
|
|
|
|
ctx := L.CheckString(2)
|
|
|
|
fields := L.CheckTable(3)
|
|
|
|
|
|
|
|
var fds []string
|
|
|
|
fields.ForEach(func(k lua.LValue, v lua.LValue) {
|
|
|
|
fds = append(fds, v.String())
|
|
|
|
})
|
|
|
|
|
|
|
|
completions, _ := binaryComplete(query, ctx, fds)
|
|
|
|
luaComps := L.NewTable()
|
|
|
|
|
|
|
|
for _, comp := range completions {
|
|
|
|
luaComps.Append(lua.LString(comp))
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(luaComps)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-03-02 02:00:46 +00:00
|
|
|
func setVimMode(mode string) {
|
|
|
|
util.SetField(l, hshMod, "vimMode", lua.LString(mode), "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
2022-03-06 16:08:00 +00:00
|
|
|
hooks.Em.Emit("hilbish.vimMode", mode)
|
2022-03-02 02:00:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func unsetVimMode() {
|
|
|
|
util.SetField(l, hshMod, "vimMode", lua.LNil, "Current Vim mode of Hilbish (nil if not in Vim mode)")
|
|
|
|
}
|
|
|
|
|
2021-10-16 16:40:53 +00:00
|
|
|
// run(cmd)
|
2021-12-14 00:13:17 +00:00
|
|
|
// Runs `cmd` in Hilbish's sh interpreter.
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param cmd string
|
2021-10-16 16:40:53 +00:00
|
|
|
func hlrun(L *lua.LState) int {
|
2021-12-07 21:27:07 +00:00
|
|
|
var exitcode uint8
|
2021-05-16 21:13:28 +00:00
|
|
|
cmd := L.CheckString(1)
|
2022-02-27 23:12:58 +00:00
|
|
|
err := execCommand(cmd, cmd)
|
2021-05-16 21:13:28 +00:00
|
|
|
|
|
|
|
if code, ok := interp.IsExitStatus(err); ok {
|
|
|
|
exitcode = code
|
|
|
|
} else if err != nil {
|
|
|
|
exitcode = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(lua.LNumber(exitcode))
|
|
|
|
return 1
|
2021-05-16 19:53:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-16 16:40:53 +00:00
|
|
|
// cwd()
|
|
|
|
// Returns the current directory of the shell
|
|
|
|
func hlcwd(L *lua.LState) int {
|
2021-05-27 23:06:17 +00:00
|
|
|
cwd, _ := os.Getwd()
|
|
|
|
|
|
|
|
L.Push(lua.LString(cwd))
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2021-10-08 00:58:07 +00:00
|
|
|
func getenv(key, fallback string) string {
|
|
|
|
value := os.Getenv(key)
|
|
|
|
if len(value) == 0 {
|
|
|
|
return fallback
|
|
|
|
}
|
|
|
|
return value
|
|
|
|
}
|
2021-10-30 23:53:42 +00:00
|
|
|
|
|
|
|
// read(prompt) -> input?
|
|
|
|
// Read input from the user, using Hilbish's line editor/input reader.
|
|
|
|
// This is a separate instance from the one Hilbish actually uses.
|
|
|
|
// Returns `input`, will be nil if ctrl + d is pressed, or an error occurs (which shouldn't happen)
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param prompt string
|
2021-10-30 23:53:42 +00:00
|
|
|
func hlread(L *lua.LState) int {
|
|
|
|
luaprompt := L.CheckString(1)
|
2022-03-06 18:38:27 +00:00
|
|
|
lualr := newLineReader("", true)
|
2022-03-06 18:23:41 +00:00
|
|
|
lualr.SetPrompt(luaprompt)
|
2021-10-30 23:53:42 +00:00
|
|
|
|
|
|
|
input, err := lualr.Read()
|
|
|
|
if err != nil {
|
|
|
|
L.Push(lua.LNil)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
L.Push(lua.LString(input))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2022-02-26 15:36:04 +00:00
|
|
|
/*
|
|
|
|
prompt(str)
|
2022-01-26 19:51:52 +00:00
|
|
|
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
|
2022-02-26 15:36:04 +00:00
|
|
|
`%h` - Hostname of device
|
|
|
|
--- @param str string
|
|
|
|
*/
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlprompt(L *lua.LState) int {
|
2022-03-05 19:46:38 +00:00
|
|
|
prompt = L.CheckString(1)
|
2022-03-05 01:54:47 +00:00
|
|
|
lr.SetPrompt(fmtPrompt(prompt))
|
2022-01-26 19:51:52 +00:00
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// multiprompt(str)
|
|
|
|
// Changes the continued line prompt to `str`
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param str string
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlmlprompt(L *lua.LState) int {
|
|
|
|
multilinePrompt = L.CheckString(1)
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// alias(cmd, orig)
|
|
|
|
// Sets an alias of `orig` to `cmd`
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param cmd string
|
|
|
|
// --- @param orig string
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlalias(L *lua.LState) int {
|
|
|
|
alias := L.CheckString(1)
|
|
|
|
source := L.CheckString(2)
|
|
|
|
|
|
|
|
aliases.Add(alias, source)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// appendPath(dir)
|
|
|
|
// Appends `dir` to $PATH
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param dir string|table
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlappendPath(L *lua.LState) int {
|
|
|
|
// check if dir is a table or a string
|
|
|
|
arg := L.Get(1)
|
|
|
|
fmt.Println(arg.Type())
|
|
|
|
if arg.Type() == lua.LTTable {
|
|
|
|
arg.(*lua.LTable).ForEach(func(k lua.LValue, v lua.LValue) {
|
|
|
|
appendPath(v.String())
|
|
|
|
})
|
|
|
|
} else if arg.Type() == lua.LTString {
|
|
|
|
appendPath(arg.String())
|
|
|
|
} else {
|
|
|
|
L.RaiseError("bad argument to appendPath (expected string or table, got %v)", L.Get(1).Type().String())
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func appendPath(dir string) {
|
|
|
|
dir = strings.Replace(dir, "~", curuser.HomeDir, 1)
|
|
|
|
pathenv := os.Getenv("PATH")
|
|
|
|
|
|
|
|
// if dir isnt already in $PATH, add it
|
|
|
|
if !strings.Contains(pathenv, dir) {
|
|
|
|
os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// exec(cmd)
|
|
|
|
// Replaces running hilbish with `cmd`
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param cmd string
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlexec(L *lua.LState) int {
|
|
|
|
cmd := L.CheckString(1)
|
|
|
|
cmdArgs, _ := splitInput(cmd)
|
2022-03-02 02:14:55 +00:00
|
|
|
if runtime.GOOS != "windows" {
|
|
|
|
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
|
|
|
|
syscall.Exec(cmdPath, cmdArgs, os.Environ())
|
|
|
|
} else {
|
|
|
|
cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...)
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Run()
|
|
|
|
os.Exit(0)
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 02:14:55 +00:00
|
|
|
return 0
|
2022-01-26 19:51:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// goro(fn)
|
|
|
|
// Puts `fn` in a goroutine
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param fn function
|
2022-03-04 18:40:54 +00:00
|
|
|
func hlgoro(L *lua.LState) int {
|
2022-01-26 19:51:52 +00:00
|
|
|
fn := L.CheckFunction(1)
|
|
|
|
argnum := L.GetTop()
|
|
|
|
args := make([]lua.LValue, argnum)
|
|
|
|
for i := 1; i <= argnum; i++ {
|
|
|
|
args[i - 1] = L.Get(i)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call fn
|
|
|
|
go func() {
|
2022-03-05 02:21:34 +00:00
|
|
|
if err := L.CallByParam(lua.P{
|
|
|
|
Fn: fn,
|
|
|
|
NRet: 0,
|
2022-01-26 19:51:52 +00:00
|
|
|
Protect: true,
|
2022-03-05 02:21:34 +00:00
|
|
|
}, args...); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error in goro function:\n\n", err)
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// timeout(cb, time)
|
|
|
|
// Runs the `cb` function after `time` in milliseconds
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param cb function
|
|
|
|
// --- @param time number
|
2022-01-26 19:51:52 +00:00
|
|
|
func hltimeout(L *lua.LState) int {
|
|
|
|
cb := L.CheckFunction(1)
|
|
|
|
ms := L.CheckInt(2)
|
|
|
|
|
|
|
|
timeout := time.Duration(ms) * time.Millisecond
|
|
|
|
time.Sleep(timeout)
|
|
|
|
|
2022-03-05 02:21:34 +00:00
|
|
|
if err := L.CallByParam(lua.P{
|
|
|
|
Fn: cb,
|
|
|
|
NRet: 0,
|
2022-01-26 19:51:52 +00:00
|
|
|
Protect: true,
|
2022-03-05 02:21:34 +00:00
|
|
|
}); err != nil {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error in goro function:\n\n", err)
|
|
|
|
}
|
2022-01-26 19:51:52 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// interval(cb, time)
|
|
|
|
// Runs the `cb` function every `time` milliseconds
|
2022-02-26 15:36:04 +00:00
|
|
|
// --- @param cb function
|
|
|
|
// --- @param time number
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlinterval(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 {
|
|
|
|
fmt.Fprintln(os.Stderr, "Error in interval function:\n\n", err)
|
|
|
|
stop <- lua.LTrue // stop the interval
|
|
|
|
}
|
|
|
|
case <-stop:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
L.Push(lua.LChannel(stop))
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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`).
|
2022-03-05 20:12:46 +00:00
|
|
|
// `cb` must be a function that returns a table of "completion groups."
|
|
|
|
// A completion group is a table with the keys `items` and `type`.
|
|
|
|
// `items` being a table of items and `type` being the display type of
|
|
|
|
// `grid` (the normal file completion display) or `list` (with a description)
|
2022-03-02 02:12:48 +00:00
|
|
|
// --- @param scope string
|
|
|
|
// --- @param cb function
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlcomplete(L *lua.LState) int {
|
|
|
|
scope := L.CheckString(1)
|
|
|
|
cb := L.CheckFunction(2)
|
|
|
|
|
|
|
|
luaCompletions[scope] = cb
|
|
|
|
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// prependPath(dir)
|
|
|
|
// Prepends `dir` to $PATH
|
2022-03-02 02:12:48 +00:00
|
|
|
// --- @param dir string
|
2022-01-26 19:51:52 +00:00
|
|
|
func hlprependPath(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
|
|
|
|
}
|
2022-03-01 22:59:44 +00:00
|
|
|
|
|
|
|
// which(binName)
|
|
|
|
// Searches for an executable called `binName` in the directories of $PATH
|
2022-03-02 02:12:48 +00:00
|
|
|
// --- @param binName string
|
2022-03-01 22:59:44 +00:00
|
|
|
func hlwhich(L *lua.LState) int {
|
|
|
|
binName := L.CheckString(1)
|
|
|
|
path, err := exec.LookPath(binName)
|
|
|
|
if err != nil {
|
|
|
|
l.Push(lua.LNil)
|
|
|
|
return 1
|
|
|
|
}
|
2022-03-05 19:46:38 +00:00
|
|
|
|
2022-03-01 23:29:30 +00:00
|
|
|
l.Push(lua.LString(path))
|
2022-03-01 22:59:44 +00:00
|
|
|
return 1
|
|
|
|
}
|
2022-03-02 02:00:46 +00:00
|
|
|
|
|
|
|
// inputMode(mode)
|
|
|
|
// Sets the input mode for Hilbish's line reader. Accepts either emacs for vim
|
2022-03-02 02:12:48 +00:00
|
|
|
// --- @param mode string
|
2022-03-02 02:00:46 +00:00
|
|
|
func hlinputMode(L *lua.LState) int {
|
|
|
|
mode := L.CheckString(1)
|
|
|
|
switch mode {
|
|
|
|
case "emacs":
|
|
|
|
unsetVimMode()
|
|
|
|
lr.rl.InputMode = readline.Emacs
|
|
|
|
case "vim":
|
|
|
|
setVimMode("insert")
|
|
|
|
lr.rl.InputMode = readline.Vim
|
|
|
|
default: L.RaiseError("inputMode: expected vim or emacs, received " + mode)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|