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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
|
module Dfetch.Battery
( Battery(..)
, allBatteries
, renderBat
) where
import Data.List (isPrefixOf)
import GHC.Float (double2Int)
import System.Directory (listDirectory)
import System.FilePath ((</>))
basePath :: FilePath
basePath = "/sys/class/power_supply/"
data Battery = Battery
{ name :: String
, capacity :: Int
, energy :: Double
, energyFull :: Double
} deriving (Show, Eq)
getCapacity :: String -> IO Battery
getCapacity bName = Battery
<$> pure bName
<*> (read <$> readFile (path </> "capacity"))
<*> (read <$> readFile (path </> "energy_now"))
<*> (read <$> readFile (path </> "energy_full"))
where
path = basePath </> bName
findBatteries :: IO [String]
findBatteries = filter ("BAT" `isPrefixOf`) <$> listDirectory basePath
-- | Fetch all batteries from the running system
allBatteries :: IO [Battery]
allBatteries = findBatteries >>= mapM getCapacity
renderBat :: [Battery] -> String
renderBat bs = unlines $
[ "Battery" , "======="] <> map render bs
where
totalEnergy = sum . map (energyFull) $ bs
calcEnergy =
show . double2Int . (* 100) . (/ totalEnergy) . energyFull
render b = mconcat $
[ "\t"
, name b
, ": "
, show (capacity b)
, "%,\t"
, calcEnergy b
, "% of total energy"
]
|