diff options
Diffstat (limited to 'src/Plugins/Monitors/Mem.hs')
-rw-r--r-- | src/Plugins/Monitors/Mem.hs | 73 |
1 files changed, 54 insertions, 19 deletions
diff --git a/src/Plugins/Monitors/Mem.hs b/src/Plugins/Monitors/Mem.hs index 3cf46c7..403fa43 100644 --- a/src/Plugins/Monitors/Mem.hs +++ b/src/Plugins/Monitors/Mem.hs @@ -15,12 +15,45 @@ module Plugins.Monitors.Mem (memConfig, runMem, totalMem, usedMem) where import Plugins.Monitors.Common +import qualified Data.Map as M +import System.Console.GetOpt + +data MemOpts = MemOpts + { usedIconPattern :: Maybe IconPattern + , freeIconPattern :: Maybe IconPattern + , availableIconPattern :: Maybe IconPattern + } + +defaultOpts :: MemOpts +defaultOpts = MemOpts + { usedIconPattern = Nothing + , freeIconPattern = Nothing + , availableIconPattern = Nothing + } + +options :: [OptDescr (MemOpts -> MemOpts)] +options = + [ Option "" ["used-icon-pattern"] (ReqArg (\x o -> + o { usedIconPattern = Just $ parseIconPattern x }) "") "" + , Option "" ["free-icon-pattern"] (ReqArg (\x o -> + o { freeIconPattern = Just $ parseIconPattern x }) "") "" + , Option "" ["available-icon-pattern"] (ReqArg (\x o -> + o { availableIconPattern = Just $ parseIconPattern x }) "") "" + ] + +parseOpts :: [String] -> IO MemOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, []) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs memConfig :: IO MConfig memConfig = mkMConfig "Mem: <usedratio>% (<cache>M)" -- template - ["usedbar", "freebar", "usedratio", "freeratio", "total", - "free", "buffer", "cache", "rest", "used"] -- available replacements + ["usedbar", "usedvbar", "usedipat", "freebar", "freevbar", "freeipat", + "availablebar", "availablevbar", "availableipat", + "usedratio", "freeratio", "availableratio", + "total", "free", "buffer", "cache", "available", "used"] -- available replacements fileMEM :: IO String fileMEM = readFile "/proc/meminfo" @@ -28,13 +61,15 @@ fileMEM = readFile "/proc/meminfo" parseMEM :: IO [Float] parseMEM = do file <- fileMEM - let content = map words $ take 4 $ lines file - [total, free, buffer, cache] = map (\line -> (read $ line !! 1 :: Float) / 1024) content - rest = free + buffer + cache - used = total - rest + let content = map words $ take 8 $ lines file + info = M.fromList $ map (\line -> (head line, (read $ line !! 1 :: Float) / 1024)) content + [total, free, buffer, cache] = map (info M.!) ["MemTotal:", "MemFree:", "Buffers:", "Cached:"] + available = M.findWithDefault (free + buffer + cache) "MemAvailable:" info + used = total - available usedratio = used / total freeratio = free / total - return [usedratio, freeratio, total, free, buffer, cache, rest, used, freeratio] + availableratio = available / total + return [usedratio, freeratio, availableratio, total, free, buffer, cache, available, used] totalMem :: IO Float totalMem = fmap ((*1024) . (!!1)) parseMEM @@ -42,20 +77,20 @@ totalMem = fmap ((*1024) . (!!1)) parseMEM usedMem :: IO Float usedMem = fmap ((*1024) . (!!6)) parseMEM -formatMem :: [Float] -> Monitor [String] -formatMem (r:fr:xs) = +formatMem :: MemOpts -> [Float] -> Monitor [String] +formatMem opts (r:fr:ar:xs) = do let f = showDigits 0 - rr = 100 * r - ub <- showPercentBar rr r - fb <- showPercentBar (100 - rr) (1 - r) - rs <- showPercentWithColors r - fs <- showPercentWithColors fr - s <- mapM (showWithColors f) xs - return (ub:fb:rs:fs:s) -formatMem _ = return $ replicate 10 "N/A" + mon i x = [showPercentBar (100 * x) x, showVerticalBar (100 * x) x, showIconPattern i x] + sequence $ mon (usedIconPattern opts) r + ++ mon (freeIconPattern opts) fr + ++ mon (availableIconPattern opts) ar + ++ map showPercentWithColors [r, fr, ar] + ++ map (showWithColors f) xs +formatMem _ _ = replicate 10 `fmap` getConfigValue naString runMem :: [String] -> Monitor String -runMem _ = +runMem argv = do m <- io parseMEM - l <- formatMem m + opts <- io $ parseOpts argv + l <- formatMem opts m parseTemplate l |