ansi-color: new option SYSTEMD_COLORS=auto-16/auto-256/auto-24bit (#40303)

Setting SYSTEMD_COLORS=auto-16 is like SYSTEMD_COLORS=16 if output is to
a non-dumb TTY, and like SYSTEMD_COLORS=no otherwise.

Relates to
https://github.com/systemd/systemd/issues/15498#issuecomment-1682025186
This commit is contained in:
Peter Oliver
2026-01-23 13:54:31 +01:00
committed by GitHub
parent fa41ce1142
commit af718e0535
5 changed files with 76 additions and 17 deletions
+15 -5
View File
@@ -55,7 +55,7 @@ static ColorMode get_color_mode_impl(void) {
/* First, we check $SYSTEMD_COLORS, which is the explicit way to change the mode. */
ColorMode m = parse_systemd_colors();
if (m >= 0)
if (IN_SET(m, COLOR_OFF, COLOR_16, COLOR_256, COLOR_24BIT))
return m;
/* Next, check for the presence of $NO_COLOR; value is ignored. */
@@ -70,6 +70,13 @@ static ColorMode get_color_mode_impl(void) {
return COLOR_OFF;
/* We failed to figure out any reason to *disable* colors. Let's see how many colors we shall use. */
if (m == COLOR_AUTO_16)
return COLOR_16;
if (m == COLOR_AUTO_256)
return COLOR_256;
if (m == COLOR_AUTO_24BIT)
return COLOR_24BIT;
if (STRPTR_IN_SET(getenv("COLORTERM"),
"truecolor",
"24bit"))
@@ -93,10 +100,13 @@ ColorMode get_color_mode(void) {
static const char* const color_mode_table[_COLOR_MODE_MAX] = {
[COLOR_OFF] = "off",
[COLOR_16] = "16",
[COLOR_256] = "256",
[COLOR_24BIT] = "24bit",
[COLOR_OFF] = "off",
[COLOR_16] = "16",
[COLOR_256] = "256",
[COLOR_24BIT] = "24bit",
[COLOR_AUTO_16] = "auto-16",
[COLOR_AUTO_256] = "auto-256",
[COLOR_AUTO_24BIT] = "auto-24bit",
};
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT);
+7 -4
View File
@@ -5,10 +5,13 @@
/* Limits the use of ANSI colors to a subset. */
typedef enum ColorMode {
COLOR_OFF, /* No colors, monochrome output. */
COLOR_16, /* Only the base 16 colors. */
COLOR_256, /* Only 256 colors. */
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */
COLOR_OFF, /* No colors, monochrome output. */
COLOR_16, /* Only the base 16 colors. */
COLOR_256, /* Only 256 colors. */
COLOR_24BIT, /* For truecolor or 24bit color support, no restriction. */
COLOR_AUTO_16, /* The "AUTO" modes are as the above, but subject to suitable settings for */
COLOR_AUTO_256, /* the environment variables TERM and NO_COLOR. */
COLOR_AUTO_24BIT,
_COLOR_MODE_MAX,
_COLOR_MODE_INVALID = -EINVAL,
} ColorMode;