summaryrefslogtreecommitdiff
path: root/packages/code/dfetch/app/Dfetch/Battery.hs
blob: 6e6a0a81c9393abf30456ccd03c43a08e81eb323 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
module Dfetch.Battery
  ( Battery(..)
  , allBatteries
  ) where

import System.FilePath ((</>))
import System.Directory (listDirectory)
import Data.List (isPrefixOf)

basePath :: FilePath
basePath = "/sys/class/power_supply/"

data Battery = Battery
  {name :: String, capacity :: Int} deriving (Show)

getCapacity :: String -> IO Battery
getCapacity name = Battery <$> pure name <*> capacity
  where
    path = basePath </> name </> "capacity"
    capacity = read <$> readFile path

findBatteries :: IO [String]
findBatteries = filter ("BAT" `isPrefixOf`) <$> listDirectory basePath

allBatteries :: IO [Battery]
allBatteries = findBatteries >>= traverse getCapacity