on master help sign in
Disabled log colors when redirecting

.. when not on an interactive terminal, or in shell completion.

commita90cc28bc94767dca2792fa74a5c4f548f095bca authorSridhar Ratnakumar <srid@srid.ca> dateSun 13 May 2018, 14:06 (8 years ago) parent22f0e5c
3 files changed+22−22
lib/command/src/Obelisk/CLI.hs+5−5 lib/command/src/Obelisk/CLI/Logging.hs+14−14 lib/command/src/Obelisk/Command.hs+3−3
lib/command/src/Obelisk/CLI.hs+5−5
@@ -26,15 +26,15 @@ import Obelisk.App (MonadObelisk, _obelisk_logging, _obelisk_noSpinner)
2626-- Obelisk, and can be made a library of its own.
2727
2828withSpinner :: MonadObelisk m => Text -> m a -> m a
29−withSpinner s a = do
29+withSpinner s action = do
3030 conf <- ask
3131 case _obelisk_noSpinner conf of
32− True -> a
33− False -> withSpinner' (_obelisk_logging conf) s a
32+ True -> putLog Notice s >> action
33+ False -> withSpinner' (_obelisk_logging conf) s action
3434
3535cliDemo :: MonadObelisk m => m ()
3636cliDemo = do
37− putLog Notice "We will now log a warning, and then start a spinner"
37+ putLog Notice "This demo will showcase the CLI library functionality"
3838 withSpinner "Running some long-running task" $ do
3939 liftIO $ threadDelay 1000000
4040 putLog Error "Some user error while spinning"
@@ -48,6 +48,6 @@ cliDemo = do
4848 output <- readProcessAndLogStderr Notice $ proc "ls" ["-l", "/"]
4949 putLog Notice $ "Output was: " <> T.pack output
5050 liftIO $ threadDelay 1000000
51− _ <- readProcessAndLogStderr Notice $ proc "ls" ["-l", "/does-not-exist"]
51+ callProcessAndLogOutput Notice $ proc "ls" ["-l", "/does-not-exist"]
5252 liftIO $ threadDelay 1000000
5353 failWith "Something dangerous happened"
lib/command/src/Obelisk/CLI/Logging.hs+14−14
@@ -29,6 +29,7 @@ import Control.Monad.Log (MonadLog, Severity (..), WithSeverity (..), logMessage
2929
3030data LoggingConfig = LoggingConfig
3131 { _loggingConfig_level :: Severity
32+ , _loggingConfig_noColor :: Bool -- Disallow coloured output
3233 , _loggingConfig_lock :: MVar Bool -- Whether the last message was an Overwrite output
3334 }
3435
@@ -51,25 +52,24 @@ getSeverity = \case
5152 _ -> Nothing
5253
5354handleLog :: MonadIO m => LoggingConfig -> Output -> m ()
54−handleLog c output = do
55− let lock = _loggingConfig_lock c
55+handleLog (LoggingConfig level noColor lock) output = do
5656 liftIO $ modifyMVar_ lock $ \wasOverwriting -> do
5757 case getSeverity output of
58− Nothing -> handleLog' output
58+ Nothing -> handleLog' noColor output
5959 Just sev -> do
6060 -- Discard unless severity is above configured log level
61− unless (sev > _loggingConfig_level c) $ do
61+ unless (sev > level) $ do
6262 -- If the last output was an overwrite (with cursor on same line), first clear it,
63− when wasOverwriting $ handleLog' Output_ClearLine
63+ when wasOverwriting $ handleLog' noColor Output_ClearLine
6464 -- ... then actually write the log.
65− handleLog' output
65+ handleLog' noColor output
6666 return $ isOverwrite output
6767
68−handleLog' :: MonadIO m => Output -> m ()
69−handleLog' = \case
70− Output_Log m -> liftIO $ writeLogWith T.putStrLn m
68+handleLog' :: MonadIO m => Bool -> Output -> m ()
69+handleLog' noColor = \case
70+ Output_Log m -> liftIO $ writeLogWith T.putStrLn noColor m
7171 Output_LogRaw m -> liftIO $ do
72− writeLogWith T.putStr m
72+ writeLogWith T.putStr noColor m
7373 hFlush stdout
7474 Output_Overwrite xs -> liftIO $ putStrs $ "\r" : xs
7575 Output_ClearLine -> liftIO $ do
@@ -95,16 +95,16 @@ failWith s = do
9595 putLog Alert s
9696 liftIO $ exitWith $ ExitFailure 2
9797
98−-- | Write log to stdout with colors
99−-- TODO: Disable colours when not on interactive terminal
100−writeLogWith :: (MonadIO m, MonadMask m) => (Text -> IO ()) -> WithSeverity Text -> m ()
101−writeLogWith f (WithSeverity severity s) = case sevColor severity of
98+-- | Write log to stdout, with colors (unless `noColor`)
99+writeLogWith :: (MonadIO m, MonadMask m) => (Text -> IO ()) -> Bool -> WithSeverity Text -> m ()
100+writeLogWith f noColor (WithSeverity severity s) = case sevColor severity of
102101 Just setColor -> bracket_ setColor reset $ liftIO (T.putStr s)
103102 Nothing -> liftIO $ f s
104103 where
105104 -- We must reset *before* the newline, else the cursor won't be reset.
106105 reset = liftIO $ setSGR [Reset] >> f ""
107106 sevColor sev = if
107+ | noColor -> Nothing
108108 | sev <= Error -> Just $ liftIO $ setSGR [SetColor Foreground Vivid Red]
109109 | sev <= Warning -> Just $ liftIO $ setSGR [SetColor Foreground Vivid Yellow]
110110 | otherwise -> Nothing
lib/command/src/Obelisk/Command.hs+3−3
@@ -188,9 +188,9 @@ main = do
188188 -- We are not passing `logLevel` as cli argument (--verbose) because run-static-io does not
189189 -- pass along the cli arguments to recursive obelisks.
190190 logLevel <- maybe Notice read <$> lookupEnv "LOGLEVEL"
191− noSpinner <- not <$> isInteractiveTerm
192− loggingConfig <- LoggingConfig logLevel <$> newMVar False
193− let obeliskConfig = Obelisk noSpinner loggingConfig
191+ notInteractive <- not <$> isInteractiveTerm
192+ loggingConfig <- LoggingConfig logLevel notInteractive <$> newMVar False
193+ let obeliskConfig = Obelisk notInteractive loggingConfig
194194 runObelisk obeliskConfig $ ObeliskT main'
195195
196196main' :: MonadObelisk m => m ()