Loading...
Searching...
No Matches
ansi_style.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2026 TU Dresden
3 * SPDX-FileCopyrightText: 2026 Carl Seifert <carl.seifert@tu-dresden.de>
4 * SPDX-License-Identifier: LGPL-2.1-only
5 */
6
7#pragma once
8
38
44
45#include "macros/xtstr.h"
46
47#ifdef __cplusplus
48extern "C" {
49#endif
50
55
57#define _ANSI_SGR_PREFIX "\x1b["
58
60#define _ANSI_SGR_SUFFIX "m"
61
63
68
70#define _ANSI_COLOR_CODE_PRIMARY "0"
71
73#define _ANSI_COLOR_CODE_BLACK _ANSI_COLOR_CODE_PRIMARY
74
76#define _ANSI_COLOR_CODE_RED "1"
77
79#define _ANSI_COLOR_CODE_GREEN "2"
80
82#define _ANSI_COLOR_CODE_YELLOW "3"
83
85#define _ANSI_COLOR_CODE_BLUE "4"
86
88#define _ANSI_COLOR_CODE_PURPLE "5"
89
91#define _ANSI_COLOR_CODE_CYAN "6"
92
94#define _ANSI_COLOR_CODE_WHITE "7"
95
97#define _ANSI_COLOR_CODE_RGB(r, g, b) "8;2;" XTSTR(r) ";" XTSTR(g) ";" XTSTR(b)
98
100#define _ANSI_COLOR_CODE_INDEXED(index) "8;5;" XTSTR(index)
101
103
108
110#define _ANSI_COLOR_MODIFIER_FOREGROUND "3"
111
113#define _ANSI_COLOR_MODIFIER_BRIGHT_FOREGROUND "9"
114
116#define _ANSI_COLOR_MODIFIER_BACKGROUND "4"
117
119#define _ANSI_COLOR_MODIFIER_BRIGHT_BACKGROUND "10"
120
122
127
128#ifndef DOXYGEN
129/* This macro resolves the color suffix (preprocessor token) to the macro, e.g.
130 * RED => _ANSI_COLOR_CODE_RED. */
131# define __ANSI_COLOR(color) _ANSI_COLOR_CODE_ ## color
132#endif
133
135#define ANSI_STYLE_FOREGROUND(color) \
136 _ANSI_COLOR_MODIFIER_FOREGROUND __ANSI_COLOR(color)
137
142#define ANSI_STYLE_FG(color) ANSI_STYLE_FOREGROUND(color)
143
145#define ANSI_STYLE_FOREGROUND_BRIGHT(color) \
146 _ANSI_COLOR_MODIFIER_BRIGHT_FOREGROUND __ANSI_COLOR(color)
147
152#define ANSI_STYLE_FG_BRIGHT(color) ANSI_STYLE_FOREGROUND_BRIGHT(color)
153
155#define ANSI_STYLE_BACKGROUND(color) \
156 _ANSI_COLOR_MODIFIER_BACKGROUND __ANSI_COLOR(color)
157
162#define ANSI_STYLE_BG(color) ANSI_STYLE_BACKGROUND(color)
163
165#define ANSI_STYLE_BACKGROUND_BRIGHT(color) \
166 _ANSI_COLOR_MODIFIER_BRIGHT_BACKGROUND __ANSI_COLOR(color)
167
172#define ANSI_STYLE_BG_BRIGHT(color) ANSI_STYLE_BACKGROUND_BRIGHT(color)
173
175
180
181#ifndef DOXYGEN
182# define ANSI_STYLE_NONE "0"
183#endif
184
190#define ANSI_STYLE_BOLD "1"
191
197#define ANSI_STYLE_DIM "2"
198
200#define ANSI_STYLE_ITALIC "3"
202#define ANSI_STYLE_UNDERLINED "4"
203
205#define ANSI_STYLE_BLINK_SLOW "5"
207#define ANSI_STYLE_BLINK_FAST "6"
208
210#define ANSI_STYLE_NEGATED "7"
211
213#define ANSI_STYLE_CONCEALED "8"
214
216#define ANSI_STYLE_STRIKETHROUGH "9"
217
219
224
225#ifndef DOXYGEN
226/* These macros apply multiple SGR text styles given as suffixes of their respective
227 * ANSI_STYLE_... macro, so styling macros at the expansion site are kept short.
228 * Example: __ANSI_STYLE_2(BOLD, UNDERLINED) => ";" ANSI_STYLE_BOLD ";" ANSI_STYLE_UNDERLINED
229 * __ANSI_GET_STYLE implements variadic style parameters, so ANSI_STYLE can be given
230 * anywhere between zero and 5 style macros. This limit may be increased in the future. */
231
232/* __ANSI_GET_STYLE returns the correct concatenation macro (__ANSI_STYLE_N) based on the
233 * number of arguments. */
234# define __ANSI_GET_STYLE(_0, _1, _2, _3, _4, _5, _6, name, ...) name
235/* __ANSI_STYLED builds the longer style macro from just the suffix token. */
236# define __ANSI_STYLED(keyword) ANSI_STYLE_ ## keyword
237/* These macros recursively expand 1 to 5 style arguments. */
238# define __ANSI_STYLE_0() __ANSI_STYLED(NONE)
239# define __ANSI_STYLE_1(a) __ANSI_STYLED(a)
240# define __ANSI_STYLE_2(a, b) __ANSI_STYLE_1(a) ";" __ANSI_STYLED(b)
241# define __ANSI_STYLE_3(a, b, c) __ANSI_STYLE_2(a, b) ";" __ANSI_STYLED(c)
242# define __ANSI_STYLE_4(a, b, c, d) __ANSI_STYLE_3(a, b, c) ";" __ANSI_STYLED(d)
243# define __ANSI_STYLE_5(a, b, c, d, e) __ANSI_STYLE_4(a, b, c, d) ";" __ANSI_STYLED(e)
244# define __ANSI_STYLE_6(a, b, c, d, e, f) __ANSI_STYLE_5(a, b, c, d, e) ";" __ANSI_STYLED(f)
245
246/* This macro calls __ANSI_GET_STYLE and expands to the concatenated SGR
247 * style string (";" .. ";" ...) */
248# define __ANSI_APPLY_STYLES(...) \
249 __ANSI_GET_STYLE(dummy, ##__VA_ARGS__, \
250 __ANSI_STYLE_6, __ANSI_STYLE_5, __ANSI_STYLE_4, __ANSI_STYLE_3, __ANSI_STYLE_2, \
251 __ANSI_STYLE_1, __ANSI_STYLE_0 \
252 )(__VA_ARGS__)
253
254#endif
255
336#define ANSI_STYLE(...) \
337 _ANSI_SGR_PREFIX \
338 __ANSI_APPLY_STYLES(__VA_ARGS__) \
339 _ANSI_SGR_SUFFIX
340
348#define ANSI_STYLE_RESET _ANSI_SGR_PREFIX ANSI_STYLE_NONE _ANSI_SGR_SUFFIX
349
367#define ANSI_STYLED(text, ...) \
368 ANSI_STYLE(__VA_ARGS__) text ANSI_STYLE_RESET
369 /* end of section */
371
372#ifdef __cplusplus
373}
374#endif
375 /* end of group */
Macro to return string representation of x.