summaryrefslogtreecommitdiffhomepage
path: root/src/Plugins/Monitors/Batt.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/Plugins/Monitors/Batt.hs')
-rw-r--r--src/Plugins/Monitors/Batt.hs69
1 files changed, 37 insertions, 32 deletions
diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs
index c276e6b..77b2d62 100644
--- a/src/Plugins/Monitors/Batt.hs
+++ b/src/Plugins/Monitors/Batt.hs
@@ -13,11 +13,14 @@
--
-----------------------------------------------------------------------------
+{-# LANGUAGE BangPatterns #-}
+
module Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where
-import qualified Data.ByteString.Lazy.Char8 as B
+import Control.Exception (SomeException, handle)
import Plugins.Monitors.Common
import System.FilePath ((</>))
+import System.IO (IOMode(ReadMode), hGetLine, withFile)
import System.Posix.Files (fileExist)
import System.Console.GetOpt
@@ -83,17 +86,21 @@ data Files = Files
} | NoFiles
data Battery = Battery
- { full :: Float
- , now :: Float
- , voltage :: Float
- , current :: Float
+ { full :: !Float
+ , now :: !Float
+ , voltage :: !Float
+ , current :: !Float
}
+safeFileExist :: String -> IO Bool
+safeFileExist f = handle noErrors $ fileExist f
+ where noErrors = const (return False) :: SomeException -> IO Bool
+
batteryFiles :: String -> IO Files
batteryFiles bat =
- do is_charge <- fileExist $ prefix </> "charge_now"
- is_energy <- fileExist $ prefix </> "energy_now"
- is_current <- fileExist $ prefix </> "current_now"
+ do is_charge <- safeFileExist $ prefix </> "charge_now"
+ is_energy <- safeFileExist $ prefix </> "energy_now"
+ is_current <- safeFileExist $ prefix </> "current_now"
let cf = if is_current then "current_now" else "power_now"
return $ case (is_charge, is_energy) of
(True, _) -> files "charge" cf
@@ -106,12 +113,9 @@ batteryFiles bat =
, fVoltage = prefix </> "voltage_now" }
haveAc :: FilePath -> IO Bool
-haveAc f = do
- exists <- fileExist ofile
- if exists
- then fmap ((== "1\n") . B.unpack) (B.readFile ofile)
- else return False
- where ofile = sysDir </> f
+haveAc f =
+ handle onError $ withFile (sysDir </> f) ReadMode (fmap (== "1") . hGetLine)
+ where onError = const (return False) :: SomeException -> IO Bool
readBattery :: Files -> IO Battery
readBattery NoFiles = return $ Battery 0 0 0 0
@@ -124,7 +128,8 @@ readBattery files =
(3600 * b / 1000000) -- wattseconds
(c / 1000000) -- volts
(if c > 0 then (d / c) else -1) -- amperes
- where grab f = catch (fmap (read . B.unpack) $ B.readFile f) (\_ -> return 0)
+ where grab f = handle onError $ withFile f ReadMode (fmap read . hGetLine)
+ onError = const (return (-1)) :: SomeException -> IO Float
readBatteries :: BattOpts -> [Files] -> IO Result
readBatteries opts bfs =
@@ -152,20 +157,20 @@ runBatt' bfs args = do
do l <- fmtPercent x
parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts])
NA -> return "N/A"
- where fmtPercent :: Float -> Monitor [String]
- fmtPercent x = do
- p <- showPercentWithColors x
- b <- showPercentBar (100 * x) x
- return [b, p]
- fmtWatts x o = color x o $ showDigits 1 x ++ "W"
- fmtTime :: Integer -> String
- fmtTime x = hours ++ ":" ++ if length minutes == 2
- then minutes else '0' : minutes
- where hours = show (x `div` 3600)
- minutes = show ((x `mod` 3600) `div` 60)
- maybeColor Nothing _ = ""
- maybeColor (Just c) str = "<fc=" ++ c ++ ">" ++ str ++ "</fc>"
- color x o | x >= 0 = maybeColor (posColor o)
- | x >= highThreshold o = maybeColor (highWColor o)
- | x >= lowThreshold o = maybeColor (mediumWColor o)
- | otherwise = maybeColor (lowWColor o)
+ where fmtPercent :: Float -> Monitor [String]
+ fmtPercent x = do
+ p <- showPercentWithColors x
+ b <- showPercentBar (100 * x) x
+ return [b, p]
+ fmtWatts x o = color x o $ showDigits 1 x ++ "W"
+ fmtTime :: Integer -> String
+ fmtTime x = hours ++ ":" ++ if length minutes == 2
+ then minutes else '0' : minutes
+ where hours = show (x `div` 3600)
+ minutes = show ((x `mod` 3600) `div` 60)
+ maybeColor Nothing _ = ""
+ maybeColor (Just c) str = "<fc=" ++ c ++ ">" ++ str ++ "</fc>"
+ color x o | x >= 0 = maybeColor (posColor o)
+ | x >= highThreshold o = maybeColor (highWColor o)
+ | x >= lowThreshold o = maybeColor (mediumWColor o)
+ | otherwise = maybeColor (lowWColor o)