summaryrefslogtreecommitdiffhomepage
path: root/Commands.hs
blob: 6b8dcbd47f3475659fd07ecf3026b3926b03eb02 (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
-----------------------------------------------------------------------------
-- |
-- 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 is for OS commands to be run by Xmobar 
--
-----------------------------------------------------------------------------

module Commands where

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

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

data Command = Com Program Args Alias Rate
               deriving (Show,Read,Eq)

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

instance Exec Command where
    alias (Com p _ a _) | p /= "" = if  a == "" then p else a
                        | otherwise = ""
    rate (Com _ _ _ r) = r
    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