module Monky.Examples.Battery
( getBatteryHandle
, getBatteryHandle'
, BatteryH
)
where
import Formatting
import Data.Text (Text)
import Data.Composition ((.:))
import Monky.Modules
import Monky.Examples.Images
import Monky.Battery hiding (getBatteryHandle, getBatteryHandle')
import qualified Monky.Battery as B (getBatteryHandle, getBatteryHandle')
batteryColor :: BatteryState -> Int -> Text
batteryColor BatLoading _ = "#5fff5f"
batteryColor _ p
| p < 5 = "#ff0000"
| p < 10 = "#ffaf00"
| p < 15 = "#ffd700"
| p < 20 = "#ffff00"
| otherwise = ""
batterySymbol :: BatteryState -> Int -> Text
batterySymbol BatLoading _ = "ac_01"
batterySymbol _ p
| p < 50 = "bat_low_01"
| p < 20 = "bat_empty_01"
| otherwise = "bat_full_01"
instance PollModule BatteryH where
getOutput (BH bh) = do
p <- getCurrentLevel bh
s <- getTimeLeft bh
online <- getCurrentStatus bh
pow <- getLoading bh
let h = s `div` 3600
m = (s `mod` 3600) `div` 60
return
[ batteryImage (batterySymbol online p)
, MonkyColor (batteryColor online p, "") $
MonkyPlain $ sformat ((left 4 ' ' %. fixed 1) % "W " % int % "% " % (left 2 ' ' %. int) % ":" % (left 2 '0' %. int)) pow p h m
]
newtype BatteryH = BH BatteryHandle
getBatteryHandle :: String
-> String
-> IO BatteryH
getBatteryHandle = fmap BH .: B.getBatteryHandle
getBatteryHandle' :: String -> IO BatteryH
getBatteryHandle' = fmap BH . B.getBatteryHandle'