From a75100d15def4c8db9edb7c938a8da21498a7f12 Mon Sep 17 00:00:00 2001 From: sammy <38820196+TorchedSammy@users.noreply.github.com> Date: Sat, 1 May 2021 14:51:57 -0400 Subject: [PATCH] feat: add _user variable this will probably change to a better global var in the future, but it's `_user` now. this provides a simple, guaranteed way of getting the current user's name $USER (in?)correctly shows the previous user when `su` is used, _user will always be root in this case --- .hilbishrc.lua | 2 +- lua.go | 1 + main.go | 14 +++++++++----- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/.hilbishrc.lua b/.hilbishrc.lua index b51db8f..4706937 100644 --- a/.hilbishrc.lua +++ b/.hilbishrc.lua @@ -9,7 +9,7 @@ function doPrompt(fail) end print(ansikit.format('Welcome to {magenta}Hilbish{reset}, {cyan}' .. -os.getenv 'USER' .. '{reset}.\n' .. +_user .. '{reset}.\n' .. 'The nice lil shell for {blue}Lua{reset} fanatics!\n')) doPrompt() diff --git a/lua.go b/lua.go index 5b6eb79..bb0a9f8 100644 --- a/lua.go +++ b/lua.go @@ -23,6 +23,7 @@ func LuaInit() { l.OpenLibs() l.SetGlobal("_ver", lua.LString(version)) + l.SetGlobal("_user", lua.LString(curuser.Username)) l.SetGlobal("prompt", l.NewFunction(hshprompt)) l.SetGlobal("multiprompt", l.NewFunction(hshmlprompt)) diff --git a/main.go b/main.go index 7c27d59..b4926ac 100644 --- a/main.go +++ b/main.go @@ -4,9 +4,10 @@ import ( "fmt" "io" "os" + "strings" "os/signal" "os/user" - "strings" + "path/filepath" "hilbish/golibs/bait" @@ -27,15 +28,18 @@ var ( commands = map[string]bool{} aliases = map[string]string{} - hooks bait.Bait homedir string + curuser *user.User + running bool // Is a command currently running + hooks bait.Bait interactive bool login bool // Are we the login shell? ) func main() { homedir, _ = os.UserHomeDir() + curuser, _ = user.Current() defaultconfpath := homedir + "/.hilbishrc.lua" // parser := argparse.NewParser("hilbish", "A shell for lua and flower lovers") @@ -169,16 +173,16 @@ func ContinuePrompt(prev string) (string, error) { // This semi cursed function formats our prompt (obviously) func fmtPrompt() string { - user, _ := user.Current() host, _ := os.Hostname() cwd, _ := os.Getwd() - cwd = strings.Replace(cwd, user.HomeDir, "~", 1) + cwd = strings.Replace(cwd, curuser.HomeDir, "~", 1) args := []string{ "d", cwd, + "D", filepath.Base(cwd), "h", host, - "u", user.Username, + "u", curuser.Username, } for i, v := range args {