From 78bcb8c73ffb1b09b379ea0bbd7e68f2d84e187e Mon Sep 17 00:00:00 2001 From: jao Date: Sun, 27 Jan 2019 23:11:26 +0000 Subject: Monitor combiners (eventually to xmobar) --- src/lib/Monitors.hs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src') diff --git a/src/lib/Monitors.hs b/src/lib/Monitors.hs index 15900e5..fb51893 100644 --- a/src/lib/Monitors.hs +++ b/src/lib/Monitors.hs @@ -2,6 +2,39 @@ module Monitors where import Xmobar import Config +import Control.Concurrent +import Control.Concurrent.Async (async) +import Control.Concurrent.STM + +data CombinedMonitor a b = CombinedMonitor a b (String -> String -> String) + +instance (Show a, Show b) => Show (CombinedMonitor a b) where + show (CombinedMonitor a b _) = "Alt (" ++ show a ++ ") (" ++ show b ++ ")" + +instance (Read a, Read b) => Read (CombinedMonitor a b) where + readsPrec _ = undefined + +instance (Exec a, Exec b) => Exec (CombinedMonitor a b) where + alias (CombinedMonitor a b _) = (alias a) ++ "_" ++ (alias b) + rate (CombinedMonitor a b _) = min (rate a) (rate b) + start (CombinedMonitor a b comb) cb + = startMonitors a b (\s t -> cb $ comb s t) + +startMonitors a b cmb = do + sta <- atomically $ newTVar "" + stb <- atomically $ newTVar "" + _ <- async $ start a (\x -> atomically $ writeTVar sta x) + _ <- async $ start b (\x -> atomically $ writeTVar stb x) + go sta stb cmb + where go sta' stb' cmb' = do + s <- atomically $ readTVar sta' + t <- atomically $ readTVar stb' + cmb' s t + tenthSeconds $ min (rate b) (rate a) + go sta' stb' cmb' + +altMonitor a b = CombinedMonitor a b (\s t -> if null s then t else s) +concatMonitor sep a b = CombinedMonitor a b (\s t -> s ++ sep ++ t) topProc p = TopProc (p <~> ["-t" , " \ \ยท " -- cgit v1.2.3