diff options
author | Krzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com> | 2007-07-19 03:55:30 +0200 |
---|---|---|
committer | Krzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com> | 2007-07-19 03:55:30 +0200 |
commit | 238c4ab7e2f076f879d931588f140bab78654747 (patch) | |
tree | 650a1c9ac819042d5e21360009f71cdee36f0631 /Plugins/Monitors | |
parent | e0c7868402dc99a06816f55d6249b1b9e6351122 (diff) | |
download | xmobar-238c4ab7e2f076f879d931588f140bab78654747.tar.gz xmobar-238c4ab7e2f076f879d931588f140bab78654747.tar.bz2 |
Safer parsing without parsec in battery monitor
darcs-hash:20070719015530-ba08c-065e9572b186c12db8236c28fa249793eba51e32.gz
Diffstat (limited to 'Plugins/Monitors')
-rw-r--r-- | Plugins/Monitors/Batt.hs | 25 |
1 files changed, 12 insertions, 13 deletions
diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs index 96cd8ef..4341452 100644 --- a/Plugins/Monitors/Batt.hs +++ b/Plugins/Monitors/Batt.hs @@ -16,10 +16,10 @@ 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 Data.Either import Control.Monad -import Text.ParserCombinators.Parsec import System.Posix.Files import Plugins.Monitors.Common @@ -48,18 +48,17 @@ readFileBatt (i,s) = return $ mkMap a b mkMap :: B.ByteString -> B.ByteString -> BattMap -mkMap a b = M.fromList . mapMaybe toAssoc $ concatMap B.lines [a, b] +mkMap a b = M.fromList . mapMaybe parseLine $ 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) +parseLine :: B.ByteString -> Maybe (String, Integer) +parseLine s = + let (k, r) = B.break (==':') s + (v, _) = B.span (isDigit) (B.tail r) + (ks, vs) = (B.unpack k, B.unpack v) + in case (ks, vs) of + ([], _) -> Nothing + (_, []) -> Nothing + _ -> Just (ks, read vs) parseBATT :: IO Float parseBATT = |