-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
Description
With a little snippet during initialization, Windows 10 and newer support ANSI color codes out of the box:
#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 <io.h>
instead of #include <unistd.h>
) - 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.