Skip to content

Commit

Permalink
Merge pull request #268 from serokell/aeqz/#237-modify-coloring-options
Browse files Browse the repository at this point in the history
[#237] Modify coloring options
  • Loading branch information
aeqz authored Jan 23, 2023
2 parents 999518b + 29c1ab1 commit 747c884
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
==========
Expand Down
16 changes: 12 additions & 4 deletions src/Xrefcheck/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 $
Expand Down
9 changes: 7 additions & 2 deletions src/Xrefcheck/Command.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -64,7 +70,6 @@ defaultAction Options{..} = do
|]
pure $ defConfig GitHub

withinCI <- askWithinCI
let showProgressBar = oShowProgressBar ?: not withinCI

(ScanResult scanErrs repoInfo) <- allowRewrite showProgressBar $ \rw -> do
Expand Down
34 changes: 34 additions & 0 deletions tests/golden/check-color/check-color.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env bats

# SPDX-FileCopyrightText: 2023 Serokell <https://serokell.io>
#
# 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
}
9 changes: 9 additions & 0 deletions tests/golden/check-color/color.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<!--
- SPDX-FileCopyrightText: 2023 Serokell <https://serokell.io>
-
- SPDX-License-Identifier: MPL-2.0
-->

# Color

[Color](#Color)
12 changes: 12 additions & 0 deletions tests/golden/check-color/expected-color.gold
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 12 additions & 0 deletions tests/golden/check-color/expected-no-color.gold
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit 747c884

Please sign in to comment.