diff options
Diffstat (limited to 'packages/code/dfetch/app')
| -rw-r--r-- | packages/code/dfetch/app/Dfetch/Battery.hs | 24 | ||||
| -rw-r--r-- | packages/code/dfetch/app/Dfetch/Brightness.hs | 29 | ||||
| -rw-r--r-- | packages/code/dfetch/app/Main.hs | 7 |
3 files changed, 46 insertions, 14 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 diff --git a/packages/code/dfetch/app/Dfetch/Brightness.hs b/packages/code/dfetch/app/Dfetch/Brightness.hs index 912905a..9d7b7b9 100644 --- a/packages/code/dfetch/app/Dfetch/Brightness.hs +++ b/packages/code/dfetch/app/Dfetch/Brightness.hs @@ -1,8 +1,29 @@ -module Dfetch.Brightness where +module Dfetch.Brightness + ( Brightness(..) + , fetchBrightness + , allBs + ) where -import System.FilePath ((</>)) -import System.Directory (listDirectory) -import Data.List (isPrefixOf) +import System.Directory (listDirectory) +import System.FilePath ((</>)) basePath :: FilePath basePath = "/sys/class/backlight/" + +data Brightness = Brightness + { bright :: Int + , maxbright :: Int + } deriving Show + +backlights :: IO [String] +backlights = listDirectory basePath + +fetchBrightness :: String -> IO Brightness +fetchBrightness device = Brightness + <$> (read <$> readFile (path </> "brightness")) + <*> (read <$> readFile (path </> "max_brightness")) + where + path = basePath </> device + +allBs :: IO [Brightness] +allBs = backlights >>= mapM fetchBrightness diff --git a/packages/code/dfetch/app/Main.hs b/packages/code/dfetch/app/Main.hs index d9a15ac..e04ff42 100644 --- a/packages/code/dfetch/app/Main.hs +++ b/packages/code/dfetch/app/Main.hs @@ -1,8 +1,11 @@ module Main (main) where import Dfetch.Battery (allBatteries) +import Dfetch.Brightness (allBs) main :: IO () main = do - bs <- allBatteries - print bs + bats <- allBatteries + backlights <- allBs + print bats + print backlights |
