diff options
author | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-07-12 19:50:34 +0200 |
---|---|---|
committer | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-07-12 19:50:34 +0200 |
commit | e85b0920a06ad019754e1cb8e72eb6cc34cdeedc (patch) | |
tree | 34a28f6a54290bd7347f83efc4a7a5f387419f31 /Commands.hs | |
parent | 14267de1e03841980ce99206c0cf63e40bfa6cca (diff) | |
download | xmobar-e85b0920a06ad019754e1cb8e72eb6cc34cdeedc.tar.gz xmobar-e85b0920a06ad019754e1cb8e72eb6cc34cdeedc.tar.bz2 |
use of existential types for plugin support
This patch, which *changes the configuration format*, adds easy plugin support
by using an existential type for storing the list of commands to be
executed. Adding a plugin is just a matter of writing the appropriate
instance of the Exec class, after importing Commands.hs.
I must thank Claus Reinke for the help in understanding the mysteries of
reading existential types. The Read instance of Runnable must be credited to
him. See here:
http://www.haskell.org/pipermail/haskell-cafe/2007-July/028227.html
darcs-hash:20070712175034-d6583-f10174bb3b0a9b4f6e08d05052c18f30e539b319.gz
Diffstat (limited to 'Commands.hs')
-rw-r--r-- | Commands.hs | 80 |
1 files changed, 46 insertions, 34 deletions
diff --git a/Commands.hs b/Commands.hs index 7c0985e..459892b 100644 --- a/Commands.hs +++ b/Commands.hs @@ -1,3 +1,4 @@ +{-# OPTIONS -fglasgow-exts #-} ----------------------------------------------------------------------------- -- | -- Module : XMobar.Commands @@ -26,48 +27,59 @@ 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) +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 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 + rate :: e -> Int + alias :: e -> String instance Exec Command where - 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 (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 + 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 + + |