summaryrefslogtreecommitdiffhomepage
path: root/app/Main.hs
blob: 3c17447a6223365682606d2caf15ba184b0ccf81 (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
52
53
54
55
56
57
58
59
{-# LANGUAGE FlexibleContexts #-}
-----------------------------------------------------------------------------
-- |
-- Module      :  Xmobar.Main
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
--
-- Maintainer  :  Jose A. Ortega Ruiz <jao@gnu.org>
-- Stability   :  unstable
-- Portability :  unportable
--
-- The main module of Xmobar, a text based status bar
--
-----------------------------------------------------------------------------

module Main (main) where

import Data.List (intercalate)

import System.Environment (getArgs)
import Control.Monad (unless)
import System.Posix.Files (fileExist)

import Xmobar
import Xmobar.App.Main
import Xmobar.App.Opts

-- $main

-- | The main entry point
main :: IO ()
main = do
  (o,file) <- getArgs >>= getOpts
  (c,defaultings) <- case file of
                       [cfgfile] -> config cfgfile usage
                       _ -> defConfig usage
  unless (null defaultings || notElem Debug o) $ putStrLn $
    "Fields missing from config defaulted: " ++ intercalate "," defaultings
  doOpts c o >>= xmobar

-- | 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,[])

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