module System.Taffybar.CommandRunner ( commandRunnerNew ) where
import qualified Graphics.UI.Gtk as Gtk
import System.Taffybar.Pager (colorize)
import System.Taffybar.Widgets.PollingLabel
import Control.Monad
import System.Exit (ExitCode (..))
import qualified System.IO as IO
import qualified System.Process as P
commandRunnerNew :: Double
-> String
-> [String]
-> String
-> String
-> IO Gtk.Widget
commandRunnerNew interval cmd args defaultOutput color = do
label <- pollingLabelNew "" interval $ runCommand cmd args defaultOutput color
Gtk.widgetShowAll label
return $ Gtk.toWidget label
runCommand :: FilePath -> [String] -> String -> String -> IO String
runCommand cmd args defaultOutput color = do
(ecode, stdout, stderr) <- P.readProcessWithExitCode cmd args ""
unless (null stderr) $ do
IO.hPutStrLn IO.stderr stderr
return $ colorize color "" $ case ecode of
ExitSuccess -> stdout
ExitFailure _ -> defaultOutput