2018年7月10日 星期二

print out the color

#include <stdio.h>
#include <stdint.h>
#include <string.h>


#define cRED "\x1b[;31;1m"
#define cGREEN "\x1b[;32;1m"
#define cRESET "\x1b[0;m"


void printfc(char *color, char *str)
{
    char buf[16];
    strncpy(buf, color, 16);
    if ( !strcmp(buf, "red"))
        printf("\x1B[031m%s\x1B[0m", str);
    else if (!strcmp(buf, "green"))
        printf("\x1B[032m%s\x1B[0m", str);
    else if (!strcmp(buf, "yellow"))
        printf("\x1B[033m%s\x1B[0m", str);
    else if (!strcmp(buf, "blue"))
        printf("\x1B[034m%s\x1B[0m", str);
    else if (!strcmp(buf, "purple"))
        printf("\x1B[035m%s\x1B[0m", str);
    else
        printf("%s", str);

}

int main()
{
    printfc("red", "hello\n");
    printfc("yellow", "hello\n");
    printfc("green", "hello\n");
    printfc("blue", "hello\n");
    printfc("purple", "hello\n");
    printf ("default\n");
    
    printf(cGEN "airplane mode%s\n" cRESET, "ESC1");

    
}

沒有留言:

張貼留言