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/Main.hs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs index f296a12..594de0c 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -14,6 +14,8 @@ import System.Exit import System.FilePath import System.FilePath.Glob import System.IO +import System.Posix.Terminal +import System.Posix.Types import Config import Output @@ -28,6 +30,7 @@ data CmdlineOptions = CmdlineOptions { optTest :: TestOptions , optRepeat :: Int , optVerbose :: Bool + , optColor :: Maybe Bool , optShowHelp :: Bool , optShowVersion :: Bool } @@ -37,6 +40,7 @@ defaultCmdlineOptions = CmdlineOptions { optTest = defaultTestOptions , optRepeat = 1 , optVerbose = False + , optColor = Nothing , optShowHelp = False , optShowVersion = False } @@ -52,6 +56,12 @@ options = , Option ['v'] ["verbose"] (NoArg (\opts -> opts { optVerbose = True })) "show output of processes and successful tests" + , Option [] [ "color" ] + (NoArg (\opts -> opts { optColor = Just True })) + "always use colors for output (default when stdout is tty)" + , Option [] [ "no-color" ] + (NoArg (\opts -> opts { optColor = Just False })) + "never use colors for output (default when stdout is not a tty)" , Option ['t'] ["timeout"] (ReqArg (\str -> to $ \opts -> case readMaybe str of Just timeout -> opts { optTimeout = timeout } @@ -133,7 +143,10 @@ main = do when (null files) $ fail $ "No test files" - out <- startOutput $ optVerbose opts + useColor <- case optColor opts of + Just use -> return use + Nothing -> queryTerminal (Fd 1) + out <- startOutput (optVerbose opts) useColor tests <- forM files $ \(path, mbTestName) -> do fileTests <- parseTestFile path -- cgit v1.2.3