一、格式
格式参考:
shell bash终端中输出的颜色和格式详解(超详细) - 小麦大叔 - 博客园https://www.cnblogs.com/unclemac/p/12783387.html
二、使用
#include <stdio.h>
#include "shell_font.h"
void main(void)
{
for(;;)
{
sleep(1);
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_RED, "Error") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_YELLOW, "Warrning") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_GREEN, "Okay") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_RED, "Error") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_YELLOW, "Warrning") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_GREEN, "Okay") "\r\n");
printf("\r\n");
}
}
三、效果
四、代码
/********************************************************************************
* @name: shell_color.h
* @author: zhangzhilin
* @description: 1225297600@qq.com
* @version: 1.0
* @date: 2025-4-18
* @brief: The function design for Shell printf color define
* @note:
********************************************************************************/
#ifndef __SHELL_FONT_H
#define __SHELL_FONT_H
/*------------------------------------------- Define [Start] -------------------------------------------*/
#define SHELL_FONT_STRING_STEP_1(R) #R
#define SHELL_FONT_STRING(R) SHELL_FONT_STRING_STEP_1(R)
#define SHELL_FONT_CMD(cmd) "\e[" SHELL_FONT_STRING(cmd) "m"
// 样式
#define SHELL_FONT_BOLD 1 // 加粗
#define SHELL_FONT_DIM 2 // 黯淡
#define SHELL_FONT_UDER_LINE 4 // 下划线
#define SHELL_FONT_BLINK 5 // 闪烁
#define SHELL_FONT_MINVERTED 7 // 反色
#define SHELL_FONT_HIDDEN 8 // 隐藏
// 复位
#define SHELL_FONT_RST_ALL 0
#define SHELL_FONT_RST_BOLD 21
#define SHELL_FONT_RST_DIM 22
#define SHELL_FONT_RST_UDER_LINE 24
#define SHELL_FONT_RST_BLINK 25
#define SHELL_FONT_RST_MINVERTED 27
#define SHELL_FONT_RST_HIDDEN 28
// 前景色
#define SHELL_COLOR_STR_DEFAULT 39 // 默认
#define SHELL_COLOR_STR_BLACK 30 // 黑色
#define SHELL_COLOR_STR_RED 31 // 红色
#define SHELL_COLOR_STR_GREEN 32 // 绿色
#define SHELL_COLOR_STR_YELLOW 33 // 黄色
#define SHELL_COLOR_STR_BLUE 34 // 蓝色
#define SHELL_COLOR_STR_MAGENTA 35 // 品红
#define SHELL_COLOR_STR_CYAN 36 // 青色
#define SHELL_COLOR_STR_LIG_GREY 37 // 浅灰
#define SHELL_COLOR_STR_DAR_GREY 90 // 深灰色
#define SHELL_COLOR_STR_LIG_RED 91 // 红灯
#define SHELL_COLOR_STR_LIG_GREEN 92 // 浅绿色
#define SHELL_COLOR_STR_LIG_YELLOW 93 // 淡黄色
#define SHELL_COLOR_STR_LIG_BLUE 94 // 浅蓝
#define SHELL_COLOR_STR_LIG_MAGENTA 95 // 浅洋红色
#define SHELL_COLOR_STR_LIG_CYAN 96 // 浅青色
#define SHELL_COLOR_STR_WHITE 97 // 白色
// 背景颜色
#define SHELL_COLOR_BK_DEFAULT 49 // 默认
#define SHELL_COLOR_BK_BLACK 40 // 黑色
#define SHELL_COLOR_BK_RED 41 // 红色
#define SHELL_COLOR_BK_GREEN 42 // 绿色
#define SHELL_COLOR_BK_YELLOW 43 // 黄色
#define SHELL_COLOR_BK_BLUE 44 // 蓝色
#define SHELL_COLOR_BK_MAGENTA 45 // 品红
#define SHELL_COLOR_BK_CYAN 46 // 青色
#define SHELL_COLOR_BK_LIG_GREY 47 // 浅灰
#define SHELL_COLOR_BK_DAR_GREY 100 // 深灰色
#define SHELL_COLOR_BK_LIG_RED 101 // 红灯
#define SHELL_COLOR_BK_LIG_GREEN 102 // 浅绿色
#define SHELL_COLOR_BK_LIG_YELLOW 103 // 淡黄色
#define SHELL_COLOR_BK_LIG_BLUE 104 // 浅蓝
#define SHELL_COLOR_BK_LIG_MAGENTA 105 // 浅洋红色
#define SHELL_COLOR_BK_LIG_CYAN 106 // 浅青色
#define SHELL_COLOR_BK_WHITE 107 // 白色
/*------------------------------------------- Define [End] -------------------------------------------*/
/*------------------------------------------- Special String [Start] -------------------------------------------*/
#define SHELL_STR_COLOR(cmd, str) SHELL_FONT_CMD(cmd) str SHELL_FONT_CMD(SHELL_FONT_RST_ALL)
#define SHELL_BK_INFO SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_BLUE, "Info")
#define SHELL_BK_WARN SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_YELLOW, "Warn")
#define SHELL_BK_ERR SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_RED, "Error")
#define SHELL_BK_OK SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_GREEN, "OK")
/*------------------------------------------- Special String [End] -------------------------------------------*/
/*------------------------------------------- Use [Start] -------------------------------------------*/
/*
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_RED, "Error") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_YELLOW, "Warrning") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_STR_LIG_GREEN, "Okay") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_RED, "Error") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_YELLOW, "Warrning") "\r\n");
printf(SHELL_STR_COLOR(SHELL_COLOR_BK_LIG_GREEN, "Okay") "\r\n");
*/
/*------------------------------------------- Use [End] -------------------------------------------*/
#endif
/* shell_color.h file end */