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 /app/Main.hs | |
parent | 7c270f9d0810db118f4ba3b8c47bc70b8be78d1e (diff) | |
download | xmobar-5bb1fec0ba93e56691f043323dddf3bb303b5636.tar.gz xmobar-5bb1fec0ba93e56691f043323dddf3bb303b5636.tar.bz2 |
Utilities for dealing with config and data directories
Diffstat (limited to 'app/Main.hs')
-rw-r--r-- | app/Main.hs | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/app/Main.hs b/app/Main.hs index b5d3993..51bfe8b 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -23,11 +23,10 @@ import System.Exit import System.Environment (getArgs) import Control.Monad (unless) import Text.Read (readMaybe) +import System.Posix.Files (fileExist) import Xmobar - import Paths_xmobar (version) -import Configuration as C (readConfig, readDefaultConfig) -- $main @@ -36,12 +35,32 @@ main :: IO () main = do (o,file) <- getArgs >>= getOpts (c,defaultings) <- case file of - [cfgfile] -> C.readConfig cfgfile usage - _ -> C.readDefaultConfig usage + [cfgfile] -> config cfgfile usage + _ -> defConfig usage unless (null defaultings || (not $ any (== Debug) o)) $ putStrLn $ "Fields missing from config defaulted: " ++ intercalate "," defaultings doOpts c o >>= xmobar +config :: FilePath -> String -> IO (Config,[String]) +config f msg = do + let err m = error $ f ++ ": " ++ m + file <- fileExist f + r <- if file + then readConfig defaultConfig f + else err $ "file not found" ++ "\n" ++ msg + case r of + Left e -> err (show e) + Right res -> return res + +-- | Read default configuration file or load the default config +defConfig :: String -> IO (Config,[String]) +defConfig msg = do + xdgConfigFile <- xmobarConfigFile + xdgConfigFileExists <- fileExist xdgConfigFile + if xdgConfigFileExists + then config xdgConfigFile msg + else return (defaultConfig,[]) + data Opts = Help | Debug | Version |