Hilbish/main.go

265 lines
6.0 KiB
Go
Raw Normal View History

2021-03-19 19:11:59 +00:00
package main
import (
"bufio"
2021-03-19 19:11:59 +00:00
"fmt"
2021-04-19 02:09:27 +00:00
"io"
2021-03-19 19:11:59 +00:00
"os"
2021-04-19 02:09:27 +00:00
"os/user"
"path/filepath"
"runtime"
"strings"
"hilbish/golibs/bait"
"github.com/pborman/getopt"
2021-03-19 23:03:11 +00:00
"github.com/yuin/gopher-lua"
"github.com/maxlandon/readline"
"golang.org/x/term"
2021-03-19 19:11:59 +00:00
)
var (
2021-04-20 21:51:12 +00:00
l *lua.LState
lr *lineReader
2021-04-19 02:09:27 +00:00
commands = map[string]*lua.LFunction{}
luaCompletions = map[string]*lua.LFunction{}
2021-04-19 02:09:27 +00:00
confDir string
userDataDir string
curuser *user.User
hooks bait.Bait
defaultConfPath string
defaultHistPath string
2021-04-20 21:51:12 +00:00
)
2021-03-19 23:03:11 +00:00
2021-03-19 19:11:59 +00:00
func main() {
curuser, _ = user.Current()
homedir := curuser.HomeDir
confDir, _ = os.UserConfigDir()
preloadPath = strings.Replace(preloadPath, "~", homedir, 1)
sampleConfPath = strings.Replace(sampleConfPath, "~", homedir, 1)
// 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:
// this is fine on windows, dont know about others
userDataDir = confDir
}
if defaultConfDir == "" {
// we'll add *our* default if its empty (wont be if its changed comptime)
defaultConfPath = filepath.Join(confDir, "hilbish", "init.lua")
} else {
// else do ~ substitution
defaultConfPath = filepath.Join(strings.Replace(defaultConfDir, "~", homedir, 1), "init.lua")
}
if defaultHistDir == "" {
defaultHistPath = filepath.Join(userDataDir, "hilbish", ".hilbish-history")
} else {
defaultHistPath = filepath.Join(strings.Replace(defaultHistDir, "~", homedir, 1), ".hilbish-history")
}
2021-06-12 14:48:57 +00:00
helpflag := getopt.BoolLong("help", 'h', "Prints Hilbish flags")
verflag := getopt.BoolLong("version", 'v', "Prints Hilbish version")
setshflag := getopt.BoolLong("setshellenv", 'S', "Sets $SHELL to Hilbish's executed path")
2021-05-11 22:19:53 +00:00
cmdflag := getopt.StringLong("command", 'c', "", "Executes a command on startup")
configflag := getopt.StringLong("config", 'C', defaultConfPath, "Sets the path to Hilbish's config")
2021-06-12 14:49:22 +00:00
getopt.BoolLong("login", 'l', "Force Hilbish to be a login shell")
2021-05-08 13:12:21 +00:00
getopt.BoolLong("interactive", 'i', "Force Hilbish to be an interactive shell")
2021-05-08 13:30:32 +00:00
getopt.BoolLong("noexec", 'n', "Don't execute and only report Lua syntax errors")
getopt.Parse()
loginshflag := getopt.Lookup('l').Seen()
interactiveflag := getopt.Lookup('i').Seen()
2021-05-08 12:56:24 +00:00
noexecflag := getopt.Lookup('n').Seen()
2021-06-12 14:48:57 +00:00
if *helpflag {
getopt.PrintUsage(os.Stdout)
os.Exit(0)
}
if *cmdflag == "" || interactiveflag {
interactive = true
}
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
interactive = false
}
if getopt.NArgs() > 0 {
interactive = false
}
2021-05-08 12:56:24 +00:00
if noexecflag {
noexecute = true
}
// first arg, first character
if loginshflag || os.Args[0][0] == '-' {
login = true
}
if *verflag {
fmt.Printf("Hilbish %s\n", version)
os.Exit(0)
}
2021-03-28 22:58:58 +00:00
// Set $SHELL if the user wants to
2021-04-19 02:09:27 +00:00
if *setshflag {
os.Setenv("SHELL", os.Args[0])
}
2021-03-21 07:51:44 +00:00
go handleSignals()
luaInit()
2022-03-06 18:38:27 +00:00
lr = newLineReader("", false)
2021-03-28 22:58:58 +00:00
// If user's config doesn't exixt,
if _, err := os.Stat(defaultConfPath); os.IsNotExist(err) && *configflag == defaultConfPath {
2021-03-30 23:47:02 +00:00
// Read default from current directory
// (this is assuming the current dir is Hilbish's git)
_, err := os.ReadFile(".hilbishrc.lua")
confpath := ".hilbishrc.lua"
2021-03-30 23:47:02 +00:00
if err != nil {
// If it wasnt found, go to the real sample conf
_, err = os.ReadFile(sampleConfPath)
confpath = sampleConfPath
2021-03-30 23:47:02 +00:00
if err != nil {
fmt.Println("could not find .hilbishrc.lua or", sampleConfPath)
2021-03-30 23:47:02 +00:00
return
}
}
runConfig(confpath)
} else {
runConfig(*configflag)
}
2021-03-21 07:51:44 +00:00
if fileInfo, _ := os.Stdin.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
scanner := bufio.NewScanner(bufio.NewReader(os.Stdin))
for scanner.Scan() {
text := scanner.Text()
runInput(text, text)
}
}
if *cmdflag != "" {
runInput(*cmdflag, *cmdflag)
}
if getopt.NArgs() > 0 {
luaArgs := l.NewTable()
for _, arg := range getopt.Args() {
luaArgs.Append(lua.LString(arg))
}
l.SetGlobal("args", luaArgs)
err := l.DoFile(getopt.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
os.Exit(0)
}
input:
for interactive {
2021-04-14 17:20:03 +00:00
running = false
2021-04-16 14:27:11 +00:00
input, err := lr.Read()
2021-04-16 14:27:11 +00:00
2021-03-20 16:57:18 +00:00
if err == io.EOF {
2021-03-28 22:58:58 +00:00
// Exit if user presses ^D (ctrl + d)
hooks.Em.Emit("hilbish.exit")
2021-03-20 16:57:18 +00:00
break
}
2021-03-19 19:11:59 +00:00
if err != nil {
if err != readline.CtrlC {
// If we get a completely random error, print
fmt.Fprintln(os.Stderr, err)
}
fmt.Println("^C")
continue
2021-03-19 19:11:59 +00:00
}
oldInput := input
2021-03-28 22:58:58 +00:00
2021-04-05 00:30:03 +00:00
input = strings.TrimSpace(input)
if len(input) == 0 {
hooks.Em.Emit("command.exit", 0)
continue
}
2021-04-05 00:30:03 +00:00
if strings.HasSuffix(input, "\\") {
for {
input, err = continuePrompt(input)
if err != nil {
goto input // continue inside nested loop
}
if !strings.HasSuffix(input, "\\") {
2021-04-19 02:09:27 +00:00
break
}
}
}
runInput(input, oldInput)
termwidth, _, err := term.GetSize(0)
2021-04-19 02:09:27 +00:00
if err != nil {
continue
}
2021-04-24 03:44:59 +00:00
fmt.Printf("\u001b[7m∆\u001b[0m" + strings.Repeat(" ", termwidth - 1) + "\r")
}
}
func continuePrompt(prev string) (string, error) {
hooks.Em.Emit("multiline", nil)
lr.SetPrompt(multilinePrompt)
cont, err := lr.Read()
if err != nil {
return "", err
2021-03-19 19:11:59 +00:00
}
cont = strings.TrimSpace(cont)
return prev + strings.TrimSuffix(cont, "\n"), nil
2021-03-19 19:11:59 +00:00
}
2021-03-28 22:58:58 +00:00
// This semi cursed function formats our prompt (obviously)
func fmtPrompt(prompt string) string {
host, _ := os.Hostname()
cwd, _ := os.Getwd()
2021-03-28 22:58:58 +00:00
if strings.HasPrefix(cwd, curuser.HomeDir) {
cwd = "~" + strings.TrimPrefix(cwd, curuser.HomeDir)
}
username := curuser.Username
// this will be baked into binary since GOOS is a constant
if runtime.GOOS == "windows" {
username = strings.Split(username, "\\")[1] // for some reason Username includes the hostname on windows
}
2021-03-28 22:58:58 +00:00
args := []string{
2021-03-28 22:58:58 +00:00
"d", cwd,
"D", filepath.Base(cwd),
2021-03-28 22:58:58 +00:00
"h", host,
"u", username,
}
2021-03-28 22:58:58 +00:00
for i, v := range args {
if i % 2 == 0 {
args[i] = "%" + v
}
}
2021-03-28 22:58:58 +00:00
r := strings.NewReplacer(args...)
nprompt := r.Replace(prompt)
2021-03-28 22:58:58 +00:00
return nprompt
2021-03-26 05:06:14 +00:00
}
func handleHistory(cmd string) {
lr.AddHistory(cmd)
// TODO: load history again (history shared between sessions like this ye)
}