From 542e518ddd09ad9e4b44f17185d97b9f5ee943f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Wed, 31 Jul 2024 21:14:40 +0200 Subject: Enable color only for terminal output, add manual options Changelog: Use colors by default only on terminal, add `--color`/`--no-color` options to select manually. --- src/Output.hs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'src/Output.hs') diff --git a/src/Output.hs b/src/Output.hs index 96bb965..135e6e0 100644 --- a/src/Output.hs +++ b/src/Output.hs @@ -27,6 +27,7 @@ data Output = Output data OutputConfig = OutputConfig { outVerbose :: Bool + , outUseColor :: Bool } data OutputState = OutputState @@ -50,10 +51,10 @@ class MonadIO m => MonadOutput m where instance MonadIO m => MonadOutput (ReaderT Output m) where getOutput = ask -startOutput :: Bool -> IO Output -startOutput verbose = Output +startOutput :: Bool -> Bool -> IO Output +startOutput outVerbose outUseColor = Output <$> newMVar OutputState { outPrint = TL.putStrLn, outHistory = emptyHistory } - <*> pure OutputConfig { outVerbose = verbose } + <*> pure OutputConfig { .. } outColor :: OutputType -> Text outColor OutputChildStdout = T.pack "0" @@ -97,11 +98,15 @@ outLine :: MonadOutput m => OutputType -> Maybe Text -> Text -> m () outLine otype prompt line = ioWithOutput $ \out -> when (outVerbose (outConfig out) || printWhenQuiet otype) $ do withMVar (outState out) $ \st -> do - outPrint st $ TL.fromChunks - [ T.pack "\ESC[", outColor otype, T.pack "m" - , maybe "" (<> outSign otype <> outArr otype <> " ") prompt - , line - , T.pack "\ESC[0m" + outPrint st $ TL.fromChunks $ concat + [ if outUseColor (outConfig out) + then [ T.pack "\ESC[", outColor otype, T.pack "m" ] + else [] + , [ maybe "" (<> outSign otype <> outArr otype <> " ") prompt ] + , [ line ] + , if outUseColor (outConfig out) + then [ T.pack "\ESC[0m" ] + else [] ] outPromptGetLine :: MonadOutput m => Text -> m (Maybe Text) -- cgit v1.2.3