Skip to content

Allow/Default use of ANSI color codes on Windows #3003

@Ext3h

Description

@Ext3h

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions