summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorKrzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com>2007-07-19 03:55:30 +0200
committerKrzysztof Kosciuszkiewicz <k.kosciuszkiewicz@gmail.com>2007-07-19 03:55:30 +0200
commit238c4ab7e2f076f879d931588f140bab78654747 (patch)
tree650a1c9ac819042d5e21360009f71cdee36f0631
parente0c7868402dc99a06816f55d6249b1b9e6351122 (diff)
downloadxmobar-238c4ab7e2f076f879d931588f140bab78654747.tar.gz
xmobar-238c4ab7e2f076f879d931588f140bab78654747.tar.bz2
Safer parsing without parsec in battery monitor
darcs-hash:20070719015530-ba08c-065e9572b186c12db8236c28fa249793eba51e32.gz
-rw-r--r--Plugins/Monitors/Batt.hs25
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 =