基本原理
- 在支持ANSI color控制的终端上,可用通过ANSI控制码改变终端的字符的显示方式;
- 所以我们可以通过printf或者cout输出特定ANSI控制码来让后面的字符串输出改变成想要的显示方式;
- ANSI控制码以ESC作为控制码的开始标记,在C语言中对应转义字符\033;然后以m作为结尾字符;
- 在C中设定ANSI控制码的格式为: \033[控制码1;控制码2;控制码3;…;控制码nm
- 常用的ANSI控制码见blog:常用ANSI控制码表
简单示例
#include <stdio.h>
#include <string.h>
#include <sys/msg.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <errno.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include <sys/time.h>
//将ANSI的显示方式恢复到默认
#define PRINT_DEFAULT {printf("\033[0m");}
//字符以设定颜色和高亮的方式显示;
#define PRINT_RED(fmt, args...) {printf("\033[1;31m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_YELLOW(fmt, args...) {printf("\033[1;33m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_BLUE(fmt, args...) {printf("\033[1;34m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_GREEN(fmt, args...) {printf("\033[1;32m");printf(fmt, ##args);PRINT_DEFAULT}
//字符也设定颜色并且闪烁高亮的方式显示
#define PRINT_RED_FLIKER(fmt, args...) {printf("\033[1;5;31m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_YELLOW_FLIKER(fmt, args...) {printf("\033[1;5;33m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_BLUE_FLIKER(fmt, args...) {printf("\033[1;5;34m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_GREEN_FLIKER(fmt, args...) {printf("\033[1;5;32m");printf(fmt, ##args);PRINT_DEFAULT}
//字符以设定颜色并加下划线的方式显示
#define PRINT_RED_UNDERLINE(fmt, args...) {printf("\033[4;31m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_YELLOW_UNDERLINE(fmt, args...) {printf("\033[4;33m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_BLUE_UNDERLINE(fmt, args...) {printf("\033[4;34m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_GREEN_UNDERLINE(fmt, args...) {printf("\033[4;32m");printf(fmt, ##args);PRINT_DEFAULT}
//字符以设定颜色反转显示
#define PRINT_RED_INVERSE(fmt, args...) {printf("\033[7;31m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_YELLOW_INVERSE(fmt, args...) {printf("\033[7;33m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_BLUE_INVERSE(fmt, args...) {printf("\033[7;34m");printf(fmt, ##args);PRINT_DEFAULT}
#define PRINT_GREEN_INVERSE(fmt, args...) {printf("\033[7;32m");printf(fmt, ##args);PRINT_DEFAULT}
//debug打印默认打印当前函数名和行号;
#define PRINT_DBG(fmt, args...) {printf("[%s] [%d]", __FUNCTION__, __LINE__);printf(fmt, ##args);}
/**
* [_timeShowTask 每隔一秒原处更新显示时间]
* @param arg [NULL]
* @return [NULL]
*/
static void *_timeShowTask(void* arg)
{
time_t stTime;
char strTime[1024];
while(1)
{
time(&stTime);
ctime_r(&stTime, strTime);
PRINT_RED("The time is:%s", strTime);
sleep(1);
//打印的光标向上移动一行;让时间在原位置打印更新
printf("\033[1A");
}
return NULL;
}
int main(int argc, char *argv[])
{
const char * testStr = "This is a test sting!";
pthread_t threadID;
PRINT_RED("%s\n", testStr);
PRINT_YELLOW("%s\n", testStr);
PRINT_BLUE("%s\n", testStr);
PRINT_GREEN("%s\n", testStr);
PRINT_RED_FLIKER("%s\n", testStr);
PRINT_YELLOW_FLIKER("%s\n", testStr);
PRINT_BLUE_FLIKER("%s\n", testStr);
PRINT_GREEN_FLIKER("%s\n", testStr);
PRINT_RED_UNDERLINE("%s\n", testStr);
PRINT_YELLOW_UNDERLINE("%s\n", testStr);
PRINT_BLUE_UNDERLINE("%s\n", testStr);
PRINT_GREEN_UNDERLINE("%s\n", testStr);
PRINT_RED_INVERSE("%s\n", testStr);
PRINT_YELLOW_INVERSE("%s\n", testStr);
PRINT_BLUE_INVERSE("%s\n", testStr);
PRINT_GREEN_INVERSE("%s\n", testStr);
PRINT_DBG("%s\n", testStr);
if(0 != pthread_create(&threadID, NULL, _timeShowTask, NULL))
{
PRINT_DBG("pthread_create failed!\n");
return -1;
}
pthread_detach(threadID);
while(1)
{
sleep(50);
}
return 0;
}
编译运行后显示效果如下图: