How to print colors in console using D?

I tried to run the escape sequences using the writeln () function, I also tried to use them with the printf () function imported from the std.c.stdlib module, but it only prints an empty line.

printf("\0x1B[5;32;40m Blink Text"); printf("\e[5;32;40m Blink Text\e[m"); writeln("\0x1b\x5b1;31;40m\tColor"); 

None of these works.

I tried everything I could think of, is there any way?

Finding a link to the D site library did not help me.


EDIT: SOLUTION

Ok, so I tried to import the SetConsoleTextAttribute function, as Mars kindly suggested:

 extern (Windows) bool SetConsoleTextAttribute(void*, ushort); 

I also imported another function (which I just guessed, I need to import, since I have no previous Win programming experience)

 extern (Windows) void* GetStdHandle(uint); 

And just call two functions

 auto handle = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(handle, FOREGROUND_BLUE); writeln("In Color"); 

This works great, thank you all for your time and help.

+7
source share
3 answers

As pointed out by CyberShadow, you should use \ x1B or \ 033. It should work fine if you are running Linux. However, Windows does not support these codes. Here you should use the SetConsoleTextAttribute API function from std.c.windows.windows.

+4
source

There is a typo in your line: use \x1B instead of \0x1B .

D does not support escape code \e in strings, use \x1B .

+3
source
+2
source

All Articles