summaryrefslogtreecommitdiffhomepage
path: root/Commands.hs
blob: 1a80b80f95e7d1f97b3a8d1a506b60c0db5cbc95 (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
80
81
82
83
84
85
86
87
88
89
90
-----------------------------------------------------------------------------
-- |
-- Module      :  XMobar.Commands
-- Copyright   :  (c) Andrea Rossato
-- License     :  BSD-style (see LICENSE)
-- 
-- Maintainer  :  Andrea Rossato <andrea.rossato@unibz.it>
-- Stability   :  unstable
-- Portability :  unportable
--
-- The 'Exec' class and the 'Command' data type.
--
-- The 'Exec' class rappresents the executable types, whose constructors may
-- appear in the 'Config.commands' field of the 'Config.Config' data type.
--
-- The 'Command' data type stores the monitors to be run internally by
-- XMobar.
--
-----------------------------------------------------------------------------

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

class Exec e where
    run :: e -> IO String
    rate :: e -> Int
    alias :: e -> String

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

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

instance Exec Command where
    alias (Weather s _ _) = s
    alias (Network i _ _) = i
    alias (Memory _ _) = "memory"
    alias (Swap _ _) = "swap"
    alias (Cpu _ _) = "cpu"
    alias (Battery _ _) = "battery"
    alias (Com p _ a _) | p /= "" = if  a == "" then p else a
                        | otherwise = ""
    rate (Weather _ _ r) = r
    rate (Network _ _ r) = r
    rate (Memory _ r) = r
    rate (Swap _ r) = r
    rate (Cpu _ r) = r
    rate (Battery _ r) = r
    rate (Com _ _ _ r) = r
    run (Weather s a _) = runM (a ++ [s]) weatherConfig runWeather 
    run (Network i a _) = runM (a ++ [i]) netConfig runNet
    run (Memory args _) = runM args memConfig runMem
    run (Swap args _) = runM args swapConfig runSwap
    run (Cpu args _) = runM args cpuConfig runCpu
    run (Battery args _) = runM args battConfig runBatt
    run (Com 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