| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include "basic-forward.h" |
| 5 |
|
| 6 |
/* Limits the use of ANSI colors to a subset. */ |
| 7 |
typedef enum ColorMode { |
| 8 |
COLOR_OFF, /* No colors, monochrome output. */ |
| 9 |
COLOR_16, /* Only the base 16 colors. */ |
| 10 |
COLOR_256, /* Only 256 colors. */ |
| 11 |
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */ |
| 12 |
_COLOR_MODE_FIXED_MAX, |
| 13 |
|
| 14 |
/* The "AUTO" modes are as the above, but subject to suitable settings for the environment variables |
| 15 |
* TERM and NO_COLOR. */ |
| 16 |
COLOR_AUTO_16 = _COLOR_MODE_FIXED_MAX, |
| 17 |
COLOR_AUTO_256, |
| 18 |
COLOR_AUTO_24BIT, |
| 19 |
|
| 20 |
/* Same as default (unset), except that $NO_COLOR is ignored/overridden */ |
| 21 |
COLOR_TRUE, |
| 22 |
|
| 23 |
_COLOR_MODE_MAX, |
| 24 |
_COLOR_MODE_INVALID = -EINVAL, |
| 25 |
} ColorMode; |
| 26 |
|
| 27 |
DECLARE_STRING_TABLE_LOOKUP(color_mode, ColorMode); |
| 28 |
|
| 29 |
ColorMode get_color_mode(void); |
| 30 |
static inline bool colors_enabled(void) { |
| 31 |
/* Returns true if colors are considered supported on our stdout. */ |
| 32 |
return get_color_mode() != COLOR_OFF; |
| 33 |
} |
| 34 |
|
| 35 |
ColorMode parse_systemd_colors(void); |
| 36 |
|
| 37 |
bool underline_enabled(void); |
| 38 |
|
| 39 |
void reset_ansi_feature_caches(void); |
| 40 |
|
| 41 |
bool looks_like_ansi_color_code(const char *str); |
| 42 |
|
| 43 |
/* Regular colors */ |
| 44 |
#define ANSI_BLACK "\x1B[0;30m" /* Some type of grey usually. */ |
| 45 |
#define ANSI_RED "\x1B[0;31m" |
| 46 |
#define ANSI_GREEN "\x1B[0;32m" |
| 47 |
#define ANSI_YELLOW "\x1B[0;33m" |
| 48 |
#define ANSI_BLUE "\x1B[0;34m" |
| 49 |
#define ANSI_MAGENTA "\x1B[0;35m" |
| 50 |
#define ANSI_CYAN "\x1B[0;36m" |
| 51 |
#define ANSI_WHITE "\x1B[0;37m" /* This is actually rendered as light grey, legible even on a white |
| 52 |
* background. See ANSI_HIGHLIGHT_WHITE for real white. */ |
| 53 |
|
| 54 |
#define ANSI_BRIGHT_BLACK "\x1B[0;90m" |
| 55 |
#define ANSI_BRIGHT_RED "\x1B[0;91m" |
| 56 |
#define ANSI_BRIGHT_GREEN "\x1B[0;92m" |
| 57 |
#define ANSI_BRIGHT_YELLOW "\x1B[0;93m" |
| 58 |
#define ANSI_BRIGHT_BLUE "\x1B[0;94m" |
| 59 |
#define ANSI_BRIGHT_MAGENTA "\x1B[0;95m" |
| 60 |
#define ANSI_BRIGHT_CYAN "\x1B[0;96m" |
| 61 |
#define ANSI_BRIGHT_WHITE "\x1B[0;97m" |
| 62 |
|
| 63 |
#define ANSI_GREY "\x1B[0;38:5:245m" |
| 64 |
|
| 65 |
/* Bold/highlighted */ |
| 66 |
#define ANSI_HIGHLIGHT_BLACK "\x1B[0;1;30m" |
| 67 |
#define ANSI_HIGHLIGHT_RED "\x1B[0;1;31m" |
| 68 |
#define ANSI_HIGHLIGHT_GREEN "\x1B[0;1;32m" |
| 69 |
#define _ANSI_HIGHLIGHT_YELLOW "\x1B[0;1;33m" /* This yellow is currently not displayed well by some terminals */ |
| 70 |
#define ANSI_HIGHLIGHT_BLUE "\x1B[0;1;34m" |
| 71 |
#define ANSI_HIGHLIGHT_MAGENTA "\x1B[0;1;35m" |
| 72 |
#define ANSI_HIGHLIGHT_CYAN "\x1B[0;1;36m" |
| 73 |
#define ANSI_HIGHLIGHT_WHITE "\x1B[0;1;37m" |
| 74 |
#define ANSI_HIGHLIGHT_YELLOW4 "\x1B[0;1;38:5:100m" |
| 75 |
#define ANSI_HIGHLIGHT_KHAKI3 "\x1B[0;1;38:5:185m" |
| 76 |
#define ANSI_HIGHLIGHT_GREY "\x1B[0;1;38:5:245m" |
| 77 |
|
| 78 |
#define ANSI_HIGHLIGHT_YELLOW ANSI_HIGHLIGHT_KHAKI3 /* Replacement yellow that is more legible */ |
| 79 |
|
| 80 |
/* Underlined */ |
| 81 |
#define ANSI_GREY_UNDERLINE "\x1B[0;4;38:5:245m" |
| 82 |
#define ANSI_BRIGHT_BLACK_UNDERLINE "\x1B[0;4;90m" |
| 83 |
#define ANSI_HIGHLIGHT_RED_UNDERLINE "\x1B[0;1;4;31m" |
| 84 |
#define ANSI_HIGHLIGHT_GREEN_UNDERLINE "\x1B[0;1;4;32m" |
| 85 |
#define ANSI_HIGHLIGHT_YELLOW_UNDERLINE "\x1B[0;1;4;38:5:185m" |
| 86 |
#define ANSI_HIGHLIGHT_BLUE_UNDERLINE "\x1B[0;1;4;34m" |
| 87 |
#define ANSI_HIGHLIGHT_MAGENTA_UNDERLINE "\x1B[0;1;4;35m" |
| 88 |
#define ANSI_HIGHLIGHT_GREY_UNDERLINE "\x1B[0;1;4;38:5:245m" |
| 89 |
|
| 90 |
/* Other ANSI codes */ |
| 91 |
#define ANSI_UNDERLINE "\x1B[0;4m" |
| 92 |
#define ANSI_ADD_UNDERLINE "\x1B[4m" |
| 93 |
#define ANSI_ADD_UNDERLINE_GREY ANSI_ADD_UNDERLINE "\x1B[58:5:245m" |
| 94 |
#define ANSI_ITALICS "\x1B[0;3m" |
| 95 |
#define ANSI_ADD_ITALICS "\x1B[3m" |
| 96 |
#define ANSI_HIGHLIGHT "\x1B[0;1;39m" |
| 97 |
#define ANSI_HIGHLIGHT_UNDERLINE "\x1B[0;1;4m" |
| 98 |
|
| 99 |
/* Fallback colors: 256 → 16 */ |
| 100 |
#define ANSI_HIGHLIGHT_GREY_FALLBACK "\x1B[0;1;90m" |
| 101 |
#define ANSI_HIGHLIGHT_GREY_FALLBACK_UNDERLINE "\x1B[0;1;4;90m" |
| 102 |
#define ANSI_HIGHLIGHT_YELLOW_FALLBACK "\x1B[0;1;33m" |
| 103 |
#define ANSI_HIGHLIGHT_YELLOW_FALLBACK_UNDERLINE "\x1B[0;1;4;33m" |
| 104 |
|
| 105 |
/* Background colors */ |
| 106 |
#define ANSI_BACKGROUND_BLUE "\x1B[44m" |
| 107 |
|
| 108 |
/* Reset/clear ANSI styles */ |
| 109 |
#define ANSI_NORMAL "\x1B[0m" |
| 110 |
|
| 111 |
#define DEFINE_ANSI_FUNC(name, NAME) \ |
| 112 |
static inline const char* ansi_##name(void) { \ |
| 113 |
return colors_enabled() ? ANSI_##NAME : ""; \ |
| 114 |
} |
| 115 |
|
| 116 |
/* NB: in 256 mode we always emit the fallback color first, in order to deal with terminals with |
| 117 |
* incomplete 256 color support (most notably Linux console, which a) lacks support for ":" |
| 118 |
* subcommand separator and b) skips over the whole CSI-m sequence if it sees an "invalid" command). |
| 119 |
* In 24-bit mode we don't bother with this however, under the assumption that $COLORTERM and friends |
| 120 |
* reflect the correct status. */ |
| 121 |
|
| 122 |
#define DEFINE_ANSI_FUNC_256(name, NAME, FALLBACK) \ |
| 123 |
static inline const char* ansi_##name(void) { \ |
| 124 |
switch (get_color_mode()) { \ |
| 125 |
case COLOR_OFF: return ""; \ |
| 126 |
case COLOR_16: return ANSI_##FALLBACK; \ |
| 127 |
case COLOR_256: return ANSI_##FALLBACK ANSI_##NAME; \ |
| 128 |
default: return ANSI_##NAME; \ |
| 129 |
} \ |
| 130 |
} |
| 131 |
|
| 132 |
DEFINE_ANSI_FUNC(normal, NORMAL); |
| 133 |
DEFINE_ANSI_FUNC(highlight, HIGHLIGHT); |
| 134 |
DEFINE_ANSI_FUNC(black, BLACK); |
| 135 |
DEFINE_ANSI_FUNC(red, RED); |
| 136 |
DEFINE_ANSI_FUNC(green, GREEN); |
| 137 |
DEFINE_ANSI_FUNC(yellow, YELLOW); |
| 138 |
DEFINE_ANSI_FUNC(blue, BLUE); |
| 139 |
DEFINE_ANSI_FUNC(magenta, MAGENTA); |
| 140 |
DEFINE_ANSI_FUNC(cyan, CYAN); |
| 141 |
DEFINE_ANSI_FUNC(white, WHITE); |
| 142 |
DEFINE_ANSI_FUNC_256(grey, GREY, BRIGHT_BLACK); |
| 143 |
|
| 144 |
DEFINE_ANSI_FUNC(bright_black, BRIGHT_BLACK); |
| 145 |
DEFINE_ANSI_FUNC(bright_red, BRIGHT_RED); |
| 146 |
DEFINE_ANSI_FUNC(bright_green, BRIGHT_GREEN); |
| 147 |
DEFINE_ANSI_FUNC(bright_yellow, BRIGHT_YELLOW); |
| 148 |
DEFINE_ANSI_FUNC(bright_blue, BRIGHT_BLUE); |
| 149 |
DEFINE_ANSI_FUNC(bright_magenta, BRIGHT_MAGENTA); |
| 150 |
DEFINE_ANSI_FUNC(bright_cyan, BRIGHT_CYAN); |
| 151 |
DEFINE_ANSI_FUNC(bright_white, BRIGHT_WHITE); |
| 152 |
|
| 153 |
DEFINE_ANSI_FUNC(highlight_black, HIGHLIGHT_BLACK); |
| 154 |
DEFINE_ANSI_FUNC(highlight_red, HIGHLIGHT_RED); |
| 155 |
DEFINE_ANSI_FUNC(highlight_green, HIGHLIGHT_GREEN); |
| 156 |
DEFINE_ANSI_FUNC_256(highlight_yellow, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK); |
| 157 |
DEFINE_ANSI_FUNC_256(highlight_yellow4, HIGHLIGHT_YELLOW4, HIGHLIGHT_YELLOW_FALLBACK); |
| 158 |
DEFINE_ANSI_FUNC(highlight_blue, HIGHLIGHT_BLUE); |
| 159 |
DEFINE_ANSI_FUNC(highlight_magenta, HIGHLIGHT_MAGENTA); |
| 160 |
DEFINE_ANSI_FUNC(highlight_cyan, HIGHLIGHT_CYAN); |
| 161 |
DEFINE_ANSI_FUNC_256(highlight_grey, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK); |
| 162 |
DEFINE_ANSI_FUNC(highlight_white, HIGHLIGHT_WHITE); |
| 163 |
|
| 164 |
static inline const char* _ansi_highlight_yellow(void) { |
| 165 |
return colors_enabled() ? _ANSI_HIGHLIGHT_YELLOW : ""; |
| 166 |
} |
| 167 |
|
| 168 |
static inline const char* ansi_highlight_green_red(bool b) { |
| 169 |
return b ? ansi_highlight_green() : ansi_highlight_red(); |
| 170 |
} |
| 171 |
|
| 172 |
static inline const char* ansi_underline(void) { |
| 173 |
return underline_enabled() ? ANSI_UNDERLINE : ""; |
| 174 |
} |
| 175 |
|
| 176 |
static inline const char* ansi_add_underline(void) { |
| 177 |
return underline_enabled() ? ANSI_ADD_UNDERLINE : ""; |
| 178 |
} |
| 179 |
|
| 180 |
static inline const char* ansi_add_underline_grey(void) { |
| 181 |
return underline_enabled() ? |
| 182 |
(colors_enabled() ? ANSI_ADD_UNDERLINE_GREY : ANSI_ADD_UNDERLINE) : ""; |
| 183 |
} |
| 184 |
|
| 185 |
static inline const char* ansi_italics(void) { |
| 186 |
/* We hook italics also into the underline checks, close enough */ |
| 187 |
return underline_enabled() ? ANSI_ITALICS : ""; |
| 188 |
} |
| 189 |
|
| 190 |
static inline const char* ansi_add_italics(void) { |
| 191 |
return underline_enabled() ? ANSI_ADD_ITALICS : ""; |
| 192 |
} |
| 193 |
|
| 194 |
#define DEFINE_ANSI_FUNC_UNDERLINE(name, NAME) \ |
| 195 |
static inline const char* ansi_##name##_underline(void) { \ |
| 196 |
return underline_enabled() ? ANSI_##NAME##_UNDERLINE : \ |
| 197 |
ansi_##name(); \ |
| 198 |
} |
| 199 |
|
| 200 |
#define DEFINE_ANSI_FUNC_UNDERLINE_256(name, NAME, FALLBACK) \ |
| 201 |
static inline const char* ansi_##name##_underline(void) { \ |
| 202 |
if (!underline_enabled()) \ |
| 203 |
return ansi_##name(); \ |
| 204 |
\ |
| 205 |
switch (get_color_mode()) { \ |
| 206 |
case COLOR_OFF: return ""; \ |
| 207 |
case COLOR_16: return ANSI_##FALLBACK##_UNDERLINE; \ |
| 208 |
case COLOR_256: return ANSI_##FALLBACK##_UNDERLINE ANSI_##NAME##_UNDERLINE; \ |
| 209 |
default: return ANSI_##NAME##_UNDERLINE; \ |
| 210 |
} \ |
| 211 |
} |
| 212 |
|
| 213 |
DEFINE_ANSI_FUNC_UNDERLINE(highlight, HIGHLIGHT); |
| 214 |
DEFINE_ANSI_FUNC_UNDERLINE(highlight_red, HIGHLIGHT_RED); |
| 215 |
DEFINE_ANSI_FUNC_UNDERLINE(highlight_green, HIGHLIGHT_GREEN); |
| 216 |
DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_yellow, HIGHLIGHT_YELLOW, HIGHLIGHT_YELLOW_FALLBACK); |
| 217 |
DEFINE_ANSI_FUNC_UNDERLINE(highlight_blue, HIGHLIGHT_BLUE); |
| 218 |
DEFINE_ANSI_FUNC_UNDERLINE(highlight_magenta, HIGHLIGHT_MAGENTA); |
| 219 |
DEFINE_ANSI_FUNC_UNDERLINE_256(grey, GREY, BRIGHT_BLACK); |
| 220 |
DEFINE_ANSI_FUNC_UNDERLINE_256(highlight_grey, HIGHLIGHT_GREY, HIGHLIGHT_GREY_FALLBACK); |