Skip to content

Instantly share code, notes, and snippets.

@fleroviux
Created March 19, 2018 14:24
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fleroviux/8343879d95a72140274535dc207f467d to your computer and use it in GitHub Desktop.
Save fleroviux/8343879d95a72140274535dc207f467d to your computer and use it in GitHub Desktop.
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;
}
@alexeygrigorev
Copy link

What to do with this code?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment