From b79dc579f90c519de2883febce0952e1d233d76b Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Sun, 8 Jul 2007 12:16:28 +0200 Subject: some monitors are now run internally darcs-hash:20070708101628-d6583-e37e08ad4f17993b2b1102efd3b71105f259dc01.gz --- XMobar.hs | 57 +++++++++++++++------------------------------------------ 1 file changed, 15 insertions(+), 42 deletions(-) diff --git a/XMobar.hs b/XMobar.hs index ae8d5fe..c9da0b5 100644 --- a/XMobar.hs +++ b/XMobar.hs @@ -25,7 +25,6 @@ module XMobar (-- * Main Stuff , printStrings -- * Program Execution -- $commands - , getOptions , execCommands , execCommand , runCommandLoop @@ -45,12 +44,9 @@ import Control.Monad.State import Control.Monad.Reader import Control.Concurrent -import System.Process -import System.Exit -import System.IO (hClose, hGetLine) - import Config import Parsers +import Commands -- $main -- @@ -155,64 +151,41 @@ printStrings gc fontst offs sl@((s,c,l):xs) = -- $commands --- | Gets the command options set in configuration. -getOptions :: Config -> String -> [String] -getOptions c com = - let l = commands c - p = filter (\(s,_,_) -> s == com) l - in case p of - [(_,_,opts)] -> opts - _ -> [] - -- | Gets the refresh rate set in configuration for a given command. -getRefRate :: Config -> String -> Int +getRefRate :: Config -> Command -> Int getRefRate c com = let l = commands c - p = filter (\(s,_,_) -> s == com) l + p = filter (\(s,_) -> s == com) l in case p of - [(_,int,_)] -> int + [(_,int)] -> int _ -> refresh c --- | Runs a list of programs -execCommands :: Config -> [(String,String,String)] -> IO [(ThreadId, MVar String)] +-- | Runs a list of programs as independent threads and returns their thread id +-- and the MVar they will be writing to. +execCommands :: Config -> [(Command,String,String)] -> IO [(ThreadId, MVar String)] execCommands _ [] = return [] execCommands c (x:xs) = do i <- execCommand c x is <- execCommands c xs return $ i : is -execCommand :: Config -> (String,String,String) -> IO (ThreadId, MVar String) +execCommand :: Config -> (Command,String,String) -> IO (ThreadId, MVar String) execCommand c com = do var <- newMVar "Updating..." h <- forkIO $ runCommandLoop var c com return (h,var) --- | Runs the external program -runCommandLoop :: MVar String -> Config -> (String,String,String) -> IO () -runCommandLoop var conf c@(s,com,ss) - | com == "" = +runCommandLoop :: MVar String -> Config -> (Command,String,String) -> IO () +runCommandLoop var conf c@(com,s,ss) + | show com == "" = do modifyMVar_ var (\_ -> return $ "Could not parse the template") tenthSeconds (refresh conf) runCommandLoop var conf c | otherwise = - do (i,o,e,p) <- runInteractiveCommand (com ++ concat (map (' ':) $ getOptions conf com)) - -- the followinf leaks memory - --(i,o,e,p) <- runInteractiveProcess com (getOptions c com) Nothing Nothing - exit <- waitForProcess p - let closeHandles = do hClose o - hClose i - hClose e - case exit of - ExitSuccess -> do str <- hGetLine o - closeHandles - modifyMVar_ var (\_ -> return $ s ++ str ++ ss) - tenthSeconds (getRefRate conf com) - runCommandLoop var conf c - _ -> do closeHandles - modifyMVar_ var $ \_ -> return $ "Could not execute command " ++ com - tenthSeconds (getRefRate conf com) - runCommandLoop var conf c - + do str <- run com + modifyMVar_ var (\_ -> return $ s ++ str ++ ss) + tenthSeconds (getRefRate conf com) + runCommandLoop var conf c -- | Reads MVars set by 'runCommandLoop' readVariables :: [(ThreadId, MVar String)] -> IO String -- cgit v1.2.3