summaryrefslogtreecommitdiff
path: root/src/Main.hs
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2024-07-31 21:14:40 +0200
committerRoman Smrž <roman.smrz@seznam.cz>2024-07-31 21:14:40 +0200
commit542e518ddd09ad9e4b44f17185d97b9f5ee943f1 (patch)
tree7fa6aaf71b4299729a96a58933be23c929bbbe09 /src/Main.hs
parent28a0bc8c32d5e68f1a2ede45e8407a5f2f3acc64 (diff)
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.
Diffstat (limited to 'src/Main.hs')
-rw-r--r--src/Main.hs15
1 files changed, 14 insertions, 1 deletions
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