Make printf printing more colorful

Basic principle

  • On the terminal that supports ANSI color control, the character display mode of the terminal can be changed by ANSI control code;
  • So we can use printf or cout to output specific ANSI control code to change the following string output to the desired display mode;
  • The ANSI control code uses ESC as the start mark of the control code, which corresponds to the escape character \ 033 in C language, and then uses m as the end character;
  • Set the format of ANSI control code in C as follows: 033 [control code 1; control code 2; control code 3 Control code nm
  • See blog for common ANSI control codes: Common ANSI control code 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>

//Restore ANSI display to default
#define PRINT_DEFAULT {printf("\033[0m");}

//Characters are displayed in the way of setting color and highlighting;
#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}
//The character also sets the color and blinks to highlight
#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}
//Characters are displayed in a set color and underlined way
#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}
//Character to set color reversal display
#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 prints the current function name and line number by default;
#define PRINT_DBG(fmt, args...) {printf("[%s] [%d]", __FUNCTION__, __LINE__);printf(fmt, ##args);}

/**
 * [_timeShowTask Update the display time every second]
 * @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);
        //Move the printed cursor up one line; let time print the update in its original position
        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;
}

The display effect after compiling and running is as follows:

Tags: C

Posted on Thu, 19 Mar 2020 14:29:18 -0400 by jeliot