ANSI Colors in Command Prompt

ANSI Colors in Command Prompt

{fmt} has this nice feature allowing you to use colors for your output via ANSI escape code.  Too bad when I tried to run it under Windows the first time all I saw was the escape code and no colors in Command Prompt.

Enable Virtual Terminal Processing

This article explains how to make it work.  It comes down to just a few lines of code:

DWORD dwMode = 0;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut != INVALID_HANDLE_VALUE) {
	if (GetConsoleMode(hOut, &dwMode)) {
		SetConsoleMode(hOut, dwMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING);
	}
}

And you can use the dwMode variable to restore the console mode once you are done.