diff options
author | Krzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com> | 2007-07-16 02:34:48 +0200 |
---|---|---|
committer | Krzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com> | 2007-07-16 02:34:48 +0200 |
commit | e0c7868402dc99a06816f55d6249b1b9e6351122 (patch) | |
tree | 8c3414a7f00f6a4af4912813aac0f66d64927ba8 | |
parent | 5483ea4be08b7609e71eb5f1168f2746dcbe2d8f (diff) | |
download | xmobar-e0c7868402dc99a06816f55d6249b1b9e6351122.tar.gz xmobar-e0c7868402dc99a06816f55d6249b1b9e6351122.tar.bz2 |
Fix parsing and processing data in Batt monitor.
Sometimes the monitor would crash due to bad index given to (!!). Now it
should be safe, as parsing is done differently.
darcs-hash:20070716003448-ba08c-4ae93a39919c2aee628825e5a9f7da8970dbcb8c.gz
-rw-r--r-- | Plugins/Monitors/Batt.hs | 53 |
1 files changed, 38 insertions, 15 deletions
diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 79c0015..96cd8ef 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -15,10 +15,17 @@ module Plugins.Monitors.Batt where import qualified Data.ByteString.Lazy.Char8 as B +import qualified Data.Map as M +import Data.Maybe +import Data.Either +import Control.Monad +import Text.ParserCombinators.Parsec import System.Posix.Files import Plugins.Monitors.Common +type BattMap = M.Map String Integer + battConfig :: IO MConfig battConfig = mkMConfig "Batt: <left>" -- template @@ -34,25 +41,41 @@ checkFileBatt :: (String, String) -> IO Bool checkFileBatt (i,_) = fileExist i -readFileBatt :: (String, String) -> IO (B.ByteString, B.ByteString) +readFileBatt :: (String, String) -> IO BattMap readFileBatt (i,s) = - do a <- B.readFile i - b <- B.readFile s - return (a,b) + do a <- catch (B.readFile i) (const $ return B.empty) + b <- catch (B.readFile s) (const $ return B.empty) + return $ mkMap a b + +mkMap :: B.ByteString -> B.ByteString -> BattMap +mkMap a b = M.fromList . mapMaybe toAssoc $ concatMap B.lines [a, b] + +toAssoc bs = case parse parseLine "" (B.unpack bs) of + Left _ -> Nothing + Right a -> Just a + +parseLine = do hd <- many1 $ noneOf ":" + char ':' + spaces + tl <- many1 digit + let t = read tl + return (hd, t) parseBATT :: IO Float parseBATT = - do (a1,b1) <- readFileBatt fileB1 - c <- checkFileBatt fileB2 - let sp p s = read $ stringParser p s - (fu, pr) = (sp (3,2) a1, sp (2,4) b1) - case c of - True -> do (a2,b2) <- readFileBatt fileB1 - let full = fu + (sp (3,2) a2) - present = pr + (sp (2,4) b2) - return $ present / full - _ -> return $ pr / fu - + do m1 <- readFileBatt fileB1 + m2 <- readFileBatt fileB2 + let pr1 = M.findWithDefault 0 remKey m1 + fu1 = M.findWithDefault 0 fullKey m1 + pr2 = M.findWithDefault 0 remKey m2 + fu2 = M.findWithDefault 0 fullKey m2 + pr = pr1 + pr2 + fu = fu1 + fu2 + pc = if fu /= 0 then fromInteger pr / fromInteger fu else 0.0 + return pc + where + remKey = "remaining capacity" + fullKey = "last full capacity" formatBatt :: Float -> Monitor [String] formatBatt x = |