module Hi3Status.Block.Util (
onUpdate,
periodic,
periodic_,
formatText,
maxLengthText
) where
import Hi3Status.Block
import Control.Concurrent
import Control.Monad.IO.Class
import qualified Data.Text as T
onUpdate :: BlockM () -> BlockM ()
onUpdate blockm = do
waitForUpdateSignal
blockm
onUpdate blockm
periodic :: Int -> BlockM () -> BlockM ()
periodic t blockm = do
u <- getUpdater
liftIO $ forkIO $ timer u
go
where
go = do
blockm
waitForUpdateSignal
go
timer u = do
threadDelay t
u
timer u
periodic_ :: Int -> BlockM () -> BlockM ()
periodic_ t blockm = do
blockm
liftIO $ threadDelay t
periodic_ t blockm
formatText :: [(String, String)] -> String -> T.Text
formatText subs format = foldl (flip ($)) (T.pack format)
$ map makeFormatter subs
where
makeFormatter (t,s) = T.replace (T.concat ["{",T.pack t,"}"]) (T.pack s)
maxLengthText :: String -> Maybe Int -> String
maxLengthText t ml = case ml of
Nothing -> t
Just n -> (take n t) ++ (if n < length t then "..." else "")