blob: 340124ac0b82a264030956021e3bd30082fb31d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
{-# LANGUAGE FlexibleContexts, CPP #-}
------------------------------------------------------------------------------
-- |
-- Module: Configuration
-- Copyright: (c) 2018 Jose Antonio Ortega Ruiz
-- License: BSD3-style (see LICENSE)
--
-- Maintainer: jao@gnu.org
-- Stability: unstable
-- Portability: portable
-- Created: Wed Nov 21, 2018 23:13
--
--
-- Parsing configuration files
--
------------------------------------------------------------------------------
module Configuration (readConfig, readDefaultConfig) where
import Control.Monad.IO.Class (liftIO)
import System.Environment
import System.Posix.Files (fileExist)
import qualified Xmobar as X
-- | Reads the configuration files or quits with an error
readConfig :: FilePath -> String -> IO (X.Config,[String])
readConfig f usage = do
let err m = error $ f ++ ": " ++ m ++ "\n" ++ usage
file <- liftIO $ fileExist f
r <- if file then X.readConfig X.defaultConfig f else err "file not found"
case r of
Left e -> err (show e)
Right res -> return res
-- | Read default configuration file or load the default config
readDefaultConfig :: String -> IO (X.Config,[String])
readDefaultConfig usage = do
xdgConfigFile <- X.getXdgConfigFile
xdgConfigFileExists <- liftIO $ fileExist xdgConfigFile
home <- liftIO $ getEnv "HOME"
let defaultConfigFile = home ++ "/.xmobarrc"
defaultConfigFileExists <- liftIO $ fileExist defaultConfigFile
if xdgConfigFileExists
then readConfig xdgConfigFile usage
else if defaultConfigFileExists
then readConfig defaultConfigFile usage
else return (X.defaultConfig,[])
|