module Xmobar.Plugins.Monitors.ThermalZone (thermalZoneConfig, runThermalZone) where
import Xmobar.Plugins.Monitors.Common
import System.Posix.Files (fileExist)
import Control.Exception (IOException, catch)
import qualified Data.ByteString.Char8 as B
thermalZoneConfig :: IO MConfig
thermalZoneConfig = mkMConfig "<temp>C" ["temp"]
runThermalZone :: [String] -> Monitor String
runThermalZone args = do
let zone = head args
file = "/sys/class/thermal/thermal_zone" ++ zone ++ "/temp"
handleIOError :: IOException -> IO (Maybe B.ByteString)
handleIOError _ = return Nothing
parse = return . (read :: String -> Int) . B.unpack
exists <- io $ fileExist file
if exists
then do contents <- io $ catch (fmap Just $ B.readFile file) handleIOError
case contents of
Just d -> do
mdegrees <- parse d
temp <- showWithColors show (mdegrees `quot` 1000)
parseTemplate [ temp ]
Nothing -> getConfigValue naString
else getConfigValue naString