Skip to content

Commit

Permalink
Support coloured terminal output on Windows
Browse files Browse the repository at this point in the history
First try the modern Windowsy way, where we can directly query if escape sequences will be processed.
The function is available as far back as Windows 2000, but it just won't return the right flag until the Windows version is new enough.

If that fails, fall back to the Unixy way, as not all colour-supporting terminal emulators for Windows use the Win32 API to declare that capability.
The implementation isn't identical as isatty wasn't available without adding more headers, and we already have Windows.h in this file, so I might as well use the Win32 API instead of its POSIX-compatibility layer.
  • Loading branch information
AnyOldName3 committed Apr 13, 2024
1 parent 55c5f21 commit 1930bfe
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions components/debug/debugging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,17 @@ namespace Debug
private:
static bool useColoredOutput()
{
// Note: cmd.exe in Win10 should support ANSI colors, but in its own way.
#if defined(_WIN32)
return 0;
if (getenv("NO_COLOR"))
return false;

DWORD mode;
if (GetConsoleMode(GetStdHandle(STD_ERROR_HANDLE), &mode) && mode & ENABLE_VIRTUAL_TERMINAL_PROCESSING)
return true;

// some console emulators may not use the Win32 API, so try the Unixy approach
char* term = getenv("TERM");
return term && GetFileType(GetStdHandle(STD_ERROR_HANDLE)) == FILE_TYPE_CHAR;
#else
char* term = getenv("TERM");
bool useColor = term && !getenv("NO_COLOR") && isatty(fileno(stderr));
Expand Down

0 comments on commit 1930bfe

Please sign in to comment.