diff options
| author | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-12-17 14:56:28 +0100 | 
|---|---|---|
| committer | Jose Antonio Ortega Ruiz <jao@gnu.org> | 2011-12-17 14:56:28 +0100 | 
| commit | 3b49df5e2819874296f5b2206edd00cd4a8c21aa (patch) | |
| tree | 64d781352b4d9d13209a384e4e700235dc951067 /src | |
| parent | d6cd9d7c55a15450efed8dcd70d8eb70fe6cc002 (diff) | |
| download | xmobar-3b49df5e2819874296f5b2206edd00cd4a8c21aa.tar.gz xmobar-3b49df5e2819874296f5b2206edd00cd4a8c21aa.tar.bz2 | |
Batt improvements: watts display fix and no -c needed
We no longer use -c to specify cases when only _design exists, but try
to detect that automatically (as well as the energy/charge dichotomy).
Also fixed a bug that precluded display of <watts> when no threshold
colours were specified.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Plugins/Monitors/Batt.hs | 45 | 
1 files changed, 22 insertions, 23 deletions
| diff --git a/src/Plugins/Monitors/Batt.hs b/src/Plugins/Monitors/Batt.hs index 662ff43..f0aaa4a 100644 --- a/src/Plugins/Monitors/Batt.hs +++ b/src/Plugins/Monitors/Batt.hs @@ -13,8 +13,6 @@  --  ----------------------------------------------------------------------------- -{-# LANGUAGE BangPatterns #-} -  module Plugins.Monitors.Batt ( battConfig, runBatt, runBatt' ) where  import Control.Exception (SomeException, handle) @@ -34,7 +32,6 @@ data BattOpts = BattOpts    , lowThreshold :: Float    , highThreshold :: Float    , onlineFile :: FilePath -  , chargeFile :: FilePath    }  defaultOpts :: BattOpts @@ -48,7 +45,6 @@ defaultOpts = BattOpts    , lowThreshold = -12    , highThreshold = -10    , onlineFile = "AC/online" -  , chargeFile = "charge_full"    }  options :: [OptDescr (BattOpts -> BattOpts)] @@ -62,7 +58,6 @@ options =    , Option "L" ["lowt"] (ReqArg (\x o -> o { lowThreshold = read x }) "") ""    , Option "H" ["hight"] (ReqArg (\x o -> o { highThreshold = read x }) "") ""    , Option "f" ["online"] (ReqArg (\x o -> o { onlineFile = x }) "") "" -  , Option "c" ["charge"] (ReqArg (\x o -> o { chargeFile = x }) "") ""    ]  parseOpts :: [String] -> IO BattOpts @@ -95,25 +90,28 @@ data Battery = Battery    , current :: !Float    } -safeFileExist :: String -> IO Bool -safeFileExist f = handle noErrors $ fileExist f +safeFileExist :: String -> String -> IO Bool +safeFileExist d f = handle noErrors $ fileExist (d </> f)    where noErrors = const (return False) :: SomeException -> IO Bool -batteryFiles :: String -> String -> IO Files -batteryFiles charge_file bat = -  do is_charge <- safeFileExist $ prefix </> "charge_now" -     is_energy <- safeFileExist $ prefix </> "energy_now" -     is_current <- safeFileExist $ prefix </> "current_now" +batteryFiles :: String -> IO Files +batteryFiles bat = +  do is_charge <- exists "charge_now" +     is_energy <- if is_charge then return False else exists "energy_now" +     is_current <- exists "current_now" +     plain <- if is_charge then exists "charge_full" else exists "energy_full"       let cf = if is_current then "current_now" else "power_now" +         sf = if plain then "" else "_design"       return $ case (is_charge, is_energy) of -       (True, _) -> files "charge" cf -       (_, True) -> files "energy" cf +       (True, _) -> files "charge" cf sf +       (_, True) -> files "energy" cf sf         _ -> NoFiles    where prefix = sysDir </> bat -        files ch cf = Files { fFull = prefix </> charge_file -                            , fNow = prefix </> ch ++ "_now" -                            , fCurrent = prefix </> cf -                            , fVoltage = prefix </> "voltage_now" } +        exists = safeFileExist prefix +        files ch cf sf = Files { fFull = prefix </> ch ++ "_full" ++ sf +                               , fNow = prefix </> ch ++ "_now" +                               , fCurrent = prefix </> cf +                               , fVoltage = prefix </> "voltage_now" }  haveAc :: FilePath -> IO Bool  haveAc f = @@ -130,7 +128,7 @@ readBattery files =         return $ Battery (3600 * a / 1000000) -- wattseconds                          (3600 * b / 1000000) -- wattseconds                          (c / 1000000) -- volts -                        (if c > 0 then (d / c) else -1) -- amperes +                        (if c > 0 then d / c else -1) -- amperes      where grab f = handle onError $ withFile f ReadMode (fmap read . hGetLine)            onError = const (return (-1)) :: SomeException -> IO Float @@ -154,24 +152,25 @@ runBatt = runBatt' ["BAT0","BAT1","BAT2"]  runBatt' :: [String] -> [String] -> Monitor String  runBatt' bfs args = do    opts <- io $ parseOpts args -  c <- io $ readBatteries opts =<< mapM (batteryFiles (chargeFile opts)) bfs +  c <- io $ readBatteries opts =<< mapM batteryFiles bfs +  suffix <- getConfigValue useSuffix    case c of      Result x w t s ->        do l <- fmtPercent x -         parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts]) +         parseTemplate (l ++ s:[fmtTime $ floor t, fmtWatts w opts suffix])      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" +        fmtWatts x o s = color x o $ showDigits 1 x ++ (if s then "W" else "")          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 Nothing str = str          maybeColor (Just c) str = "<fc=" ++ c ++ ">" ++ str ++ "</fc>"          color x o | x >= 0 = maybeColor (posColor o)                    | x >= highThreshold o = maybeColor (highWColor o) | 
