summaryrefslogtreecommitdiff
path: root/src/Propellor/Message.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@debian.org>2014-07-09 22:11:31 -0400
committerJoey Hess <joeyh@debian.org>2014-07-09 22:11:31 -0400
commit82da31b3e0e9acdfbca4c48eb12ab1f28515ba10 (patch)
tree0a3e0c6e134680e35665364b2cd6895863bcc990 /src/Propellor/Message.hs
propellor (0.8.1) unstable; urgency=medium
* Run apt-get update in initial bootstrap. * --list-fields now includes a table of fields that are not currently set, but would be used if they got set. * Remove .gitignore from cabal file list, to avoid build failure on Debian. Closes: #754334 # imported from the archive
Diffstat (limited to 'src/Propellor/Message.hs')
-rw-r--r--src/Propellor/Message.hs66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/Propellor/Message.hs b/src/Propellor/Message.hs
new file mode 100644
index 00000000..afbed1ca
--- /dev/null
+++ b/src/Propellor/Message.hs
@@ -0,0 +1,66 @@
+{-# 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 = actionMessage' Nothing
+
+-- | Shows a message while performing an action on a specified host,
+-- with a colored status display.
+actionMessageOn :: (MonadIO m, ActionResult r) => HostName -> Desc -> m r -> m r
+actionMessageOn = actionMessage' . Just
+
+actionMessage' :: (MonadIO m, ActionResult r) => Maybe HostName -> Desc -> m r -> m r
+actionMessage' mhn desc a = do
+ liftIO $ do
+ setTitle $ "propellor: " ++ desc
+ hFlush stdout
+
+ r <- a
+
+ liftIO $ do
+ setTitle "propellor: running"
+ showhn mhn
+ putStr $ desc ++ " ... "
+ let (msg, intensity, color) = getActionResult r
+ colorLine intensity color msg
+ hFlush stdout
+
+ return r
+ where
+ showhn Nothing = return ()
+ showhn (Just hn) = do
+ setSGR [SetColor Foreground Dull Cyan]
+ putStr (hn ++ " ")
+ setSGR []
+
+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