diff options
Diffstat (limited to 'packages/code/dfetch/app/Dfetch/Battery.hs')
| -rw-r--r-- | packages/code/dfetch/app/Dfetch/Battery.hs | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/packages/code/dfetch/app/Dfetch/Battery.hs b/packages/code/dfetch/app/Dfetch/Battery.hs index 6e6a0a8..0315882 100644 --- a/packages/code/dfetch/app/Dfetch/Battery.hs +++ b/packages/code/dfetch/app/Dfetch/Battery.hs @@ -3,24 +3,32 @@ module Dfetch.Battery , allBatteries ) where -import System.FilePath ((</>)) -import System.Directory (listDirectory) -import Data.List (isPrefixOf) +import Data.List (isPrefixOf) +import System.Directory (listDirectory) +import System.FilePath ((</>)) basePath :: FilePath basePath = "/sys/class/power_supply/" data Battery = Battery - {name :: String, capacity :: Int} deriving (Show) + { name :: String + , capacity :: Int + , energy :: Double + , energyFull :: Double + } deriving (Show, Eq) getCapacity :: String -> IO Battery -getCapacity name = Battery <$> pure name <*> capacity +getCapacity bName = Battery + <$> pure bName + <*> (read <$> readFile (path </> "capacity")) + <*> (read <$> readFile (path </> "energy_now")) + <*> (read <$> readFile (path </> "energy_full")) where - path = basePath </> name </> "capacity" - capacity = read <$> readFile path + path = basePath </> bName findBatteries :: IO [String] findBatteries = filter ("BAT" `isPrefixOf`) <$> listDirectory basePath +-- | Fetch all batteries from the running system allBatteries :: IO [Battery] -allBatteries = findBatteries >>= traverse getCapacity +allBatteries = findBatteries >>= mapM getCapacity |
