module Hi3Status.Blocks.Weather (
WeatherBlock (..)
) where
import Hi3Status.Block
import Hi3Status.Block.Util
import System.Process
import System.Exit
import Data.List
import qualified Data.Text as T
import Control.Monad.IO.Class
data Conditions = ClearDay | ClearNight | Rain | Cloudy
data WeatherBlock = WeatherBlock {
format :: String,
clearDayText :: String,
clearNightText :: String,
rainText :: String,
cloudyText :: String,
celsius :: Bool,
location :: String,
latitude :: String,
longitude :: String
}
weather :: String -> Bool -> String -> String -> IO (Conditions, Int)
weather loc celsius long lat = do
w <- readProcess "weather" (if celsius then ["-m",loc] else [loc]) ""
temp <- readProcess "grep" ["emperature"] w >>= readProcess "awk" ["{print $2}"]
con <- if "cloud" `isInfixOf` w then return Cloudy else if "rain" `isInfixOf` w then return Rain
else do
(daynight,_,_) <- readProcessWithExitCode "sunwait" ["poll", long, lat] ""
case daynight of
ExitFailure 3 -> return ClearNight
otherwise -> return ClearDay
return (con,read temp)
instance Block WeatherBlock where
runBlock b = periodic_ 1000000000 $ do
(c,t) <- liftIO $ weather (location b) (celsius b) (longitude b) (latitude b)
let icon = case c of
ClearDay -> clearDayText b
ClearNight -> clearNightText b
Rain -> rainText b
Cloudy -> cloudyText b
pushBlockDescription $ emptyBlockDescription { full_text = formatText [("icon",icon),("temp", show t)] (format b) }