summaryrefslogtreecommitdiffhomepage
path: root/Commands.hs
blob: 8d39d305a3ee4b2c287711b7c4db33dfa0dba4b8 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
-----------------------------------------------------------------------------
-- |
-- Module      :  XMobar.Commands
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
-- 
-- Maintainer  :  Andrea Rossato <andrea.rossato@unibz.it>
-- Stability   :  unstable
-- Portability :  unportable
--
-- A Command datatype for XMobar status bar for the Xmonad Window Manager 
--
-----------------------------------------------------------------------------

module Commands where

import System.Process
import System.Exit
import System.IO (hClose, hGetLine)

import Monitors.Common ( runM )
import Monitors.Weather
import Monitors.Net
import Monitors.Mem
import Monitors.Swap
import Monitors.Cpu
import Monitors.Batt

data Command = Exec Program Args Alias 
             | Weather Station Args 
             | Network Interface Args
             | Memory Args
             | Swap Args
             | Cpu Args
             | Battery Args
               deriving (Read,Eq)

type Args = [String]
type Program = String
type Alias = String
type Station = String
type Interface = String

instance Show Command where
    show (Weather s _) = s
    show (Network i _) = i
    show (Memory _) = "memory"
    show (Swap _) = "swap"
    show (Cpu _) = "cpu"
    show (Battery _) = "battery"
    show (Exec p _ a) | p /= "" = if  a == "" then p else a
                      | otherwise = ""
class Exec e where
    run :: e -> IO String

instance Exec Command where
    run (Weather s a) = do let af = return "No station ID specified"
                           runM (a ++ [s]) weatherConfig af runWeather 
    run (Network i a) = do let f = return "No device specified"
                           runM (a ++ [i]) netConfig f runNet
    run (Memory args) = do let af = runMem []
                           runM args memConfig af runMem
    run (Swap args) = do let af = runSwap []
                         runM args swapConfig af runSwap
    run (Cpu args) = do let af = runCpu []
                        runM args cpuConfig af runCpu
    run (Battery args) = do let af = runBatt []
                            runM args battConfig af runBatt
    run (Exec prog args _) = do (i,o,e,p) <- runInteractiveCommand (prog ++ concat (map (' ':) args))
                                exit <- waitForProcess p
                                let closeHandles = do hClose o
                                                      hClose i
                                                      hClose e
                                case exit of
                                  ExitSuccess -> do str <- hGetLine o
                                                    closeHandles
                                                    return str
                                  _ -> do closeHandles
                                          return $ "Could not execute command " ++ prog