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 (
|
|
|
|
"os"
|
2021-12-14 00:13:17 +00:00
|
|
|
"path/filepath"
|
2021-09-26 01:39:06 +00:00
|
|
|
"runtime"
|
2021-07-08 10:44:57 +00:00
|
|
|
"strings"
|
2021-05-16 21:13:28 +00:00
|
|
|
|
2021-10-16 19:40:08 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
2021-05-16 22:10:46 +00:00
|
|
|
"github.com/pborman/getopt"
|
2021-05-16 21:13:28 +00:00
|
|
|
"github.com/yuin/gopher-lua"
|
|
|
|
"mvdan.cc/sh/v3/interp"
|
|
|
|
)
|
|
|
|
|
|
|
|
var exports = map[string]lua.LGFunction {
|
2021-10-16 16:40:53 +00:00
|
|
|
"run": hlrun,
|
|
|
|
"flag": hlflag,
|
|
|
|
"cwd": hlcwd,
|
2021-10-30 23:53:42 +00:00
|
|
|
"read": hlread,
|
2021-05-16 21:13:28 +00:00
|
|
|
}
|
|
|
|
|
2021-12-31 17:25:53 +00:00
|
|
|
var greeting string
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
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!
|
|
|
|
`
|
|
|
|
|
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.")
|
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()
|
|
|
|
userConfigDir, _ := os.UserConfigDir()
|
|
|
|
userDataDir := ""
|
|
|
|
// i honestly dont know what directories to use for this
|
|
|
|
switch runtime.GOOS {
|
|
|
|
case "linux":
|
|
|
|
userDataDir = getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")
|
|
|
|
default:
|
2021-12-14 13:41:47 +00:00
|
|
|
userDataDir = filepath.Join(userConfigDir)
|
2021-12-14 00:13:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
util.SetField(L, hshuser, "config", lua.LString(userConfigDir), "User's config directory")
|
|
|
|
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)
|
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)
|
|
|
|
|
2021-05-16 21:13:28 +00:00
|
|
|
L.Push(mod)
|
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
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.
|
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)
|
|
|
|
err := execCommand(cmd)
|
|
|
|
|
|
|
|
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
|
|
|
// flag(f)
|
|
|
|
// Checks if the `f` flag has been passed to Hilbish.
|
|
|
|
func hlflag(L *lua.LState) int {
|
2021-05-16 22:10:46 +00:00
|
|
|
flagchar := L.CheckString(1)
|
|
|
|
|
2021-12-14 00:13:17 +00:00
|
|
|
flag := getopt.Lookup([]rune(flagchar)[0])
|
|
|
|
if flag == nil {
|
|
|
|
L.Push(lua.LNil)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
passed := flag.Seen()
|
|
|
|
L.Push(lua.LBool(passed))
|
2021-05-16 22:10:46 +00:00
|
|
|
|
|
|
|
return 1
|
|
|
|
}
|
2021-05-27 23:06:17 +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)
|
|
|
|
func hlread(L *lua.LState) int {
|
|
|
|
luaprompt := L.CheckString(1)
|
2021-12-06 21:21:31 +00:00
|
|
|
lualr := newLineReader(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
|
|
|
|
}
|
|
|
|
|