MOS Source Code
Loading...
Searching...
No Matches
ansi_colors.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include <mos/mos_global.h>
6#include <mos_string.h>
7
15#define STD_COLOR_LIGHT 0x8
16
36
47// "_" is to prevent the "must specify at least one argument for '...' parameter of variadic macro" warning
48#define ANSI_COLOR(...) "\033[" _ANSI_COLOR_N(__VA_ARGS__, _ANSI_COLOR_3, _ANSI_COLOR_2, _ANSI_COLOR_1, _)(__VA_ARGS__) "m"
49#define ANSI_COLOR_RESET "\033[0m"
50
51// ! WARN: These below are not meant to be used directly
52
53#define _ANSI_FG "3"
54#define _ANSI_BG "4"
55
56#define _ANSI_COLOR_black "0"
57#define _ANSI_COLOR_red "1"
58#define _ANSI_COLOR_green "2"
59#define _ANSI_COLOR_yellow "3"
60#define _ANSI_COLOR_blue "4"
61#define _ANSI_COLOR_magenta "5"
62#define _ANSI_COLOR_cyan "6"
63#define _ANSI_COLOR_white "7"
64
65#define _ANSI_STYLE_regular "0"
66#define _ANSI_STYLE_bright "1"
67#define _ANSI_STYLE_faint "2"
68#define _ANSI_STYLE_italic "3"
69#define _ANSI_STYLE_underline "4"
70#define _ANSI_STYLE_blink "5"
71#define _ANSI_STYLE_blink_fast "6" // not widely supported
72#define _ANSI_STYLE_reverse "7"
73#define _ANSI_STYLE_invisible "8"
74
75#define _ANSI_COLOR_1(fg) _ANSI_FG _ANSI_COLOR_##fg
76#define _ANSI_COLOR_2(fg, style) _ANSI_STYLE_##style ";" _ANSI_FG _ANSI_COLOR_##fg
77#define _ANSI_COLOR_3(fg, style, bg) _ANSI_STYLE_##style ";" _ANSI_FG _ANSI_COLOR_##fg ";" _ANSI_BG _ANSI_COLOR_##bg
78#define _ANSI_COLOR_N(_1, _2, _3, N, ...) N
79
81{
82 MOS_UNUSED(bg);
83 static const char *g_ansi_colors[] = {
84 [Black] = ANSI_COLOR(black),
85 [Blue] = ANSI_COLOR(blue),
86 [Green] = ANSI_COLOR(green),
87 [Cyan] = ANSI_COLOR(cyan),
88 [Red] = ANSI_COLOR(red),
89 [Magenta] = ANSI_COLOR(magenta),
90 [Brown] = ANSI_COLOR(yellow),
91 [Gray] = ANSI_COLOR(white, bright),
92 [DarkGray] = ANSI_COLOR(white),
93 [LightBlue] = ANSI_COLOR(blue, bright),
94 [LightGreen] = ANSI_COLOR(green, bright),
95 [LightCyan] = ANSI_COLOR(cyan, bright),
96 [LightRed] = ANSI_COLOR(red, bright),
97 [LightMagenta] = ANSI_COLOR(magenta, bright),
98 [Yellow] = ANSI_COLOR(yellow, bright),
99 [White] = ANSI_COLOR(white, bright),
100 };
101
102 const char *color = g_ansi_colors[fg];
103
104 // TODO: add support for background colors
105 if (bg == Red)
106 color = ANSI_COLOR(red, blink);
107
108 strcpy(buf, color);
109}
110
#define STD_COLOR_LIGHT
Definition ansi_colors.h:15
should_inline void get_ansi_color(char *buf, standard_color_t fg, standard_color_t bg)
Definition ansi_colors.h:80
standard_color_t
Definition ansi_colors.h:18
#define ANSI_COLOR(...)
ANSI color codes creator.
Definition ansi_colors.h:48
@ LightBlue
Definition ansi_colors.h:28
@ DarkGray
Definition ansi_colors.h:27
@ Gray
Definition ansi_colors.h:26
@ LightCyan
Definition ansi_colors.h:30
@ LightGreen
Definition ansi_colors.h:29
@ Brown
Definition ansi_colors.h:25
@ Cyan
Definition ansi_colors.h:22
@ Magenta
Definition ansi_colors.h:24
@ White
Definition ansi_colors.h:34
@ Yellow
Definition ansi_colors.h:33
@ Black
Definition ansi_colors.h:19
@ LightMagenta
Definition ansi_colors.h:32
@ Green
Definition ansi_colors.h:21
@ Red
Definition ansi_colors.h:23
@ LightRed
Definition ansi_colors.h:31
@ Blue
Definition ansi_colors.h:20
MOSAPI char * strcpy(char *__restrict dest, const char *__restrict src)
Definition mos_string.c:184
#define should_inline
Definition mos_global.h:37
#define MOS_UNUSED(x)
Definition mos_global.h:64