当我们在命令行终端打印文字的时候,会碰到打印的文字较多或输出的信息较多,我们很难很快的找到我们想要的数据信息。所以,为了能够保证我们能够在众多的文字中很快的找到我们想要的数据信息。让打印出的文字具有明显可识别的颜色,这能够让我们很快的找到我们需要的信息。比如:程序运行的时候的错误信息,文本信息中重要的数据标题等等。
下面是源代码,这是我自己做的一个C语言函数的工具集合中的一个。做它的目的,其一是为了提高自己的开发效率,其二是减少自己代码的冗余信息。
输出颜色的接口只是其中的一个。
更多信息,可前往Github查看 :RJHD_C_TOOL
下面是C语言 命令行 输出带有颜色字体的接口,具体的使用方法可查看注释 ,该函数编写的使用环境是在Linux下进行,其中的显示方式有些暂时没有效果,还需要更改。望提出更改的建议或前往Github共同开发帮助工具。提高开发的效率。
/*
打印输出文字,可设置文字的颜色,字体背景,显示方式,输出后,是否回车 ,若不需要其中的某个选项,请填写 0 表示不使用某个功能,请勿填写其它值
参数 :
OutPut_Content 需要样式输出的内容
Font_Colour 内容的字体颜色 Value : 30-39
Font_Background 内容的字体背景 Value : 40-47
DisplayEffect 内容的显示方式 Value : 0-8
IF_Enter 是否回车 Value : 0/1
*/
//字颜色 : 30 - 39
#define Font_Colour_Black 30
#define Font_Colour_Red 31
#define Font_Colour_Green 32
#define Font_Colour_Yellow 33
#define Font_Colour_Blue 34
#define Font_Colour_Purple 35
#define Font_Colour_Dark_Grean 36
#define Font_Colour_White 37
#define Font_Colour_38 38 //在缺省的前景颜色上设置下划线
#define Font_Colour_39 39 //在缺省的后置颜色上关闭下划线
//字背景颜色范围 : 40 - 49
#define Font_Background_Black 40 //黑
#define Font_Background_Crimson 41 //深红
#define Font_Background_Green 42 //绿
#define Font_Background_Yellow 43 //黄色
#define Font_Background_Blue 44 //蓝色
#define Font_Background_Purple 45 //紫色
#define Font_Background_Dark_green 46 //深绿
#define Font_Background_White 47 //白色
//显示效果
//\33[0m 关闭所有属性
#define Font_Effect_Hi_Lite 1 //设置高亮度
#define Font_Effect_Low_Lights 2 //低亮(减弱)显示
#define Font_Effect_Underline 4 //下划线
#define Font_Effect_Twinkle 5 //闪烁
#define Font_Effect_Anti_Evolution 7 //反显
#define Font_Effect_Blank 8 //消隐
//\33[30m -- \33[37m 设置前景色
//\33[40m -- \33[47m 设置背景色
//\33[nA 光标上移n行
//\33[nB 光标下移n行
//\33[nC 光标右移n行
//\33[nD 光标左移n行
//\33[y;xH设置光标位置
//\33[2J 清屏
//\33[K 清除从光标到行尾的内容
//\33[s 保存光标位置
//\33[u 恢复光标位置
//\33[?25l 隐藏光标
//\33[?25h 显示光标
/*
打印输出文字,可设置文字的颜色,字体背景,显示方式,输出后,是否回车 ,若不需要其中的某个选项,请填写 0 表示不使用某个功能,请勿填写其它值
DisplayEffect的部分效果可能显示不出来
参数 :
OutPut_Content 需要样式输出的内容
Font_Colour 内容的字体颜色 Value : 30-39
Font_Background 内容的字体背景 Value : 40-47
DisplayEffect 内容的显示方式 Value : 0-8
IF_Enter 是否回车 Value : 0/1
*/
int PrintStyle(char *OutPut_Content, int Font_Colour, int Font_Background, int DisplayEffect, int IF_Enter)
{
char Output_Buf[1024];
memset(Output_Buf, 0, 1024);
strncat(Output_Buf, "\033[", strlen("\033["));
if (Font_Colour != 0)
{
if (Font_Colour < 30 || Font_Colour > 37)
{
printf("Front Colour Values : 30 - 37.\n");
}
else
{
strcat(Output_Buf, IntToString(Font_Colour));
}
}
if (Font_Background != 0)
{
if (Font_Background < 40 || Font_Background > 47)
{
printf("Front Background Colour Values : 40 - 47.\n");
}
else
{
if (strlen(Output_Buf) != 0)
{
strcat(Output_Buf, ";");
}
strcat(Output_Buf, IntToString(Font_Background));
}
}
if (DisplayEffect != 0)
{
if (DisplayEffect < 1 || DisplayEffect > 8)
{
printf("Front Background Colour Values : 1 - 8.\n");
}
else
{
if (strlen(Output_Buf) != 0)
{
strcat(Output_Buf, ";");
}
strcat(Output_Buf, IntToString(DisplayEffect));
}
}
strcat(Output_Buf, "m");
strncat(Output_Buf, OutPut_Content, strlen(OutPut_Content));
strcat(Output_Buf, "\033[0m");
printf("%s", Output_Buf);
if (IF_Enter == 0)
{
return 0;
}
else
{
printf("\n");
}
}