From f18c780e533ea721e445f42dcefafac92758dd4f Mon Sep 17 00:00:00 2001 From: L3af Date: Thu, 7 Oct 2021 06:01:33 +1300 Subject: [PATCH] Respect XDG directories --- hilbish.go | 9 +++++++++ main.go | 8 +++++++- vars_linux.go | 12 ++++++------ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/hilbish.go b/hilbish.go index 773affb..2b141da 100644 --- a/hilbish.go +++ b/hilbish.go @@ -33,6 +33,8 @@ func HilbishLoader(L *lua.LState) int { L.SetField(mod, "user", lua.LString(username)) L.SetField(mod, "host", lua.LString(host)) L.SetField(mod, "home", lua.LString(homedir)) + L.SetField(mod, "xdgConfig", lua.LString(getenv("XDG_CONFIG_HOME", homedir + "/.config"))) + L.SetField(mod, "xdgData", lua.LString(getenv("XDG_DATA_HOME", homedir + "/.local/share/"))) L.Push(mod) @@ -71,3 +73,10 @@ func cwd(L *lua.LState) int { return 1 } +func getenv(key, fallback string) string { + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value +} diff --git a/main.go b/main.go index e60a0bf..492f7d1 100644 --- a/main.go +++ b/main.go @@ -27,6 +27,7 @@ var ( aliases = map[string]string{} homedir string + confDir string curuser *user.User hooks bait.Bait @@ -35,13 +36,18 @@ var ( func main() { homedir, _ = os.UserHomeDir() + confDir = getenv("XDG_CONFIG_HOME", homedir + "/.config") 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) - defaultConfPath = filepath.Join(homedir, "/.hilbishrc.lua") + if _, err := os.Stat(filepath.Join(confDir, "hilbish", "hilbishrc.lua")); os.IsNotExist(err) { + defaultConfPath = filepath.Join(homedir, "/.hilbishrc.lua") + } else { + defaultConfPath = filepath.Join(confDir, "hilbish", "hilbishrc.lua") + } } else { // else do ~ substitution defaultConfPath = filepath.Join(strings.Replace(defaultConfDir, "~", homedir, 1), ".hilbishrc.lua") diff --git a/vars_linux.go b/vars_linux.go index 5125622..34f5527 100644 --- a/vars_linux.go +++ b/vars_linux.go @@ -7,12 +7,12 @@ var ( requirePaths = `';./libs/?/?.lua;./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'` + .. hilbish.xdgData .. '/hilbish/libs/?/init.lua;' + .. hilbish.xdgData .. '/hilbish/libs/?/?.lua;' + .. hilbish.xdgData .. '/hilbish/libs/?.lua' + .. hilbish.xdgConfig .. '/?/init.lua' + .. hilbish.xdgConfig .. '/?/?.lua' + .. hilbish.xdgConfig .. '/?.lua'` preloadPath = "/usr/share/hilbish/preload.lua" sampleConfPath = "/usr/share/hilbish/.hilbishrc.lua" // Path to default/sample config )