And*_*ovs 277
所有现代终端仿真器都使用ANSI转义码来显示颜色和其他内容.
不要理会库,代码非常简单.
更多信息在这里.
C中的示例:
#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main (int argc, char const *argv[]) {
printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
您可以输出特殊的颜色控制代码来获得彩色终端输出,这里是如何打印颜色的好资源.
例如:
printf("\033[22;34mHello, world!\033[0m"); // shows a blue hello world
Run Code Online (Sandbox Code Playgroud)
编辑:我原来的一个使用提示颜色代码,这是行不通的:(这个(我测试过).
您可以为每个功能指定一种颜色,以使其更有用.
#define Color_Red "\33[0:31m\\]" // Color Start
#define Color_end "\33[0m\\]" // To flush out prev settings
#define LOG_RED(X) printf("%s %s %s",Color_Red,X,Color_end)
foo()
{
LOG_RED("This is in Red Color");
}
Run Code Online (Sandbox Code Playgroud)
同样,您可以选择不同的颜色代码,使其更通用.
小智 8
#include <stdio.h>
#define BLUE(string) "\x1b[34m" string "\x1b[0m"
#define RED(string) "\x1b[31m" string "\x1b[0m"
int main(void)
{
printf("this is " RED("red") "!\n");
// a somewhat more complex ...
printf("this is " BLUE("%s") "!\n","blue");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
阅读维基百科:
| 归档时间: |
|
| 查看次数: |
155634 次 |
| 最近记录: |