如何用D将颜色打印到控制台?

Pav*_*ska 9 console d escaping colors

我已尝试使用writeln()函数转义序列,我也尝试将它们与从std.c.stdlib模块导入的printf()函数一起使用,但它只打印一个空行.

printf("\0x1B[5;32;40m Blink Text");

printf("\e[5;32;40m Blink Text\e[m");

writeln("\0x1b\x5b1;31;40m\tColor");
Run Code Online (Sandbox Code Playgroud)

这些都不起作用.

我已经尝试了我能想到的一切,有办法吗?

搜索D网站的图书馆参考对我没有帮助.


编辑:解决方案

好的,所以我试图导入函数SetConsoleTextAttribute,正如Mars所建议的那样:

extern (Windows) bool SetConsoleTextAttribute(void*, ushort);
Run Code Online (Sandbox Code Playgroud)

我还导入了另一个函数(我只是猜测我需要导入,因为我之前没有使用Win编程的经验)

extern (Windows) void* GetStdHandle(uint);
Run Code Online (Sandbox Code Playgroud)

简单地称之为两个功能

auto handle  = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_BLUE);
writeln("In Color");
Run Code Online (Sandbox Code Playgroud)

这非常有效,非常感谢您的时间和帮助

Mar*_*ars 6

就像Cyber​​Shadow指出的那样,你必须使用\ x1B或\ 033.它应该可以正常工作,只要你在Linux上.Windows虽然不支持这些代码.在这里,您必须使用std.c.windows.windows中的API函数SetConsoleTextAttribute.