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 <psychon@znc.in>
This commit is contained in:
Uli Schlachter 2015-11-28 19:33:33 +01:00
parent e39504a30f
commit 5a34f6f047
1 changed files with 29 additions and 10 deletions

View File

@ -133,6 +133,31 @@ function util.restart()
capi.awesome.restart() capi.awesome.restart()
end 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. --- Get the user's config or cache dir.
-- It first checks XDG_CONFIG_HOME / XDG_CACHE_HOME, but then goes with the -- It first checks XDG_CONFIG_HOME / XDG_CACHE_HOME, but then goes with the
-- default paths. -- default paths.
@ -140,17 +165,11 @@ end
-- @return A string containing the requested path. -- @return A string containing the requested path.
function util.getdir(d) function util.getdir(d)
if d == "config" then if d == "config" then
local dir = os.getenv("XDG_CONFIG_HOME") -- No idea why this is what is returned, I recommend everyone to use
if dir then -- get_configuration_dir() instead
return dir .. "/awesome" return util.get_xdg_config_home() .. "awesome/"
end
return os.getenv("HOME") .. "/.config/awesome"
elseif d == "cache" then elseif d == "cache" then
local dir = os.getenv("XDG_CACHE_HOME") return util.get_cache_dir()
if dir then
return dir .. "/awesome"
end
return os.getenv("HOME").."/.cache/awesome"
end end
end end