summaryrefslogtreecommitdiff
path: root/src/Propellor/Message.hs
blob: 780471c36fce89d29662d4682defb29ccdafc13e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{-# LANGUAGE PackageImports #-}

module Propellor.Message where

import System.Console.ANSI
import System.IO
import System.Log.Logger
import "mtl" Control.Monad.Reader

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
actionMessage desc a = do
	liftIO $ do
		setTitle $ "propellor: " ++ desc
		hFlush stdout

	r <- a

	liftIO $ do
		setTitle "propellor: running"
		let (msg, intensity, color) = getActionResult r
		putStr $ desc ++ " ... "
		colorLine intensity color msg
		hFlush stdout

	return r

warningMessage :: MonadIO m => String -> m ()
warningMessage s = liftIO $ colorLine Vivid Magenta $ "** warning: " ++ s

colorLine :: ColorIntensity -> Color -> String -> IO ()
colorLine intensity color msg = do
	setSGR [SetColor Foreground intensity color]
	putStr msg
	setSGR []
	-- Note this comes after the color is reset, so that
	-- the color set and reset happen in the same line.
	putStrLn ""
	hFlush stdout

errorMessage :: String -> IO a
errorMessage s = do
	liftIO $ colorLine Vivid Red $ "** error: " ++ s
	error "Cannot continue!"

-- | Causes a debug message to be displayed when PROPELLOR_DEBUG=1
debug :: [String] -> IO ()
debug = debugM "propellor" . unwords