diff options
Diffstat (limited to 'src/Plugins/Monitors/Cpu.hs')
-rw-r--r-- | src/Plugins/Monitors/Cpu.hs | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/src/Plugins/Monitors/Cpu.hs b/src/Plugins/Monitors/Cpu.hs index 6627f53..7fed989 100644 --- a/src/Plugins/Monitors/Cpu.hs +++ b/src/Plugins/Monitors/Cpu.hs @@ -18,18 +18,40 @@ module Plugins.Monitors.Cpu (startCpu) where import Plugins.Monitors.Common import qualified Data.ByteString.Lazy.Char8 as B import Data.IORef (IORef, newIORef, readIORef, writeIORef) +import System.Console.GetOpt + +data CpuOpts = CpuOpts + { loadIconPattern :: Maybe IconPattern + } + +defaultOpts :: CpuOpts +defaultOpts = CpuOpts + { loadIconPattern = Nothing + } + +options :: [OptDescr (CpuOpts -> CpuOpts)] +options = + [ Option "" ["load-icon-pattern"] (ReqArg (\x o -> + o { loadIconPattern = Just $ parseIconPattern x }) "") "" + ] + +parseOpts :: [String] -> IO CpuOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, []) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs cpuConfig :: IO MConfig cpuConfig = mkMConfig "Cpu: <total>%" - ["bar","total","user","nice","system","idle","iowait"] + ["bar","vbar","ipat","total","user","nice","system","idle","iowait"] -type CpuDataRef = IORef [Float] +type CpuDataRef = IORef [Int] -cpuData :: IO [Float] +cpuData :: IO [Int] cpuData = cpuParser `fmap` B.readFile "/proc/stat" -cpuParser :: B.ByteString -> [Float] +cpuParser :: B.ByteString -> [Int] cpuParser = map (read . B.unpack) . tail . B.words . head . B.lines parseCpu :: CpuDataRef -> IO [Float] @@ -38,22 +60,25 @@ parseCpu cref = b <- cpuData writeIORef cref b let dif = zipWith (-) b a - tot = foldr (+) 0 dif - percent = map (/ tot) dif + tot = fromIntegral $ sum dif + percent = map ((/ tot) . fromIntegral) dif return percent -formatCpu :: [Float] -> Monitor [String] -formatCpu [] = return $ replicate 6 "" -formatCpu xs = do +formatCpu :: CpuOpts -> [Float] -> Monitor [String] +formatCpu _ [] = return $ replicate 8 "" +formatCpu opts xs = do let t = sum $ take 3 xs b <- showPercentBar (100 * t) t + v <- showVerticalBar (100 * t) t + d <- showIconPattern (loadIconPattern opts) t ps <- showPercentsWithColors (t:xs) - return (b:ps) + return (b:v:d:ps) runCpu :: CpuDataRef -> [String] -> Monitor String -runCpu cref _ = +runCpu cref argv = do c <- io (parseCpu cref) - l <- formatCpu c + opts <- io $ parseOpts argv + l <- formatCpu opts c parseTemplate l startCpu :: [String] -> Int -> (String -> IO ()) -> IO () |