snitch-org / snitch

Lightweight C++20 testing framework.
Boost Software License 1.0
259 stars 7 forks source link

ANSI escape sequences are displayed in Windows console #147

Closed vid512 closed 10 months ago

vid512 commented 10 months ago

Snitch test runner seems to use ANSI escape sequence by default, unless '--color never' is passed. These don't work properly on Windows console:

←[1;36mTest runner (snitch v1.1.1.e557246)←[0m
←[1;32mUsage:←[0m
  tprotect-test [options...] [<test regex>...]

  ←[1;35m-l, --list-tests←[0m List tests by name
  ←[1;35m    --list-tags←[0m List tags by name
  ←[1;35m    --list-tests-with-tag <tag>←[0m List tests by name with a given tag
  ←[1;35m-v, --verbosity <quiet|normal|high|full>←[0m Define how much gets sent to the standard output
  ←[1;35m    --color <always|never>←[0m Enable/disable color in output
  ←[1;35m-h, --help←[0m Print help
  ←[1;35m<test regex>←[0m A regex to select which test cases to run

According to https://stackoverflow.com/a/7445778 , there's no generic way to detect console capabilities. However, default Windows console (cmd.exe) seems to me as a common enough case, to warrant a specific check, and disable ANSI escape sequences automatically.

PS: '--color never' seems to be ignored, when displaying '--help' text. So there's no way to display help text without ANSI escape sequences.

cschreib commented 10 months ago

Have you considered using this build option? https://github.com/snitch-org/snitch/blob/286b5cbd2983326305eaa09155de08c1c0ca583f/CMakeLists.txt#L25

I just realised it isn't documented in the readme... It isn't perfect, but it does allow disabling colors globally (including for --help) if your output target typically doesn't support ANSI codes. We can't base this on OS only, i.e. disable for Windows, since it is perfectly possible to use a terminal emulator that supports ANSI codes on Windows (e.g., Windows Terminal, or MSYS); this is mostly a limitation of the old command prompt.

vid512 commented 10 months ago

Nope, I didn't know about it. For my specific case it's fine.

I wouldn't disable ANSI codes on Windows either. In most circumstances they work fine (also in VSCode console). I was under impression there is a way to query the console type in a simple way, and if it is old cmd.exe then disable ANSI. Just as a convenience for better behavior in common use case.

I would still consider the '--colors never --help' a minor bug, though.

cschreib commented 10 months ago

I was under impression there is a way to query the console type in a simple way, and if it is old cmd.exe then disable ANSI.

We could have Windows API calls to detect this specific case, I suppose. The issue becomes where to draw the line; do we also try to detect non-TTY on Linux and disable colors there too? What about other platforms, etc. I think the original intent was to have a generic set of toggles to avoid platform-specific behavior.

I would still consider the '--colors never --help' a minor bug, though.

Indeed, and documenting the build-time options would also be good.

cschreib commented 10 months ago

Should be addressed in #148.