From d1ea9ec3d514c44aba64fce96f4ab2bc95f8580b Mon Sep 17 00:00:00 2001 From: Kleidi Bujari Date: Wed, 12 Aug 2026 14:52:07 -0700 Subject: [dfetch] bringup battery and brightness modules --- packages/code/dfetch/app/Dfetch/Battery.hs | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) (limited to 'packages/code/dfetch/app/Dfetch/Battery.hs') 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 -- cgit v1.3.1