From f7cbb14d9ee40d07abcde45868507f858e072fdd Mon Sep 17 00:00:00 2001 From: Jose Antonio Ortega Ruiz Date: Wed, 8 Dec 2010 23:02:56 +0100 Subject: Battery monitor: specific arguments instead of hardcoded hilite --- Plugins/Monitors/Batt.hs | 112 +++++++++++++++++++++++++++++++++++------------ 1 file changed, 85 insertions(+), 27 deletions(-) (limited to 'Plugins/Monitors/Batt.hs') diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index bb2027c..b860210 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -1,7 +1,7 @@ ----------------------------------------------------------------------------- -- | -- Module : Plugins.Monitors.Batt --- Copyright : (c) Andrea Rossato +-- Copyright : (c) Andrea Rossato, 2010 Petr Rockai, 2010 Jose A Ortega -- License : BSD-style (see LICENSE) -- -- Maintainer : Andrea Rossato @@ -17,20 +17,72 @@ module Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where import qualified Data.ByteString.Lazy.Char8 as B import Plugins.Monitors.Common import System.Posix.Files (fileExist) +import System.Console.GetOpt -data Result = Result { percent :: Float, watts :: Float, time :: Float, ac :: String } - | NA +data BattOpts = BattOpts + { onString :: String + , offString :: String + , posColor :: Maybe String + , lowWColor :: Maybe String + , mediumWColor :: Maybe String + , highWColor :: Maybe String + , lowThreshold :: Float + , highThreshold :: Float + } +defaultOpts :: BattOpts +defaultOpts = BattOpts + { onString = "On" + , offString = "Off" + , posColor = Nothing + , lowWColor = Nothing + , mediumWColor = Nothing + , highWColor = Nothing + , lowThreshold = -12 + , highThreshold = -10 + } + +options :: [OptDescr (BattOpts -> BattOpts)] +options = + [ Option "O" ["on"] (ReqArg (\x o -> o { onString = x }) "") "" + , Option "o" ["off"] (ReqArg (\x o -> o { offString = x }) "") "" + , Option "p" ["positive"] (ReqArg (\x o -> o { posColor = Just x }) "") "" + , Option "l" ["low"] (ReqArg (\x o -> o { lowWColor = Just x }) "") "" + , Option "m" ["medium"] (ReqArg (\x o -> o { mediumWColor = Just x }) "") "" + , Option "h" ["high"] (ReqArg (\x o -> o { highWColor = Just x }) "") "" + , Option "L" ["lowt"] (ReqArg (\x o -> o { lowThreshold = read x }) "") "" + , Option "H" ["hight"] (ReqArg (\x o -> o { highThreshold = read x }) "") "" + ] + +parseOpts :: [String] -> IO BattOpts +parseOpts argv = + case getOpt Permute options argv of + (o, _, []) -> return $ foldr id defaultOpts o + (_, _, errs) -> ioError . userError $ concat errs + +data Result = Result Float Float Float String | NA + +base :: String base = "/sys/class/power_supply" battConfig :: IO MConfig battConfig = mkMConfig - "Batt: , / " -- template - ["leftbar", "left", "status", "timeleft", "watts"] -- available replacements + "Batt: , % / " -- template + ["leftbar", "left", "acstatus", "timeleft", "watts"] -- replacements + +data Files = Files + { f_full :: String + , f_now :: String + , f_voltage :: String + , f_current :: String + } | NoFiles -data Files = Files { f_full :: String, f_now :: String - , f_voltage :: String, f_current :: String } | NoFiles -data Battery = Battery { full :: Float, now :: Float, voltage :: Float, current :: Float } +data Battery = Battery + { full :: Float + , now :: Float + , voltage :: Float + , current :: Float + } batteryFiles :: String -> IO Files batteryFiles bat = @@ -66,42 +118,48 @@ readBattery files = (d / c) -- amperes where grab = fmap (read . B.unpack) . catRead -readBatteries :: [Files] -> IO Result -readBatteries bfs = +readBatteries :: BattOpts -> [Files] -> IO Result +readBatteries opts bfs = do bats <- mapM readBattery (take 3 bfs) ac' <- haveAc - let ac = if ac' == Just True then True else False + let ac = (ac' == Just True) sign = if ac then 1 else -1 - left = (sum $ map now bats) / (sum $ map full bats) - watts = sign * (sum $ map voltage bats) * (sum $ map current bats) + left = sum (map now bats) / sum (map full bats) + watts = sign * sum (map voltage bats) * sum (map current bats) time = if watts == 0 then 0 else sum $ map time' bats -- negate sign time' b = (if ac then full b - now b else now b) / (sign * watts) acstr = case ac' of Nothing -> "?" - Just True -> "On" - Just False -> "Off" + Just True -> onString opts + Just False -> offString opts return $ if isNaN left then NA else Result left watts time acstr runBatt :: [String] -> Monitor String runBatt = runBatt' ["BAT0","BAT1","BAT2"] runBatt' :: [String] -> [String] -> Monitor String -runBatt' bfs _ = do - c <- io $ readBatteries =<< mapM batteryFiles bfs +runBatt' bfs args = do + opts <- io $ parseOpts args + c <- io $ readBatteries opts =<< mapM batteryFiles bfs case c of - Result x w t s -> do l <- fmtPercent x - parseTemplate (l ++ [s] ++ [fmtTime $ floor t, fmtWatts w]) + Result x w t s -> + do l <- fmtPercent x + parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts]) NA -> return "N/A" where fmtPercent :: Float -> Monitor [String] fmtPercent x = do - p <- showPercentsWithColors [x] + p <- showPercentWithColors x b <- showPercentBar (100 * x) x - return (b:p) - fmtWatts x = color x $ showDigits 1 x ++ "W" - fmtTime x = hours ++ ":" ++ if length minutes == 2 then minutes else "0" ++ minutes + return [b, p] + fmtWatts x o = color x o $ showDigits 1 x ++ "W" + fmtTime :: Integer -> String + fmtTime x = hours ++ ":" ++ if length minutes == 2 + then minutes else '0' : minutes where hours = show (x `div` 3600) minutes = show ((x `mod` 3600) `div` 60) - color x str | x >= 0 = "" ++ str ++ "" - | x >= -10 = "" ++ str ++ "" - | x >= -12 = str - | otherwise = "" ++ str ++ "" + maybeColor Nothing _ = "" + maybeColor (Just c) str = "" ++ str ++ "" + color x o | x >= 0 = maybeColor (posColor o) + | x >= highThreshold o = maybeColor (highWColor o) + | x >= lowThreshold o = maybeColor (mediumWColor o) + | otherwise = maybeColor (lowWColor o) -- cgit v1.2.3