summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorjao <jao@gnu.org>2016-08-10 03:27:58 +0200
committerjao <jao@gnu.org>2016-08-10 03:27:58 +0200
commit37202c6552e1d3618d00e72ba033db8f51d6c4d1 (patch)
treea60a1ebfa61692042e520d08ad2800c66f8ae9c1
parent1a91118305037d72c8bb14126ba7d42f5055975c (diff)
downloadxmobar-37202c6552e1d3618d00e72ba033db8f51d6c4d1.tar.gz
xmobar-37202c6552e1d3618d00e72ba033db8f51d6c4d1.tar.bz2
Batt.hs: fix by Matthew Bekkema (see #231)
-rw-r--r--src/Plugins/Monitors/Batt.hs9
1 files changed, 6 insertions, 3 deletions
diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs
index fba45dd..cc16ec8 100644
--- a/src/Plugins/Monitors/Batt.hs
+++ b/src/Plugins/Monitors/Batt.hs
@@ -147,7 +147,7 @@ haveAc f =
where onError = const (return False) :: SomeException -> IO Bool
readBattery :: Float -> Files -> IO Battery
-readBattery _ NoFiles = return $ Battery 0 0 0 "Idle"
+readBattery _ NoFiles = return $ Battery 0 0 0 "Unknown"
readBattery sc files =
do a <- grab $ fFull files
b <- grab $ fNow files
@@ -162,13 +162,16 @@ readBattery sc files =
where grab f = handle onError $ withFile f ReadMode (fmap read . hGetLine)
onError = const (return (-1)) :: SomeException -> IO Float
grabs f = handle onError' $ withFile f ReadMode hGetLine
- onError' = const (return "Idle") :: SomeException -> IO String
+ onError' = const (return "Unknown") :: SomeException -> IO String
-- sortOn is only available starting at ghc 7.10
sortOn :: Ord b => (a -> b) -> [a] -> [a]
sortOn f =
map snd . sortBy (comparing fst) . map (\x -> let y = f x in y `seq` (y, x))
+mostCommonDef :: Eq a => a -> [a] -> a
+mostCommonDef x xs = head $ last $ [x] : sortOn length (group xs)
+
readBatteries :: BattOpts -> [Files] -> IO Result
readBatteries opts bfs =
do bats <- mapM (readBattery (scale opts)) (take 3 bfs)
@@ -183,7 +186,7 @@ readBatteries opts bfs =
statuses :: [Status]
statuses = map (fromMaybe Unknown . readMaybe)
(sort (map status bats))
- acst = head $ last $ sortOn length (group statuses)
+ acst = mostCommonDef Unknown $ filter (Unknown/=) statuses
return $ if isNaN left then NA else Result left watts time acst
runBatt :: [String] -> Monitor String