Created
March 19, 2018 14:24
-
-
Save fleroviux/8343879d95a72140274535dc207f467d to your computer and use it in GitHub Desktop.
MinGW 32/64 enable ANSI color sequences on Windows 10
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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
What to do with this code?