我正在制作一个简单的应用程序,需要彩色输出。如何使我的输出像emacs和bash一样上色?
由于我的应用程序只针对UNIX系统,因此我不关心Windows。
现代终端模拟器都使用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;
}
您可以为每个功能分配一种颜色,使其更有用。
#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");
}
同样地,您可以选择不同的颜色代码并使其更通用。
printf("\033[22;34mHello, world!\033[0m"); // shows a blue hello world
编辑:我的原始代码使用提示颜色代码,但不起作用 :( 这个可以(我测试过了)。
22
更改为1
以查看粗体。 - hola#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;
}
printf()
函数。 #include<stdio.h>
#define ah_red "\e[31m"
#define printf(X) printf(ah_red "%s",X);
#int main()
{
printf("Bangladesh");
printf("\n");
return 0;
}
因为您不能使用字符串格式打印字符。您还可以考虑添加类似以下内容的格式:
#define PRINTC(c,f,s) printf ("\033[%dm" f "\033[0m", 30 + c, s)
f
是指格式化,类似于printf
PRINTC (4, "%s\n", "bar")
将会打印蓝色条
PRINTC (1, "%d", 'a')
将会打印出red 97
在@AndrejsCainikovs的答案基础上,这里提供了一段代码,适用于支持24位RGB代码(包括字体和背景颜色)的终端(如Xterm、KDE的Konsole和iTerm,以及所有基于libvte的终端,包括GNOME终端,参见https://en.wikipedia.org/wiki/ANSI_escape_code#Colors):
#include <stdio.h>
#define ANSI_FONT_COL_RESET "\x1b[0m"
#define FONT_COL_CUSTOM_RED "\e[38;2;200;0;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_GREEN "\e[38;2;0;200;0m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define FONT_COL_CUSTOM_BLUE "\e[38;2;0;0;200m" // where rrr;ggg;bbb in 38;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_RED "\e[48;2;200;0;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_GREEN "\e[48;2;0;200;0m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
#define BCKGRD_COL_CUSTOM_BLUE "\e[48;2;0;0;200m" // where rrr;ggg;bbb in 48;2;rrr;ggg;bbbm can go from 0 to 255 respectively
int main (int argc, char const *argv[]) {
printf(FONT_COL_CUSTOM_RED "This font color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN "This font color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_BLUE "This font color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_RED "This background color is CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_GREEN "This background color is CUSTOM_GREEN!" ANSI_FONT_COL_RESET "\n");
printf(BCKGRD_COL_CUSTOM_BLUE "This background color is CUSTOM_BLUE!" ANSI_FONT_COL_RESET "\n");
printf(FONT_COL_CUSTOM_GREEN BCKGRD_COL_CUSTOM_RED "This font color is CUSTOM_GREEN with background CUSTOM_RED!" ANSI_FONT_COL_RESET "\n");
printf( "This font color is NORMAL!\n");
return 0;
}