From 5a34f6f0473a1bfc1f3399d040822d26eb715d71 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Sat, 28 Nov 2015 19:33:33 +0100 Subject: [PATCH] Add various functions for getting directories This adds functions for getting directories according to the XDG base dir specification. This might be useful in general. This also adds a function for getting the cache dir, because I like an explicit function call more than something which "switches" based on a string argument. Better error messages if you mis-type something. :-) Finally, this adds a function for getting the directory containing the rc.lua file. Sadly, awful.util.getdir("config") did not actually do that. See #218. Fixes: https://github.com/awesomeWM/awesome/issues/218 Signed-off-by: Uli Schlachter --- lib/awful/util.lua | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/lib/awful/util.lua b/lib/awful/util.lua index 665953f15..f114e5482 100644 --- a/lib/awful/util.lua +++ b/lib/awful/util.lua @@ -133,6 +133,31 @@ function util.restart() capi.awesome.restart() end +--- Get the config home according to the XDG basedir specification. +-- @return the config home (XDG_CONFIG_HOME) with a slash at the end. +function util.get_xdg_config_home() + return (os.getenv("XDG_CONFIG_HOME") or os.getenv("HOME") .. "/.config") .. "/" +end + +--- Get the cache home according to the XDG basedir specification. +-- @return the cache home (XDG_CACHE_HOME) with a slash at the end. +function util.get_xdg_cache_home() + return (os.getenv("XDG_CACHE_HOME") or os.getenv("HOME") .. "/.cache") .. "/" +end + +--- Get the path to the user's config dir. +-- This is the directory containing the configuration file ("rc.lua"). +-- @return A string with the requested path with a slash at the end. +function util.get_configuration_dir() + return capi.awesome.conffile:match(".*/") or "./" +end + +--- Get the path to a directory that should be used for caching data. +-- @return A string with the requested path with a slash at the end. +function util.get_cache_dir() + return util.get_xdg_cache_home() .. "awesome/" +end + --- Get the user's config or cache dir. -- It first checks XDG_CONFIG_HOME / XDG_CACHE_HOME, but then goes with the -- default paths. @@ -140,17 +165,11 @@ end -- @return A string containing the requested path. function util.getdir(d) if d == "config" then - local dir = os.getenv("XDG_CONFIG_HOME") - if dir then - return dir .. "/awesome" - end - return os.getenv("HOME") .. "/.config/awesome" + -- No idea why this is what is returned, I recommend everyone to use + -- get_configuration_dir() instead + return util.get_xdg_config_home() .. "awesome/" elseif d == "cache" then - local dir = os.getenv("XDG_CACHE_HOME") - if dir then - return dir .. "/awesome" - end - return os.getenv("HOME").."/.cache/awesome" + return util.get_cache_dir() end end