From dc171e4a07e597f9f5d7839c231b8b9c3ae19437 Mon Sep 17 00:00:00 2001 From: Jose A Ortega Ruiz Date: Fri, 19 Mar 2010 21:33:24 +0100 Subject: Usage bars Ignore-this: 63fd21a117029674e33a9c4419dbc4de ASCII art bars for a bunch of monitors. darcs-hash:20100319203324-748be-2f927aa0e16d8874e10a04f0245427d32e0e53ce.gz --- Plugins/Monitors/Batt.hs | 7 +++++-- Plugins/Monitors/Common.hs | 34 +++++++++++++++++++++++++--------- Plugins/Monitors/Cpu.hs | 8 ++++++-- Plugins/Monitors/Disk.hs | 7 +++++-- Plugins/Monitors/Mem.hs | 23 ++++++++++++++--------- Plugins/Monitors/MultiCpu.hs | 15 +++++++++------ Plugins/Monitors/Net.hs | 22 +++++++++++++--------- Plugins/Monitors/Wireless.hs | 7 +++++-- 8 files changed, 82 insertions(+), 41 deletions(-) (limited to 'Plugins') diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 6ea62a9..16af6da 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -24,7 +24,7 @@ data Batt = Batt Float String battConfig :: IO MConfig battConfig = mkMConfig "Batt: " -- template - ["left","status"] -- available replacements + ["leftbar", "left", "status"] -- available replacements type File = (String, String) @@ -61,7 +61,10 @@ parseBATT bfs = return $ if isNaN left then NA else Batt left c0 formatBatt :: Float -> Monitor [String] -formatBatt x = showPercentsWithColors [x] +formatBatt x = do + p <- showPercentsWithColors [x] + b <- showPercentBar (100 * x) x + return (b:p) runBatt :: [String] -> Monitor String runBatt = runBatt' ["BAT0","BAT1","BAT2"] diff --git a/Plugins/Monitors/Common.hs b/Plugins/Monitors/Common.hs index 101907b..535329e 100644 --- a/Plugins/Monitors/Common.hs +++ b/Plugins/Monitors/Common.hs @@ -40,6 +40,7 @@ module Plugins.Monitors.Common ( , showWithColors , showWithColors' , showPercentsWithColors + , showPercentBar , showWithUnits , takeDigits , showDigits @@ -348,16 +349,18 @@ showWithPadding s = pr <- getConfigValue padRight return $ padString mn mx p pr s +colorizeString :: (Num a, Ord a) => a -> String -> Monitor String +colorizeString x s = do + h <- getConfigValue high + l <- getConfigValue low + let col = setColor s + [ll,hh] = map fromIntegral $ sort [l, h] -- consider high < low + head $ [col highColor | x > hh ] ++ + [col normalColor | x > ll ] ++ + [col lowColor | True] + showWithColors :: (Num a, Ord a) => (a -> String) -> a -> Monitor String -showWithColors f x = - do h <- getConfigValue high - l <- getConfigValue low - s <- showWithPadding $ f x - let col = setColor s - [ll,hh] = map fromIntegral $ sort [l, h] -- consider high < low - head $ [col highColor | x > hh ] ++ - [col normalColor | x > ll ] ++ - [col lowColor | True] +showWithColors f x = showWithPadding (f x) >>= colorizeString x showWithColors' :: (Num a, Ord a) => String -> a -> Monitor String showWithColors' str v = showWithColors (const str) v @@ -367,6 +370,19 @@ showPercentsWithColors fs = do fstrs <- mapM floatToPercent fs zipWithM (showWithColors . const) fstrs (map (*100) fs) +barBackground :: Char +barBackground = ':' +barForeground :: Char +barForeground = '#' +barLength :: Int +barLength = 10 + +showPercentBar :: Float -> Float -> Monitor String +showPercentBar v x = do + let len = min barLength $ round (fromIntegral barLength * x) + s <- colorizeString v (replicate len barForeground) + return $ s ++ replicate (barLength - len) barBackground + -- $threads doActionTwiceWithDelay :: Int -> IO [a] -> IO ([a], [a]) diff --git a/Plugins/Monitors/Cpu.hs b/Plugins/Monitors/Cpu.hs index e813713..d94bd0f 100644 --- a/Plugins/Monitors/Cpu.hs +++ b/Plugins/Monitors/Cpu.hs @@ -20,7 +20,7 @@ import qualified Data.ByteString.Lazy.Char8 as B cpuConfig :: IO MConfig cpuConfig = mkMConfig "Cpu: " -- template - ["total","user","nice","system","idle"] -- available replacements + ["bar","total","user","nice","system","idle"] -- available replacements cpuData :: IO [Float] cpuData = do s <- B.readFile "/proc/stat" @@ -40,7 +40,11 @@ parseCPU = formatCpu :: [Float] -> Monitor [String] formatCpu [] = return [""] -formatCpu xs = showPercentsWithColors $ (foldr (+) 0 $ take 3 xs) : xs +formatCpu xs = do + let t = foldr (+) 0 $ take 3 xs + b <- showPercentBar (100 * t) t + ps <- showPercentsWithColors (t:xs) + return (b:ps) runCpu :: [String] -> Monitor String runCpu _ = diff --git a/Plugins/Monitors/Disk.hs b/Plugins/Monitors/Disk.hs index c0d8de7..f3c66a1 100644 --- a/Plugins/Monitors/Disk.hs +++ b/Plugins/Monitors/Disk.hs @@ -27,7 +27,8 @@ diskIOConfig :: IO MConfig diskIOConfig = mkMConfig "" ["total", "read", "write"] diskUConfig :: IO MConfig -diskUConfig = mkMConfig "" ["size", "free", "used", "freep", "usedp"] +diskUConfig = mkMConfig "" + ["size", "free", "used", "freep", "usedp", "freebar", "usedbar"] type DevName = String type Path = String @@ -123,7 +124,9 @@ runDiskU' tmp path = do fr = fromIntegral freep / 100 s <- zipWithM showWithColors' strs [100, freep, 100 - freep] sp <- showPercentsWithColors [fr, 1 - fr] - parseTemplate $ s ++ sp + fb <- showPercentBar (fromIntegral freep) fr + ub <- showPercentBar (fromIntegral $ 100 - freep) (1 - fr) + parseTemplate $ s ++ sp ++ [fb, ub] runDiskU :: [(String, String)] -> [String] -> Monitor String runDiskU disks _ = do diff --git a/Plugins/Monitors/Mem.hs b/Plugins/Monitors/Mem.hs index 16fc3a4..c6c4dc4 100644 --- a/Plugins/Monitors/Mem.hs +++ b/Plugins/Monitors/Mem.hs @@ -3,7 +3,7 @@ -- Module : Plugins.Monitors.Mem -- Copyright : (c) Andrea Rossato -- License : BSD-style (see LICENSE) --- +-- -- Maintainer : Andrea Rossato -- Stability : unstable -- Portability : unportable @@ -19,29 +19,34 @@ import Plugins.Monitors.Common memConfig :: IO MConfig memConfig = mkMConfig "Mem: % (M)" -- template - ["total", "free", "buffer", -- available replacements - "cache", "rest", "used", "usedratio"] + ["usedbar", "freebar", "usedratio", "total", -- available replacements + "free", "buffer", "cache", "rest", "used"] fileMEM :: IO String fileMEM = readFile "/proc/meminfo" parseMEM :: IO [Float] parseMEM = - do file <- fileMEM + 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 - usedratio = used * 100 / total - return [total, free, buffer, cache, rest, used, usedratio] + usedratio = used / total + return [usedratio, total, free, buffer, cache, rest, used] formatMem :: [Float] -> Monitor [String] -formatMem x = +formatMem (r:xs) = do let f n = showDigits 0 n - mapM (showWithColors f) x + rr = 100 * r + ub <- showPercentBar rr r + fb <- showPercentBar (100 - rr) (1 - r) + s <- mapM (showWithColors f) (rr:xs) + return (ub:fb:s) +formatMem _ = return $ replicate 8 "N/A" runMem :: [String] -> Monitor String runMem _ = do m <- io $ parseMEM l <- formatMem m - parseTemplate l + parseTemplate l diff --git a/Plugins/Monitors/MultiCpu.hs b/Plugins/Monitors/MultiCpu.hs index 360671b..223ff24 100644 --- a/Plugins/Monitors/MultiCpu.hs +++ b/Plugins/Monitors/MultiCpu.hs @@ -19,10 +19,10 @@ import qualified Data.ByteString.Lazy.Char8 as B import Data.List (isPrefixOf) multiCpuConfig :: IO MConfig -multiCpuConfig = mkMConfig - "Cpu: " - [ k ++ n | n <- "" : map show [0 :: Int ..] - , k <- ["total","user","nice","system","idle"]] +multiCpuConfig = + mkMConfig "Cpu: " + [ k ++ n | n <- "" : map show [0 :: Int ..] + , k <- ["bar","total","user","nice","system","idle"]] cpuData :: IO [[Float]] @@ -53,8 +53,11 @@ formatMultiCpus xs = fmap concat $ mapM formatCpu xs formatCpu :: [Float] -> Monitor [String] formatCpu xs - | length xs < 4 = showPercentsWithColors $ replicate 5 0.0 - | otherwise = showPercentsWithColors $ (foldr (+) 0 $ take 3 xs) : xs + | length xs < 4 = showPercentsWithColors $ replicate 6 0.0 + | otherwise = let t = foldr (+) 0 $ take 3 xs + in do b <- showPercentBar (100 * t) t + ps <- showPercentsWithColors (t:xs) + return (b:ps) runMultiCpu :: [String] -> Monitor String runMultiCpu _ = diff --git a/Plugins/Monitors/Net.hs b/Plugins/Monitors/Net.hs index e948bc0..7fed311 100644 --- a/Plugins/Monitors/Net.hs +++ b/Plugins/Monitors/Net.hs @@ -29,14 +29,14 @@ interval = 500000 netConfig :: IO MConfig netConfig = mkMConfig ": |" -- template - ["dev", "rx", "tx"] -- available replacements + ["dev", "rx", "tx", "rxbar", "txbar"] -- available replacements -- Given a list of indexes, take the indexed elements from a list. getNElements :: [Int] -> [a] -> [a] getNElements ns as = map (as!!) ns -- Split into words, with word boundaries indicated by the given predicate. --- Drops delimiters. Duplicates 'Data.List.Split.wordsBy'. +-- Drops delimiters. Duplicates 'Data.List.Split.wordsBy'. -- -- > map (wordsBy (`elem` " :")) ["lo:31174097 31174097", "eth0: 43598 88888"] -- @@ -62,17 +62,21 @@ netParser :: B.ByteString -> [NetDev] netParser = map (readNetDev . getNElements [0,1,9] . wordsBy (`elem` " :") . B.unpack) . drop 2 . B.lines -formatNet :: Float -> Monitor String -formatNet d = - showWithColors f d - where f s = showDigits 1 s ++ "Kb" +formatNet :: Float -> Monitor (String, String) +formatNet d = do + h <- getConfigValue high + let dx = 8 + logBase 10 (d / fromIntegral h) + b <- showPercentBar d $ max (dx / 10) 0 + x <- showWithColors f d + return (x, b) + where f s = showDigits 1 s ++ "Kb" printNet :: NetDev -> Monitor String printNet nd = case nd of - ND d r t -> do rx <- formatNet r - tx <- formatNet t - parseTemplate [d,rx,tx] + ND d r t -> do (rx, rb) <- formatNet r + (tx, tb) <- formatNet t + parseTemplate [d,rx,tx,rb,tb] NA -> return "N/A" parseNET :: String -> IO [NetDev] diff --git a/Plugins/Monitors/Wireless.hs b/Plugins/Monitors/Wireless.hs index eb07afe..4ac0c10 100644 --- a/Plugins/Monitors/Wireless.hs +++ b/Plugins/Monitors/Wireless.hs @@ -18,14 +18,17 @@ import Plugins.Monitors.Common import IWlib wirelessConfig :: IO MConfig -wirelessConfig = mkMConfig " " ["essid", "quality"] +wirelessConfig = + mkMConfig " " ["essid", "quality", "qualitybar"] runWireless :: [String] -> Monitor String runWireless (iface:_) = do wi <- io $ getWirelessInfo iface let essid = wiEssid wi qlty = wiQuality wi + fqlty = fromIntegral qlty e = if essid == "" then "N/A" else essid q <- if qlty >= 0 then showWithColors show qlty else showWithPadding "" - parseTemplate [e, q] + qb <- showPercentBar fqlty (fqlty / 100) + parseTemplate [e, q, qb] runWireless _ = return "" \ No newline at end of file -- cgit v1.2.3