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
pull/46/head
sammy 2021-05-01 14:51:57 -04:00
parent 60b1b50bb3
commit a75100d15d
No known key found for this signature in database
GPG Key ID: 50EE40A2809851F5
3 changed files with 11 additions and 6 deletions

View File

@ -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()

1
lua.go
View File

@ -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))

14
main.go
View File

@ -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 {