From 2e4bea829210fbab97760f57ea54d5b2aa4ae7f7 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Mon, 13 Dec 2021 20:13:17 -0400 Subject: [PATCH] refactor!: replace hilbish.xdg with hilbish.userDir "xdg" is basically a linux specific thing and it acted linux specific also. so replace that with a more portable and general "userDir" table config is for the user's config directory and also defaults to xdg vars on linux. data is a directory to store data, mostly for scripts. on other oses beside linux, it is just the config directory for now --- hilbish.go | 32 +++++++++++++++++++++++++------- vars_linux.go | 12 ++++++------ vars_windows.go | 7 ++++--- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/hilbish.go b/hilbish.go index 60ebbd3..0af4c35 100644 --- a/hilbish.go +++ b/hilbish.go @@ -5,6 +5,7 @@ package main import ( "os" + "path/filepath" "runtime" "strings" @@ -40,12 +41,22 @@ func hilbishLoader(L *lua.LState) int { 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") - xdg := L.NewTable() - util.SetField(L, xdg, "config", lua.LString(confDir), "XDG config directory") - util.SetField(L, xdg, "data", lua.LString(getenv("XDG_DATA_HOME", curuser.HomeDir + "/.local/share")), "XDG data directory") - L.SetField(mod, "xdg", xdg) + 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: + userDataDir = filepath.Join(userConfigDir, "data") + } + + 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) - util.Document(L, xdg, "Variables for the XDG base directory spec.") util.Document(L, mod, "Hilbish's core API, containing submodules and functions which relate to the shell itself.") L.Push(mod) @@ -53,7 +64,7 @@ func hilbishLoader(L *lua.LState) int { } // run(cmd) -// Runs `cmd` in Hilbish's sh interpreter +// Runs `cmd` in Hilbish's sh interpreter. func hlrun(L *lua.LState) int { var exitcode uint8 cmd := L.CheckString(1) @@ -74,7 +85,14 @@ func hlrun(L *lua.LState) int { func hlflag(L *lua.LState) int { flagchar := L.CheckString(1) - L.Push(lua.LBool(getopt.Lookup([]rune(flagchar)[0]).Seen())) + flag := getopt.Lookup([]rune(flagchar)[0]) + if flag == nil { + L.Push(lua.LNil) + return 1 + } + + passed := flag.Seen() + L.Push(lua.LBool(passed)) return 1 } diff --git a/vars_linux.go b/vars_linux.go index 718a97d..dbd2c95 100644 --- a/vars_linux.go +++ b/vars_linux.go @@ -8,12 +8,12 @@ var ( .. hilbish.dataDir .. '/libs/?/init.lua;' .. hilbish.dataDir .. '/libs/?/?.lua;'` + linuxUserPaths linuxUserPaths = ` - .. hilbish.xdg.data .. '/hilbish/libs/?/init.lua;' - .. hilbish.xdg.data .. '/hilbish/libs/?/?.lua;' - .. hilbish.xdg.data .. '/hilbish/libs/?.lua;' - .. hilbish.xdg.config .. '/hilbish/?/init.lua;' - .. hilbish.xdg.config .. '/hilbish/?/?.lua;' - .. hilbish.xdg.config .. '/hilbish/?.lua'` + .. hilbish.userDir.data .. '/hilbish/libs/?/init.lua;' + .. hilbish.userDir.data .. '/hilbish/libs/?/?.lua;' + .. hilbish.userDir.data .. '/hilbish/libs/?.lua;' + .. hilbish.userDir.config .. '/hilbish/?/init.lua;' + .. hilbish.userDir.config .. '/hilbish/?/?.lua;' + .. hilbish.userDir.config .. '/hilbish/?.lua'` dataDir = "/usr/share/hilbish" preloadPath = dataDir + "/preload.lua" sampleConfPath = dataDir + "/.hilbishrc.lua" // Path to default/sample config diff --git a/vars_windows.go b/vars_windows.go index a32f652..531729f 100644 --- a/vars_windows.go +++ b/vars_windows.go @@ -4,9 +4,10 @@ package main // String vars that are free to be changed at compile time var ( - requirePaths = commonRequirePaths + ` - .. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\init.lua;' - .. hilbish.home .. '\\Appdata\\Roaming\\Hilbish\\libs\\?\\?.lua;'` + requirePaths = commonRequirePaths + `.. ';' + .. hilbish.user.config .. '\\Hilbish\\libs\\?\\init.lua;' + .. hilbish.user.config .. '\\Hilbish\\libs\\?\\?.lua;' + .. hilbish.user.config .. '\\Hilbish\\libs\\?.lua;'` dataDir = "~\\Appdata\\Roaming\\Hilbish" // ~ and \ gonna cry? preloadPath = dataDir + "\\preload.lua" sampleConfPath = dataDir + "\\hilbishrc.lua" // Path to default/sample config