From 37202c6552e1d3618d00e72ba033db8f51d6c4d1 Mon Sep 17 00:00:00 2001 From: jao Date: Wed, 10 Aug 2016 03:27:58 +0200 Subject: Batt.hs: fix by Matthew Bekkema (see #231) --- src/Plugins/Monitors/Batt.hs | 9 ++++++--- 1 file 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 -- cgit v1.2.3