From 46858d626efb306bb8c3f6297eea5e56fc9a8fac Mon Sep 17 00:00:00 2001 From: Fabian Kirchner Date: Mon, 12 Aug 2024 16:31:56 +0200 Subject: feat: show if battery is charging --- app/Sensor.hs | 73 +++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 48 insertions(+), 25 deletions(-) (limited to 'app') diff --git a/app/Sensor.hs b/app/Sensor.hs index f913826..7371c31 100644 --- a/app/Sensor.hs +++ b/app/Sensor.hs @@ -190,39 +190,62 @@ mem = diagram 3 memStat data BatStat = BatStat deriving (Show) -instance (S.MonadSensor m) => S.Aggregate m BatStat Float where +instance (S.MonadSensor m) => S.Aggregate m BatStat (Float, Bool) where aggregate _ = forever do S.yield =<< parse <* sleep where parse = liftIO do - fmap product . mapM (parse1 . ("/sys/class/power_supply" )) + fmap combine + . mapM (parse1 . ("/sys/class/power_supply" )) =<< listDirectory "/sys/class/power_supply" - parse1 :: FilePath -> IO Float + combine abs = + ( product (map fst abs), + or (map Prelude.snd abs) + ) + parse1 :: FilePath -> IO (Float, Bool) parse1 fp = - choice 1 $ - [ (/) - <$> (readFile (fp "charge_now") (pure . read)) - <*> (readFile (fp "charge_full") (pure . read)), - (/) - <$> (readFile (fp "energy_now") (pure . read)) - <*> (readFile (fp "energy_full") (pure . read)) - ] + liftM2 + (,) + ( choice 1 $ + [ (/) + <$> (readFile (fp "charge_now") (pure . read)) + <*> (readFile (fp "charge_full") (pure . read)), + (/) + <$> (readFile (fp "energy_now") (pure . read)) + <*> (readFile (fp "energy_full") (pure . read)) + ] + ) + (choice False [readFile (fp "status") (pure . (==) "charging" . map toLower)]) bat :: (S.MonadSensor m) => S.Sensor m () P.Doc bat = - S.sensor BatStat >>= \value -> - if - | value >= 0.9 -> pure (P.pretty "\xf0082") - | value >= 0.8 -> pure (P.pretty "\xf0081") - | value >= 0.7 -> pure (P.pretty "\xf0080") - | value >= 0.6 -> pure (P.pretty "\xf008f") - | value >= 0.5 -> pure (P.pretty "\xf008e") - | value >= 0.4 -> pure (P.pretty "\xf007d") - | value >= 0.3 -> pure (P.pretty "\xf007c") - | value >= 0.2 -> pure (P.pretty "\xf007b") - | value >= 0.1 -> pure (P.pretty "\xf007a") - | otherwise -> - (\b -> if b then P.pretty "\xf008e" else (P.color P.Red) (P.pretty "\xf0083")) - <$> blink + S.sensor BatStat >>= \(value, charging) -> + if charging + then + if + | value >= 0.9 -> pure (P.pretty "\xf0085") + | value >= 0.8 -> pure (P.pretty "\xf008b") + | value >= 0.7 -> pure (P.pretty "\xf008a") + | value >= 0.6 -> pure (P.pretty "\xf089e") + | value >= 0.5 -> pure (P.pretty "\xf0089") + | value >= 0.4 -> pure (P.pretty "\xf089d") + | value >= 0.3 -> pure (P.pretty "\xf0088") + | value >= 0.2 -> pure (P.pretty "\xf0087") + | value >= 0.1 -> pure (P.pretty "\xf0086") + | otherwise -> pure (P.pretty "\xf089c") + else + if + | value >= 0.9 -> pure (P.pretty "\xf0082") + | value >= 0.8 -> pure (P.pretty "\xf0081") + | value >= 0.7 -> pure (P.pretty "\xf0080") + | value >= 0.6 -> pure (P.pretty "\xf008f") + | value >= 0.5 -> pure (P.pretty "\xf008e") + | value >= 0.4 -> pure (P.pretty "\xf007d") + | value >= 0.3 -> pure (P.pretty "\xf007c") + | value >= 0.2 -> pure (P.pretty "\xf007b") + | value >= 0.1 -> pure (P.pretty "\xf007a") + | otherwise -> + (\b -> if b then P.pretty "\xf008e" else (P.color P.Red) (P.pretty "\xf0083")) + <$> blink data Blink = Blink deriving (Show) -- cgit v1.2.3