From bcb7793d5444be36bcb999409771d4493ec157a9 Mon Sep 17 00:00:00 2001 From: Andrea Rossato Date: Tue, 24 Jul 2007 13:40:47 +0200 Subject: Batt: better error handling darcs-hash:20070724114047-d6583-3006b98cf7421b40d1723047ea3c7fc89136ce7b.gz --- Plugins/Monitors/Batt.hs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) (limited to 'Plugins/Monitors') diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 0c2219a..c4e463a 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -17,6 +17,9 @@ module Plugins.Monitors.Batt where import qualified Data.ByteString.Lazy.Char8 as B import Plugins.Monitors.Common +data Batt = Batt Float + | NA + battConfig :: IO MConfig battConfig = mkMConfig "Batt: " -- template @@ -34,7 +37,7 @@ readFileBatt (i,s) = b <- catch (B.readFile s) (const $ return B.empty) return (a,b) -parseBATT :: IO Float +parseBATT :: IO Batt parseBATT = do (a1,b1) <- readFileBatt fileB1 (a2,b2) <- readFileBatt fileB2 @@ -43,8 +46,9 @@ parseBATT = x -> read x (f1, p1) = (sp (3,2) a1, sp (2,4) b1) (f2, p2) = (sp (3,2) a2, sp (2,4) b2) - return $ (p1 + p2) / (f1 + f2) --present / full - + left = (p1 + p2) / (f1 + f2) --present / full + return $ if isNaN left then NA else Batt left + formatBatt :: Float -> Monitor [String] formatBatt x = do let f s = floatToPercent (s / 100) @@ -54,5 +58,7 @@ formatBatt x = runBatt :: [String] -> Monitor String runBatt _ = do c <- io $ parseBATT - l <- formatBatt c - parseTemplate l + case c of + Batt x -> do l <- formatBatt x + parseTemplate l + NA -> return "N/A" -- cgit v1.2.3