module Hi3Status.Blocks.Command (
CommandBlock (..)
) where
import Hi3Status.Block
import Hi3Status.Block.Util
import qualified Data.Text as T
import System.Process
import GHC.IO.Handle
import Control.Monad.IO.Class
data CommandBlock = CommandBlock {
cmd :: String,
format :: String,
refresh :: Maybe Int
}
instance Block CommandBlock where
runBlock b = case refresh b of
Nothing -> onUpdate go
Just n -> periodic n go
where
go = do
(_, mhout, mherr, _) <- liftIO $ createProcess $ (shell (cmd b)) { std_out = CreatePipe, std_err = CreatePipe }
out <- maybe (return "") (liftIO . hGetContents) mhout
err <- maybe (return "") (liftIO . hGetContents) mherr
pushBlockDescription $ emptyBlockDescription
{ full_text = formatText [("stdout", concat . take 1 . lines $ out),("stderr", concat . take 1 . lines $ err)] (format b) }