summaryrefslogtreecommitdiffhomepage
path: root/Commands.hs
diff options
context:
space:
mode:
authorAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-08 11:55:21 +0200
committerAndrea Rossato <andrea.rossato@ing.unitn.it>2007-07-08 11:55:21 +0200
commitc733fb4a4069fa8b41901a9763cbd1769c548517 (patch)
tree9ddd33467257e500462101b40f74d381102f115a /Commands.hs
parent9192f3664b07b161a1c4120e889e37100824b16a (diff)
downloadxmobar-c733fb4a4069fa8b41901a9763cbd1769c548517.tar.gz
xmobar-c733fb4a4069fa8b41901a9763cbd1769c548517.tar.bz2
added Commands
Command is the data type for configuring command execution in XMobar. The idea came in a discussion with Spencer Janssen. At first XMobar was intended to be used only to display the output of external commands, but it came clear that the main performance bottle neck was running external programs. Now, some monitors are run internally, even if in separated threads. darcs-hash:20070708095521-d6583-ba7f21af7c50c5055c9d1a7e6efe9219e6aa21f2.gz
Diffstat (limited to 'Commands.hs')
-rw-r--r--Commands.hs65
1 files changed, 65 insertions, 0 deletions
diff --git a/Commands.hs b/Commands.hs
new file mode 100644
index 0000000..5bc02a9
--- /dev/null
+++ b/Commands.hs
@@ -0,0 +1,65 @@
+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