propellor/Propellor/Message.hs

52 lines
1.2 KiB
Haskell
Raw Normal View History

{-# LANGUAGE PackageImports #-}
2014-03-31 22:31:08 +00:00
module Propellor.Message where
import System.Console.ANSI
import System.IO
2014-04-01 15:59:48 +00:00
import System.Log.Logger
import "mtl" Control.Monad.Reader
2014-03-31 22:31:08 +00:00
import Propellor.Types
-- | Shows a message while performing an action, with a colored status
-- display.
actionMessage :: (MonadIO m, ActionResult r) => Desc -> m r -> m r
2014-03-31 22:31:08 +00:00
actionMessage desc a = do
liftIO $ do
setTitle $ "propellor: " ++ desc
hFlush stdout
2014-03-31 22:31:08 +00:00
r <- a
liftIO $ do
setTitle "propellor: running"
let (msg, intensity, color) = getActionResult r
putStr $ desc ++ " ... "
colorLine intensity color msg
hFlush stdout
2014-03-31 22:31:08 +00:00
return r
warningMessage :: MonadIO m => String -> m ()
warningMessage s = liftIO $ colorLine Vivid Red $ "** warning: " ++ s
2014-04-04 04:18:51 +00:00
colorLine :: ColorIntensity -> Color -> String -> IO ()
colorLine intensity color msg = do
setSGR [SetColor Foreground intensity color]
putStr msg
2014-03-31 22:31:08 +00:00
setSGR []
2014-04-04 04:18:51 +00:00
-- Note this comes after the color is reset, so that
-- the color set and reset happen in the same line.
putStrLn ""
2014-03-31 22:31:08 +00:00
hFlush stdout
errorMessage :: String -> IO a
errorMessage s = do
warningMessage s
2014-04-03 17:49:26 +00:00
error "Cannot continue!"
2014-04-01 15:59:48 +00:00
-- | Causes a debug message to be displayed when PROPELLOR_DEBUG=1
debug :: [String] -> IO ()
debug = debugM "propellor" . unwords