propellor/Propellor/Message.hs

47 lines
1.1 KiB
Haskell
Raw Normal View History

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
2014-03-31 22:31:08 +00:00
import Propellor.Types
-- | Shows a message while performing an action, with a colored status
-- display.
actionMessage :: ActionResult r => Desc -> IO r -> IO r
actionMessage desc a = do
2014-03-31 23:31:35 +00:00
setTitle $ "propellor: " ++ desc
2014-03-31 22:31:08 +00:00
hFlush stdout
r <- a
2014-04-04 04:18:51 +00:00
setTitle "propellor: running"
2014-03-31 22:31:08 +00:00
let (msg, intensity, color) = getActionResult r
2014-03-31 22:36:53 +00:00
putStr $ desc ++ " ... "
2014-04-04 04:18:51 +00:00
colorLine intensity color msg
2014-03-31 22:31:08 +00:00
hFlush stdout
return r
warningMessage :: String -> IO ()
2014-04-04 04:18:51 +00:00
warningMessage s = colorLine Vivid Red $ "** warning: " ++ s
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