summaryrefslogtreecommitdiff
path: root/packages/code/dfetch/app/Dfetch/Battery.hs
diff options
context:
space:
mode:
authorKleidi Bujari <mail@4kb.net>2026-08-09 17:38:22 -0700
committerKleidi Bujari <mail@4kb.net>2026-08-17 14:15:15 -0700
commitfdb7a7b876fc0b46ff36751642cbc05ed4140232 (patch)
treeeee2304fbbfb62d31d94a033ead0c876c1b036ae /packages/code/dfetch/app/Dfetch/Battery.hs
parent4b56064566c1a7ef6a8c21e11dc1045bc8c3587d (diff)
downloaddepot-fdb7a7b876fc0b46ff36751642cbc05ed4140232.tar.gz
depot-fdb7a7b876fc0b46ff36751642cbc05ed4140232.tar.bz2
depot-fdb7a7b876fc0b46ff36751642cbc05ed4140232.zip
init dfetch for system queries
Diffstat (limited to 'packages/code/dfetch/app/Dfetch/Battery.hs')
-rw-r--r--packages/code/dfetch/app/Dfetch/Battery.hs26
1 files changed, 26 insertions, 0 deletions
diff --git a/packages/code/dfetch/app/Dfetch/Battery.hs b/packages/code/dfetch/app/Dfetch/Battery.hs
new file mode 100644
index 0000000..6e6a0a8
--- /dev/null
+++ b/packages/code/dfetch/app/Dfetch/Battery.hs
@@ -0,0 +1,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