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 :: IO MConfig
thermalZoneConfig = String -> [String] -> IO MConfig
mkMConfig String
"<temp>C" [String
"temp"]
runThermalZone :: [String] -> Monitor String
runThermalZone :: [String] -> Monitor String
runThermalZone [String]
args = do
let zone :: String
zone = forall a. [a] -> a
head [String]
args
file :: String
file = String
"/sys/class/thermal/thermal_zone" forall a. [a] -> [a] -> [a]
++ String
zone forall a. [a] -> [a] -> [a]
++ String
"/temp"
handleIOError :: IOException -> IO (Maybe B.ByteString)
handleIOError :: IOException -> IO (Maybe ByteString)
handleIOError IOException
_ = forall (m :: * -> *) a. Monad m => a -> m a
return forall a. Maybe a
Nothing
parse :: ByteString -> ReaderT MConfig IO Int
parse = forall (m :: * -> *) a. Monad m => a -> m a
return forall b c a. (b -> c) -> (a -> b) -> a -> c
. (forall a. Read a => String -> a
read :: String -> Int) forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> String
B.unpack
Bool
exists <- forall a. IO a -> Monitor a
io forall a b. (a -> b) -> a -> b
$ String -> IO Bool
fileExist String
file
if Bool
exists
then do Maybe ByteString
contents <- forall a. IO a -> Monitor a
io forall a b. (a -> b) -> a -> b
$ forall e a. Exception e => IO a -> (e -> IO a) -> IO a
catch (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. a -> Maybe a
Just forall a b. (a -> b) -> a -> b
$ String -> IO ByteString
B.readFile String
file) IOException -> IO (Maybe ByteString)
handleIOError
case Maybe ByteString
contents of
Just ByteString
d -> do
Int
mdegrees <- ByteString -> ReaderT MConfig IO Int
parse ByteString
d
String
temp <- forall a. (Num a, Ord a) => (a -> String) -> a -> Monitor String
showWithColors forall a. Show a => a -> String
show (Int
mdegrees forall a. Integral a => a -> a -> a
`quot` Int
1000)
[String] -> Monitor String
parseTemplate [ String
temp ]
Maybe ByteString
Nothing -> forall a. Selector a -> Monitor a
getConfigValue MConfig -> IORef String
naString
else forall a. Selector a -> Monitor a
getConfigValue MConfig -> IORef String
naString