diff --git a/CHANGES.md b/CHANGES.md index c96b3e91..0c41e2ea 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -49,6 +49,9 @@ Unreleased configuration file with the `externalRefRedirects` parameter. * [#261](https://github.com/serokell/xrefcheck/pull/261) + Symlinks are now not processed by the scanner. +* [#268](https://github.com/serokell/xrefcheck/pull/268) + + Added CLI option `--color` that enables ANSI colors in output. + + Changed the output coloring defaults to show colors when `CI` env variable is `true`. 0.2.2 ========== diff --git a/src/Xrefcheck/CLI.hs b/src/Xrefcheck/CLI.hs index e9051020..4c26b18f 100644 --- a/src/Xrefcheck/CLI.hs +++ b/src/Xrefcheck/CLI.hs @@ -83,7 +83,7 @@ data Options = Options , oMode :: VerifyMode , oVerbose :: Bool , oShowProgressBar :: Maybe Bool - , oColorMode :: ColorMode + , oColorMode :: Maybe ColorMode , oExclusionOptions :: ExclusionOptions , oNetworkingOptions :: NetworkingOptions , oScanPolicy :: ScanPolicy @@ -185,9 +185,17 @@ optionsParser = do help "Do not display progress bar during verification." , pure Nothing ] - oColorMode <- flag WithColors WithoutColors $ - long "no-color" <> - help "Disable ANSI coloring of output." + oColorMode <- asum + [ flag' (Just WithColors) $ + long "color" <> + help "Enable ANSI coloring of output. \ + \When `CI` env var is set to true or the command output corresponds to a terminal, \ + \this option will be enabled by default." + , flag' (Just WithoutColors) $ + long "no-color" <> + help "Disable ANSI coloring of output." + , pure Nothing + ] oExclusionOptions <- exclusionOptionsParser oNetworkingOptions <- networkingOptionsParser oScanPolicy <- flag OnlyTracked IncludeUntracked $ diff --git a/src/Xrefcheck/Command.hs b/src/Xrefcheck/Command.hs index 6dddd400..c255a0a2 100644 --- a/src/Xrefcheck/Command.hs +++ b/src/Xrefcheck/Command.hs @@ -48,8 +48,14 @@ findFirstExistingFile = \case defaultAction :: Options -> IO () defaultAction Options{..} = do + withinCI <- askWithinCI coloringSupported <- supportsPretty - give (if coloringSupported then oColorMode else WithoutColors) $ do + let colorMode = oColorMode ?: + if withinCI || coloringSupported + then WithColors + else WithoutColors + + give colorMode $ do config <- case oConfigPath of Just configPath -> readConfig configPath Nothing -> do @@ -64,7 +70,6 @@ defaultAction Options{..} = do |] pure $ defConfig GitHub - withinCI <- askWithinCI let showProgressBar = oShowProgressBar ?: not withinCI (ScanResult scanErrs repoInfo) <- allowRewrite showProgressBar $ \rw -> do diff --git a/tests/golden/check-color/check-color.bats b/tests/golden/check-color/check-color.bats new file mode 100644 index 00000000..16a22843 --- /dev/null +++ b/tests/golden/check-color/check-color.bats @@ -0,0 +1,34 @@ +#!/usr/bin/env bats + +# SPDX-FileCopyrightText: 2023 Serokell +# +# SPDX-License-Identifier: MPL-2.0 + +load '../helpers/bats-support/load' +load '../helpers/bats-assert/load' +load '../helpers/bats-file/load' +load '../helpers' + +# The CI variable must be always explicitly set in these tests because they are checking for an +# intended behavior regardless of where they are actually being run (e.g. "No color flag (not +# in CI)") may be running in CI). + +@test "Color flag (not in CI)" { + CI=false xrefcheck -v --no-progress --color | diff - expected-color.gold +} + +@test "No color flag (not in CI)" { + CI=false xrefcheck -v --no-progress --no-color | diff - expected-no-color.gold +} + +@test "No color default when pipe (not in CI)" { + CI=false xrefcheck -v --no-progress | diff - expected-no-color.gold +} + +@test "Color default when CI" { + CI=true xrefcheck -v --no-progress | diff - expected-color.gold +} + +@test "No color flag in CI" { + CI=true xrefcheck -v --no-progress --no-color | diff - expected-no-color.gold +} diff --git a/tests/golden/check-color/color.md b/tests/golden/check-color/color.md new file mode 100644 index 00000000..9bb3865d --- /dev/null +++ b/tests/golden/check-color/color.md @@ -0,0 +1,9 @@ + + +# Color + +[Color](#Color) diff --git a/tests/golden/check-color/expected-color.gold b/tests/golden/check-color/expected-color.gold new file mode 100644 index 00000000..ce59a0ca --- /dev/null +++ b/tests/golden/check-color/expected-color.gold @@ -0,0 +1,12 @@ +=== Repository data === + + color.md: + - references: + - reference (file-local) at src:9:1-15: + - text: "Color" + - link: - + - anchor: Color + - anchors: + - color (header I) at src:7:1-7 + +All repository links are valid. diff --git a/tests/golden/check-color/expected-no-color.gold b/tests/golden/check-color/expected-no-color.gold new file mode 100644 index 00000000..71627354 --- /dev/null +++ b/tests/golden/check-color/expected-no-color.gold @@ -0,0 +1,12 @@ +=== Repository data === + + color.md: + - references: + - reference (file-local) at src:9:1-15: + - text: "Color" + - link: - + - anchor: Color + - anchors: + - color (header I) at src:7:1-7 + +All repository links are valid.