C语言 命令行 输出带有颜色字体接口

当我们在命令行终端打印文字的时候,会碰到打印的文字较多或输出的信息较多,我们很难很快的找到我们想要的数据信息。所以,为了能够保证我们能够在众多的文字中很快的找到我们想要的数据信息。让打印出的文字具有明显可识别的颜色,这能够让我们很快的找到我们需要的信息。比如:程序运行的时候的错误信息,文本信息中重要的数据标题等等。

下面是源代码,这是我自己做的一个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");
	}
}

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

融茧化蝶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值