diff options
author | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-07-24 11:44:02 +0200 |
---|---|---|
committer | Andrea Rossato <andrea.rossato@ing.unitn.it> | 2007-07-24 11:44:02 +0200 |
commit | 1209c4f6bb3218cacdfbd7337488abf4bcb05245 (patch) | |
tree | 3de025ecf926e47d362f661d5e739d2cebfc1726 /Plugins/Monitors | |
parent | a002121921c5c68956b3785c3d2a1f62f7f4752b (diff) | |
download | xmobar-1209c4f6bb3218cacdfbd7337488abf4bcb05245.tar.gz xmobar-1209c4f6bb3218cacdfbd7337488abf4bcb05245.tar.bz2 |
Batt.hs: refactor to reduce resource usage
darcs-hash:20070724094402-d6583-3dcd0ad09f538cfa4d34ff7ad636e8b179866500.gz
Diffstat (limited to 'Plugins/Monitors')
-rw-r--r-- | Plugins/Monitors/Batt.hs | 52 |
1 files changed, 11 insertions, 41 deletions
diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 44ee378..0c2219a 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -15,17 +15,8 @@ module Plugins.Monitors.Batt where import qualified Data.ByteString.Lazy.Char8 as B -import qualified Data.Map as M -import Data.Char -import Data.List -import Data.Maybe -import Control.Monad -import System.Posix.Files - import Plugins.Monitors.Common -type BattMap = M.Map String Integer - battConfig :: IO MConfig battConfig = mkMConfig "Batt: <left>" -- template @@ -37,44 +28,23 @@ fileB1 = ("/proc/acpi/battery/BAT1/info", "/proc/acpi/battery/BAT1/state") fileB2 :: (String, String) fileB2 = ("/proc/acpi/battery/BAT2/info", "/proc/acpi/battery/BAT2/state") -checkFileBatt :: (String, String) -> IO Bool -checkFileBatt (i,_) = - fileExist i - -readFileBatt :: (String, String) -> IO BattMap +readFileBatt :: (String, String) -> IO (B.ByteString, B.ByteString) readFileBatt (i,s) = 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 parseLine $ concatMap B.lines [a, b] - -parseLine :: B.ByteString -> Maybe (String, Integer) -parseLine s = do - let (k, r) = B.break (==':') s - (_, r') = B.span (isSpace) (B.tail r) - (v, _) = B.span (isDigit) r' - (ks, vs) = (B.unpack k, B.unpack v) - guard $ all (> 0) (map B.length [r, k, v]) - return (ks, read vs) + return (a,b) parseBATT :: IO Float parseBATT = - 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" - + do (a1,b1) <- readFileBatt fileB1 + (a2,b2) <- readFileBatt fileB2 + let sp p s = case stringParser p s of + [] -> 0 + 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 + formatBatt :: Float -> Monitor [String] formatBatt x = do let f s = floatToPercent (s / 100) |