summaryrefslogtreecommitdiffhomepage
path: root/Main.hs
blob: 55e548732fd2c7a3843a0c2c220bc1b15dfa50dc (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
-----------------------------------------------------------------------------
-- |
-- Module      :  Xmobar.Main
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
-- 
-- Maintainer  :  Andrea Rossato <andrea.rossato@unibz.it>
-- Stability   :  unstable
-- Portability :  unportable
--
-- The main module of Xmobar, a text based status bar 
--
-----------------------------------------------------------------------------

module Main ( -- * Main Stuff
              -- $main
              main
            , readConfig
            , readDefaultConfig
            ) where

import Xmobar
import Parsers
import Config
import System.Environment
import System.IO.Error

-- $main
 
-- | The main entry point
main :: IO ()
main = 
    do args <- getArgs
       config <- case args of
           [cfgfile] -> readConfig cfgfile
           _         -> readDefaultConfig
       cl <- parseTemplate config (template config)
       var <- execCommands config cl
       (d,w) <- createWin config
       eventLoop config var d w
       return ()

-- | Reads the configuration files or quits with an error
readConfig :: FilePath -> IO Config
readConfig f = 
    do s <- readFile f
       case reads s of
         [(config,_)] -> return config
         [] -> error ("Corrupt config file: " ++ f)
         _ -> error ("Some problem occured. Aborting...")

-- | Read default configuration or quit with an error
readDefaultConfig :: IO Config
readDefaultConfig = 
    do home <- getEnv "HOME"
       let path = home ++ "/.xmobarrc"
       catch (readConfig path)
             (\e -> if isUserError e then ioError e else return defaultConfig)