Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MinGW 32/64 enable ANSI color sequences on Windows 10
// Windows 10 supports ANSI color code sequences, however they need to be enabled
// and apparently MinGW/MinGW-w64 doesn't support set the required STDOUT flag yet. This code enables ANSI VT processing
// on MinGW compilers however it hasn't been tested on older Windows systems yet which do not have that flag.
// So if you use this in production code you probably have to check if the platform is Windows 10.
#include <stdio.h>
#ifdef __MINGW32__
#define NEED_COLOR_FIX
#endif
#ifdef NEED_COLOR_FIX
#include <windows.h>
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
#define CON_RESET "\e[0m"
#define COLOR_GREEN "\e[32m"
int main(int argc, char** argv) {
#ifdef NEED_COLOR_FIX
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle != INVALID_HANDLE_VALUE) {
DWORD mode = 0;
if (GetConsoleMode(handle, &mode)) {
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(handle, mode);
}
}
#endif
puts(COLOR_GREEN "I am green text" CON_RESET);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.