summaryrefslogtreecommitdiffhomepage
path: root/Plugins/Monitors
diff options
context:
space:
mode:
Diffstat (limited to 'Plugins/Monitors')
-rw-r--r--Plugins/Monitors/Batt.hs41
1 files changed, 18 insertions, 23 deletions
diff --git a/Plugins/Monitors/Batt.hs b/Plugins/Monitors/Batt.hs
index 6a7a858..af5a48c 100644
--- a/Plugins/Monitors/Batt.hs
+++ b/Plugins/Monitors/Batt.hs
@@ -3,7 +3,7 @@
-- Module : Plugins.Monitors.Batt
-- Copyright : (c) Andrea Rossato
-- License : BSD-style (see LICENSE)
---
+--
-- Maintainer : Andrea Rossato <andrea.rossato@unibz.it>
-- Stability : unstable
-- Portability : unportable
@@ -18,7 +18,7 @@ import qualified Data.ByteString.Lazy.Char8 as B
import Plugins.Monitors.Common
import System.Posix.Files (fileExist)
-data Batt = Batt Float
+data Batt = Batt Float
| NA
battConfig :: IO MConfig
@@ -26,17 +26,11 @@ battConfig = mkMConfig
"Batt: <left>" -- template
["left"] -- available replacements
-fileB0 :: (String, String)
-fileB0 = ("/proc/acpi/battery/BAT0/info", "/proc/acpi/battery/BAT0/state")
-
-fileB1 :: (String, String)
-fileB1 = ("/proc/acpi/battery/BAT1/info", "/proc/acpi/battery/BAT1/state")
-
-fileB2 :: (String, String)
-fileB2 = ("/proc/acpi/battery/BAT2/info", "/proc/acpi/battery/BAT2/state")
+file2batfile :: String -> (String, String)
+file2batfile s = ("/proc/acpi/battery/"++ s ++ "/info", "/proc/acpi/battery/"++ s ++ "/state")
readFileBatt :: (String, String) -> IO (B.ByteString, B.ByteString)
-readFileBatt (i,s) =
+readFileBatt (i,s) =
do a <- rf i
b <- rf s
return (a,b)
@@ -44,11 +38,9 @@ readFileBatt (i,s) =
f <- fileExist file
if f then B.readFile file else return B.empty
-parseBATT :: IO Batt
-parseBATT =
- do (a0,b0) <- readFileBatt fileB0
- (a1,b1) <- readFileBatt fileB1
- (a2,b2) <- readFileBatt fileB2
+parseBATT :: [(String, String)] -> IO Batt
+parseBATT bfs =
+ do [(a0,b0),(a1,b1),(a2,b2)] <- mapM readFileBatt (take 3 $ bfs ++ repeat ("",""))
let sp p s = case stringParser p s of
[] -> 0
x -> read x
@@ -58,16 +50,19 @@ parseBATT =
left = (p0 + p1 + p2) / (f0 + f1 + f2) --present / full
return $ if isNaN left then NA else Batt left
-formatBatt :: Float -> Monitor [String]
+formatBatt :: Float -> Monitor [String]
formatBatt x =
do let f s = floatToPercent (s / 100)
l <- showWithColors f (x * 100)
return [l]
runBatt :: [String] -> Monitor String
-runBatt _ =
- do c <- io $ parseBATT
- case c of
- Batt x -> do l <- formatBatt x
- parseTemplate l
- NA -> return "N/A"
+runBatt = runBatt' ["BAT0","BAT1","BAT2"]
+
+runBatt' :: [String] -> [String] -> Monitor String
+runBatt' bfs _ = do
+ c <- io $ parseBATT (map file2batfile bfs)
+ case c of
+ Batt x -> do l <- formatBatt x
+ parseTemplate l
+ NA -> return "N/A"