diff options
| author | Kleidi Bujari <mail@4kb.net> | 2026-08-12 14:52:07 -0700 |
|---|---|---|
| committer | Kleidi Bujari <mail@4kb.net> | 2026-08-17 14:15:15 -0700 |
| commit | d1ea9ec3d514c44aba64fce96f4ab2bc95f8580b (patch) | |
| tree | 6bc57f196400c652c0de0ff6f99407a7491f3d50 | |
| parent | fdb7a7b876fc0b46ff36751642cbc05ed4140232 (diff) | |
| download | depot-d1ea9ec3d514c44aba64fce96f4ab2bc95f8580b.tar.gz depot-d1ea9ec3d514c44aba64fce96f4ab2bc95f8580b.tar.bz2 depot-d1ea9ec3d514c44aba64fce96f4ab2bc95f8580b.zip | |
[dfetch] bringup battery and brightness modules
| -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 | ||||
| -rw-r--r-- | packages/code/dfetch/dfetch.cabal | 6 |
4 files changed, 49 insertions, 17 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 diff --git a/packages/code/dfetch/dfetch.cabal b/packages/code/dfetch/dfetch.cabal index 4b23eb3..ff2255e 100644 --- a/packages/code/dfetch/dfetch.cabal +++ b/packages/code/dfetch/dfetch.cabal @@ -3,13 +3,13 @@ name: dfetch version: 0.1.0.0 -- synopsis: -- description: -license: BSD-3-Clause -license-file: LICENSE +-- license: BSD-3-Clause +-- license-file: LICENSE author: Kleidi Bujari maintainer: mail@4kb.net -- copyright: build-type: Simple -extra-doc-files: CHANGELOG.md +-- extra-doc-files: CHANGELOG.md -- extra-source-files: common warnings |
