Make printf printing more colorful

Fundamental

  • On a terminal that supports ANSI color control, you can use the ANSI control code to change the display mode of the terminal's characters;
  • So we can output specific ANSI control codes through printf or cout to change the subsequent string output to the desired display mode;
  • The ANSI control code uses ESC as the start marker of the control code, and corresponds to the escape character \033 in the C language; and then uses m as the end character;
  • The format of the ANSI control code set in C is: \033[control code 1; control code 2; control code 3;...;control code nm
  • Commonly used ANSI control codes see blog: Common ANSI control codes table

Simple example

#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;
}

After compiling and running, the display effect is as follows:
ANSI display

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325628549&siteId=291194637