ANSI colors in Windows terminal

In case you are wondering how to get your ANSI colored strings into windows terminal, the following snippet might help you (from C):

#include <stdio.h>
#include <windows.h>

#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
# define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif

int main(int argc, char** argv) {
  HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
  DWORD dwMode = 0;

  GetConsoleMode(hOut, &dwMode);
  dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;

  SetConsoleMode(hOut, dwMode);

  printf("\x1b[101;93m Testing\x1b[0m\n");
  return 0;
}

More info on escape sequences can be found on https://en.wikipedia.org/wiki/ANSI_escape_code

Leave a Reply

Your email address will not be published. Required fields are marked *