Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#237] Modify coloring options #268

Merged
merged 1 commit into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think this format is just the best ))

Copy link
Contributor Author

@aeqz aeqz Jan 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah 😆, as you may have seen in the commit history, I was trying to store this in a textual representation of the output's binary data, but encountered a lot of problems, mainly:

  • Different behaviour, parameters and output on macOS and linux commands.
  • Different Windows newline character.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ugh, that sounds like a sufferring 😋

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.