How to create colored text?

In C ++, the following code, when launched in the console, will print text in color:

cout << "\e[32;40mGreenForegroundAndBlackBackgroundText" << endl;

In D, I get an error:

string s = "\e[32;40mGreenForegroundAndBlackBackgroundText";  // undefined escape sequence \e

Is there a way to make this work in D?

+5
source share
1 answer

C ++ constant string failure \efor an escape character is a non-standard GCC C extension for escapes characters (also accepted by Clang, possibly).

You just need to put octal encoding, perhaps in quality \033or\x1b

Be careful, however, that what is \e[32;40mnot standard C or C ++ is the ANSI terminal escape sequence associated with tty -s.

+8
source

All Articles