From 32dc866c396c262d3781e03bdfd1acd2b4a151ee Mon Sep 17 00:00:00 2001 From: Jose A Ortega Ruiz Date: Mon, 22 Feb 2010 15:42:26 +0100 Subject: Wireless monitor: better boundary checks Ignore-this: cfb69e10b2eaff6924a2df32b1e51bf0 darcs-hash:20100222144226-748be-f28cbe827c89238074448b23da7ba606f930ceb9.gz --- IWlib.hsc | 5 +++-- Plugins/Monitors/Common.hs | 16 +++++++++++----- Plugins/Monitors/Wireless.hs | 6 ++---- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/IWlib.hsc b/IWlib.hsc index afd6bf0..f0bc655 100644 --- a/IWlib.hsc +++ b/IWlib.hsc @@ -69,7 +69,8 @@ getWirelessInfo iface = qualv <- (#peek struct iw_param, value) qual :: IO CInt let qualm = (#ptr struct iw_range, max_qual) rng mv <- (#peek struct iw_param, value) qualm :: IO CInt - return $ fromIntegral qualv / fromIntegral (max 1 mv) + let mxv = if mv > 0 then fromIntegral mv else 1 + return $ fromIntegral qualv / mxv else return (-1) let qv = round (100 * (q :: Double)) - return $ WirelessInfo { wiEssid = essid, wiQuality = qv } + return $ WirelessInfo { wiEssid = essid, wiQuality = min 100 qv } diff --git a/Plugins/Monitors/Common.hs b/Plugins/Monitors/Common.hs index c99a40f..101907b 100644 --- a/Plugins/Monitors/Common.hs +++ b/Plugins/Monitors/Common.hs @@ -36,6 +36,7 @@ module Plugins.Monitors.Common ( -- ** String Manipulation -- $strings , padString + , showWithPadding , showWithColors , showWithColors' , showPercentsWithColors @@ -339,15 +340,20 @@ setColor str s = Just c -> return $ "" ++ str ++ "" +showWithPadding :: String -> Monitor String +showWithPadding s = + do mn <- getConfigValue minWidth + mx <- getConfigValue maxWidth + p <- getConfigValue padChars + pr <- getConfigValue padRight + return $ padString mn mx p pr s + showWithColors :: (Num a, Ord a) => (a -> String) -> a -> Monitor String showWithColors f x = do h <- getConfigValue high l <- getConfigValue low - mn <- getConfigValue minWidth - mx <- getConfigValue maxWidth - p <- getConfigValue padChars - pr <- getConfigValue padRight - let col = setColor $ padString mn mx p pr $ f x + 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 ] ++ diff --git a/Plugins/Monitors/Wireless.hs b/Plugins/Monitors/Wireless.hs index fe81ab8..eb07afe 100644 --- a/Plugins/Monitors/Wireless.hs +++ b/Plugins/Monitors/Wireless.hs @@ -24,10 +24,8 @@ runWireless :: [String] -> Monitor String runWireless (iface:_) = do wi <- io $ getWirelessInfo iface let essid = wiEssid wi - quality = wiQuality wi + qlty = wiQuality wi e = if essid == "" then "N/A" else essid - q <- if quality >= 0 - then showWithColors show quality - else showWithColors' "" (0 :: Int) + q <- if qlty >= 0 then showWithColors show qlty else showWithPadding "" parseTemplate [e, q] runWireless _ = return "" \ No newline at end of file -- cgit v1.2.3