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/Common.hs | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) (limited to 'Plugins/Monitors/Common.hs') 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]) -- cgit v1.2.3