diff options
author | jao <jao@gnu.org> | 2018-11-26 03:31:53 +0000 |
---|---|---|
committer | jao <jao@gnu.org> | 2018-11-26 03:31:53 +0000 |
commit | 5bb1fec0ba93e56691f043323dddf3bb303b5636 (patch) | |
tree | a90a4d68141ad38a8d272b48c1688c7f9c232018 /src/Xmobar | |
parent | 7c270f9d0810db118f4ba3b8c47bc70b8be78d1e (diff) | |
download | xmobar-5bb1fec0ba93e56691f043323dddf3bb303b5636.tar.gz xmobar-5bb1fec0ba93e56691f043323dddf3bb303b5636.tar.bz2 |
Utilities for dealing with config and data directories
Diffstat (limited to 'src/Xmobar')
-rw-r--r-- | src/Xmobar/App/Config.hs | 96 |
1 files changed, 84 insertions, 12 deletions
diff --git a/src/Xmobar/App/Config.hs b/src/Xmobar/App/Config.hs index 86bc9cb..7b1171f 100644 --- a/src/Xmobar/App/Config.hs +++ b/src/Xmobar/App/Config.hs @@ -10,16 +10,21 @@ -- Created: Sun Nov 25, 2018 22:26 -- -- --- Default values for Xmobar configurations +-- Default values for Xmobar configurations and functions to access +-- configuration files and directories. -- ------------------------------------------------------------------------------ -module Xmobar.App.Config (defaultConfig, getXdgConfigFile) where +module Xmobar.App.Config (defaultConfig, + xmobarConfigDir, + xmobarDataDir, + xmobarConfigFile) where import System.Environment -import System.Directory (getHomeDirectory) +import System.Directory import System.FilePath ((</>)) +import System.Posix.Files (fileExist) import Xmobar.Plugins.Date import Xmobar.Plugins.StdinReader @@ -58,14 +63,81 @@ defaultConfig = "<fc=#00FF00>%uname%</fc> * <fc=#FF0000>%theDate%</fc>" } -xdgConfigDir :: IO String -xdgConfigDir = do env <- getEnvironment - case lookup "XDG_CONFIG_HOME" env of - Just val -> return val - Nothing -> fmap (</> ".config") getHomeDirectory +-- | Return the path to the xmobar configuration directory. This +-- directory is where user configuration files are stored (e.g, the +-- xmobar.hs file). You may also create a @lib@ subdirectory in the +-- configuration directory and the default recompile command will add +-- it to the GHC include path. +-- +-- Several directories are considered. In order of +-- preference: +-- +-- 1. The directory specified in the @XMOBAR_CONFIG_DIR@ environment variable. +-- 2. The @~\/.xmobar@ directory. +-- 3. The @XDG_CONFIG_HOME/xmobar@ directory. +-- +-- The first directory that exists will be used. If none of the +-- directories exist then (1) will be used if it is set, otherwise (2) +-- will be used. Either way, a directory will be created if necessary. +xmobarConfigDir :: IO String +xmobarConfigDir = + findFirstDirWithEnv "XMOBAR_CONFIG_DIR" + [ getAppUserDataDirectory "xmobar" + , getXdgDirectory XdgConfig "xmobar" + ] + +-- | Return the path to the xmobar data directory. This directory is +-- used by Xmobar to store data files such as the run-time state file +-- and the configuration binary generated by GHC. +-- +-- Several directories are considered. In order of preference: +-- +-- 1. The directory specified in the @XMOBAR_DATA_DIR@ environment variable. +-- 2. The @~\/.xmobar@ directory. +-- 3. The @XDG_DATA_HOME/xmobar@ directory. +-- +-- The first directory that exists will be used. If none of the +-- directories exist then (1) will be used if it is set, otherwise (2) +-- will be used. Either way, a directory will be created if +-- necessary. +xmobarDataDir :: IO String +xmobarDataDir = + findFirstDirWithEnv "XMOBAR_DATA_DIR" + [ getAppUserDataDirectory "xmobar" + , getXdgDirectory XdgData "xmobar" + ] + +-- | Helper function that will find the first existing directory and +-- return its path. If none of the directories can be found, create +-- and return the first from the list. If the list is empty this +-- function returns the historical @~\/.xmobar@ directory. +findFirstDirOf :: [IO FilePath] -> IO FilePath +findFirstDirOf [] = findFirstDirOf [getAppUserDataDirectory "xmobar"] +findFirstDirOf possibles = do + found <- go possibles + case found of + Just path -> return path + Nothing -> do + primary <- head possibles + createDirectoryIfMissing True primary + return primary + where + go [] = return Nothing + go (x:xs) = do + exists <- x >>= doesDirectoryExist + if exists then x >>= return . Just else go xs -xmobarConfigDir :: IO FilePath -xmobarConfigDir = fmap (</> "xmobar") xdgConfigDir +-- | Simple wrapper around @findFirstDirOf@ that allows the primary +-- path to be specified by an environment variable. +findFirstDirWithEnv :: String -> [IO FilePath] -> IO FilePath +findFirstDirWithEnv envName paths = do + envPath' <- lookupEnv envName + case envPath' of + Nothing -> findFirstDirOf paths + Just envPath -> findFirstDirOf (return envPath:paths) -getXdgConfigFile :: IO FilePath -getXdgConfigFile = fmap (</> "xmobarrc") xmobarConfigDir +xmobarConfigFile :: IO FilePath +xmobarConfigFile = do + f <- fmap (</> "xmobarrc") xmobarConfigDir + fe <- fileExist f + if fe then return f else fmap (</> ".xmobarrc") getHomeDirectory |