From 822b2876e9168e6ab53b67999e45291469c4258b Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Sun, 20 Jun 2021 21:48:07 -0400 Subject: [PATCH 1/5] fix: reuse sh runner this makes it so env variables persist, and some other stuff builtin cd actually works now :] --- main.go | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ shell.go | 50 +------------------------------------------------- 2 files changed, 56 insertions(+), 49 deletions(-) diff --git a/main.go b/main.go index 2056e3f..3239855 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "bufio" + "context" "fmt" "io" "os" @@ -9,12 +10,14 @@ import ( "os/signal" "os/user" "path/filepath" + "time" "hilbish/golibs/bait" "github.com/pborman/getopt" "github.com/yuin/gopher-lua" "layeh.com/gopher-luar" + "mvdan.cc/sh/v3/interp" "golang.org/x/term" ) @@ -30,6 +33,8 @@ var ( hooks bait.Bait defaultConfPath string + + runner *interp.Runner ) func main() { @@ -146,6 +151,56 @@ func main() { os.Exit(0) } + // Execute handler for sh runner + exechandle := func(ctx context.Context, args []string) error { + hc := interp.HandlerCtx(ctx) + _, argstring := splitInput(strings.Join(args, " ")) + + // If alias was found, use command alias + if aliases[args[0]] != "" { + alias := aliases[args[0]] + argstring = alias + strings.TrimPrefix(argstring, args[0]) + cmdArgs, _ := splitInput(argstring) + args = cmdArgs + } + + // If command is defined in Lua then run it + if commands[args[0]] != nil { + err := l.CallByParam(lua.P{ + Fn: commands[args[0]], + NRet: 1, + Protect: true, + }, luar.New(l, args[1:])) + luaexitcode := l.Get(-1) + var exitcode uint8 = 0 + + l.Pop(1) + + if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok { + exitcode = uint8(code) + } + + if err != nil { + fmt.Fprintln(os.Stderr, + "Error in command:\n\n" + err.Error()) + } + hooks.Em.Emit("command.exit", exitcode) + return interp.NewExitStatus(exitcode) + } + + if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil { + hooks.Em.Emit("command.not-found", args[0]) + return interp.NewExitStatus(127) + } + + return interp.DefaultExecHandler(2 * time.Second)(ctx, args) + } + // Setup sh runner outside of input label + runner, _ = interp.New( + interp.StdIO(os.Stdin, os.Stdout, os.Stderr), + interp.ExecHandler(exechandle), + ) + input: for interactive { running = false diff --git a/shell.go b/shell.go index d4bb97a..09e636a 100644 --- a/shell.go +++ b/shell.go @@ -5,7 +5,6 @@ import ( "fmt" "os" "strings" - "time" // "github.com/bobappleyard/readline" "github.com/yuin/gopher-lua" @@ -114,54 +113,7 @@ func execCommand(cmd string) error { if err != nil { return err } - - exechandle := func(ctx context.Context, args []string) error { - hc := interp.HandlerCtx(ctx) - _, argstring := splitInput(strings.Join(args, " ")) - - // If alias was found, use command alias - if aliases[args[0]] != "" { - alias := aliases[args[0]] - argstring = alias + strings.TrimPrefix(argstring, args[0]) - cmdArgs, _ := splitInput(argstring) - args = cmdArgs - } - - // If command is defined in Lua then run it - if commands[args[0]] != nil { - err := l.CallByParam(lua.P{ - Fn: commands[args[0]], - NRet: 1, - Protect: true, - }, luar.New(l, args[1:])) - luaexitcode := l.Get(-1) - var exitcode uint8 = 0 - - l.Pop(1) - - if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok { - exitcode = uint8(code) - } - - if err != nil { - fmt.Fprintln(os.Stderr, - "Error in command:\n\n" + err.Error()) - } - hooks.Em.Emit("command.exit", exitcode) - return interp.NewExitStatus(exitcode) - } - - if _, err := interp.LookPathDir(hc.Dir, hc.Env, args[0]); err != nil { - hooks.Em.Emit("command.not-found", args[0]) - return interp.NewExitStatus(127) - } - - return interp.DefaultExecHandler(2 * time.Second)(ctx, args) - } - runner, _ := interp.New( - interp.StdIO(os.Stdin, os.Stdout, os.Stderr), - interp.ExecHandler(exechandle), - ) + err = runner.Run(context.TODO(), file) return err From 4caeb7ec91f5886a4e70b928b25036ccc0ec2ffd Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Thu, 8 Jul 2021 03:44:11 -0700 Subject: [PATCH 2/5] feat: replace ~ in preloadPath and sampleConfPath with homedir --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 3239855..828e904 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,8 @@ var ( func main() { homedir, _ = os.UserHomeDir() curuser, _ = user.Current() + preloadPath = strings.Replace(preloadPath, "~", homedir, 1) + sampleConfPath = strings.Replace(sampleConfPath, "~", homedir, 1) if defaultConfDir == "" { // we'll add *our* default if its empty (wont be if its changed comptime) From a3cc8160d3741c2ee2b9fe9dfa6c1df26cc1f451 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Thu, 8 Jul 2021 03:44:57 -0700 Subject: [PATCH 3/5] fix: trim off hostname in %u verb for windows --- hilbish.go | 4 +++- main.go | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/hilbish.go b/hilbish.go index 003d0b4..8708cfe 100644 --- a/hilbish.go +++ b/hilbish.go @@ -5,6 +5,7 @@ package main import ( "os" + "strings" "github.com/pborman/getopt" "github.com/yuin/gopher-lua" @@ -21,9 +22,10 @@ func HilbishLoader(L *lua.LState) int { mod := L.SetFuncs(L.NewTable(), exports) host, _ := os.Hostname() + username := strings.Split(curuser.Username, "\\")[1] // for some reason Username includes the hostname on windows L.SetField(mod, "ver", lua.LString(version)) - L.SetField(mod, "user", lua.LString(curuser.Username)) + L.SetField(mod, "user", lua.LString(username)) L.SetField(mod, "host", lua.LString(host)) L.SetField(mod, "home", lua.LString(homedir)) diff --git a/main.go b/main.go index 828e904..ab0c515 100644 --- a/main.go +++ b/main.go @@ -265,12 +265,13 @@ func fmtPrompt() string { cwd, _ := os.Getwd() cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1) + username := strings.Split(curuser.Username, "\\")[1] // for some reason Username includes the hostname on windows args := []string{ "d", cwd, "D", filepath.Base(cwd), "h", host, - "u", curuser.Username, + "u", username, } for i, v := range args { From 5d78a0c574fabd2a02618fb7639b91662ca49215 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Thu, 8 Jul 2021 17:49:51 -0700 Subject: [PATCH 4/5] feat: add compile vars for windows --- vars.go | 11 ----------- vars_unix.go | 18 ++++++++++++++++++ vars_windows.go | 12 ++++++++++++ 3 files changed, 30 insertions(+), 11 deletions(-) create mode 100644 vars_unix.go create mode 100644 vars_windows.go diff --git a/vars.go b/vars.go index 3aaf4ed..ad182f3 100644 --- a/vars.go +++ b/vars.go @@ -3,18 +3,7 @@ package main // String vars that are free to be changed at compile time var ( version = "v0.5.1" - requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua' - .. ';/usr/share/hilbish/libs/?/init.lua;' - .. ';/usr/share/hilbish/libs/?/?.lua;' - .. hilbish.home .. '/.local/share/hilbish/libs/?/init.lua;' - .. hilbish.home .. '/.local/share/hilbish/libs/?/?.lua;' - .. hilbish.home .. '/.local/share/hilbish/libs/?.lua' - .. hilbish.home .. '/.config/hilbish/?/init.lua' - .. hilbish.home .. '/.config/hilbish/?/?.lua' - .. hilbish.home .. '/.config/hilbish/?.lua'` - preloadPath = "/usr/share/hilbish/preload.lua" defaultConfDir = "" // ~ will be substituted for home, path for user's default config - sampleConfPath = "/usr/share/hilbish/.hilbishrc.lua" // Path to default/sample config prompt string // Prompt will always get changed anyway multilinePrompt = "> " diff --git a/vars_unix.go b/vars_unix.go new file mode 100644 index 0000000..4b87b7b --- /dev/null +++ b/vars_unix.go @@ -0,0 +1,18 @@ +// +build unix + +package main + +// String vars that are free to be changed at compile time +var ( + requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua' + .. ';/usr/share/hilbish/libs/?/init.lua;' + .. ';/usr/share/hilbish/libs/?/?.lua;' + .. hilbish.home .. '/.local/share/hilbish/libs/?/init.lua;' + .. hilbish.home .. '/.local/share/hilbish/libs/?/?.lua;' + .. hilbish.home .. '/.local/share/hilbish/libs/?.lua' + .. hilbish.home .. '/.config/hilbish/?/init.lua' + .. hilbish.home .. '/.config/hilbish/?/?.lua' + .. hilbish.home .. '/.config/hilbish/?.lua'` + preloadPath = "/usr/share/hilbish/preload.lua" + sampleConfPath = "/usr/share/hilbish/.hilbishrc.lua" // Path to default/sample config +) diff --git a/vars_windows.go b/vars_windows.go new file mode 100644 index 0000000..d058de5 --- /dev/null +++ b/vars_windows.go @@ -0,0 +1,12 @@ +// +build windows + +package main + +// String vars that are free to be changed at compile time +var ( + requirePaths = `';./libs/?/init.lua;./?/init.lua;./?/?.lua' + .. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;' + .. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'` + preloadPath = "~\\Appdata\\Roaming\\Hilbish\\preload.lua" // ~ and \ gonna cry? + sampleConfPath = "~\\Appdata\\Roaming\\Hilbish\\hilbishrc.lua" // Path to default/sample config +) From 196526cc6a261a14ff2baf14907583a49622ef31 Mon Sep 17 00:00:00 2001 From: sammyette <38820196+TorchedSammy@users.noreply.github.com> Date: Thu, 8 Jul 2021 17:55:19 -0700 Subject: [PATCH 5/5] fix: rename unix vars to linux --- vars_unix.go => vars_linux.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename vars_unix.go => vars_linux.go (97%) diff --git a/vars_unix.go b/vars_linux.go similarity index 97% rename from vars_unix.go rename to vars_linux.go index 4b87b7b..039a1d2 100644 --- a/vars_unix.go +++ b/vars_linux.go @@ -1,4 +1,4 @@ -// +build unix +// +build linux package main