How to use the ANSI Escape code for outputting colored text on Console
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 ca开发者_StackOverflowll to this function
textcolor(10);
But it gives compilation errors in g++ and in Visual Studio. Which compiler had this function available? Any details?
I'm afraid you forgot the ESC character:
#include <cstdio>
int main()
{
printf("%c[%dmHELLO!\n", 0x1B, 32);
}
Unfortunately it will only work on consoles that support ANSI escape sequences (like a linux console using bash, or old Windows consoles that used ansi.sys)
I created a very simple text-management library some time ago, being multiplatform, it uses native API calls for Windows and ANSI escape sequences for the rest of the platforms. It is fully documented and you can also browse the source code.
About your specific question, I think you are missing some codes. For example, in order to change the color of text, you should use something like:
static const char * CSI = "\33[";
printf( "%s%s", CSI, "31m" ); // RED
Hope this helps.
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://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences][1]
ANSI escape codes worked on DOS using the ansi.sys device driver. They won't work windows xp or higher. You need to use the console API SetConsoleTextAttribute()
textcolor
was available in the borland turbo c++ compiler.
Windows 10 supports ANSI Escape Sequences on the VT100 and derived terminal emulator technologies with 256 color extension. Description and examples are on the page Console Virtual Terminal Sequences.
std::ostringstream ss;
for (int i = 0; i < 10; ++i)
ss << "\x1b[38;2;" << 5 * i << ";" << 255 - 10 * i << ";220m"
<< "ANSI Escape Sequence " << i << std::endl;
std::cout << ss.str();
ANSI formatting codes aren't supported in windows.
http://en.wikipedia.org/wiki/ANSI_escape_code
This will work in any OS that support ANSI escape sequence
#include <iostream>
void printColored(char r, char g, char b, char _char_) {
std::cout << '\33' << '[' << '38' << ';' << '2' << ';' << r << ';' << g << ';' << b << 'm' << _char_ << '\33' << '[' << 'm'
}
Note:
char r is your red in RGB
char g is your green in RGB
char b is your blue in RGB
char char is you character to print in colored text
This may be been answered about using ANSI Escape to output RGB colored text at ANSI Color Specific RGB Sequence Bash
If you think that not true just edit it then i will accept the modification
I had this problem a while ago, too, using GCC on Windows 10. I had to set the following registry key to get it to work. [HKEY_CURRENT_USER\Console] "VirtualTerminalLevel"=dword:00000001
精彩评论