I read about ANSI-C escape codes here. Tried to use it in C/C++ printf/cout to colorize the text outputted to consolde but without sucess.
Code:
#include <iostream>
#include <cstdio>
int main()
{
int a=3, b=5;
int &ref = a;
ref = b;
//cout << "\155\32\m" << a << b <<'\n'; //here it prints m→m 5, no colored text
printf("\155\32\m %d",a); //here to it prints same - m→m 5,
getchar();
}
How to use these escape codes to output colored text to console?
Am i missing something?
EDIT: In some C++ code I saw a call to this function
textcolor(10);
But it gives compilation errors in g++ and in Visual Studio. Which compiler had this function available? Any details?
A note to anybody reading this post: https://en.wikipedia.org/wiki/ANSI_escape_code#DOS_and_Windows
In 2016, Microsoft released the Windows 10 Version 1511 update which unexpectedly implemented support for ANSI escape sequences. The change was designed to complement the Windows Subsystem for Linux, adding to the Windows Console Host used by Command Prompt support for character escape codes used by terminal-based software for Unix-like systems. This is not the default behavior and must be enabled programmatically with the Win32 API via
SetConsoleMode(handle, ENABLE_VIRTUAL_TERMINAL_PROCESSING)
under windows 10 one can use VT100 style by activating the VT100 mode in the current console :
#include <windows.h>
#include <iostream>
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#define DISABLE_NEWLINE_AUTO_RETURN 0x0008
int main()
{
HANDLE handleOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD consoleMode;
GetConsoleMode( handleOut , &consoleMode);
consoleMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
consoleMode |= DISABLE_NEWLINE_AUTO_RETURN;
SetConsoleMode( handleOut , consoleMode );
for (int i = 0; i < 10; ++i)
{
std::cout << "\x1b[38;2;" << 5 * i << ";" << 255 - 10 * i << ";220m"
<< "ANSI Escape Sequence " << i << std::endl;
}
}
see msdn page : [https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences][1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With