catchorg / catchorg/Catch2

Allow/Default use of ANSI color codes on Windows

Open
#3,003 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
C++
Stars
21.5k
Forks
3.5k
Avg merge
3d 16h
Merged PRs (30d)
2

Description

**Description**
With a little snippet during initialization, Windows 10 and newer support ANSI color codes out of the box:

```c++
#ifdef _WIN32
HANDLE outputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD mode = 0;
if (GetConsoleMode(outputHandle, &mode))
{
mode |= ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(outputHandle, mode);
}
#endif // _WIN32
```

The `CATCH_INTERNAL_HAS_ISATTY` is somewhat malformed - Windows has `isatty` too (in header `#include ` instead of `#include `) - but what it doesn't have is `STDOUT_FILENO` so the portable solution for that is `isatty(fileno(stdout))`.

On Windows 10 or newer (or in case that covers ALL support Windows platforms: For every Windows-platform in general), the ANSI color code support should be preferred unconditionally over the legacy text attribute API.

**Additional context**
This avoids a lot of issues you'd otherwise have with the Windows terminal handling - among others that setting colors via `SetConsoleTextAttribute` on Windows breaks horribly when the application under test is enabling output buffering for `stdout` as the legacy API for color codes only every works as long as `stdout` is byte-buffered only.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.