ansi-color: follow-ups for $SYSTEMD_COLOR=auto-* (#40442)

Follow-ups for #40303
This commit is contained in:
Yu Watanabe
2026-01-28 16:00:38 +09:00
committed by GitHub
6 changed files with 38 additions and 22 deletions

View File

@@ -225,15 +225,16 @@
<term><varname>$SYSTEMD_COLORS</varname></term>
<listitem>
<para>Takes a boolean argument, or a special value.</para>
<para>Takes a boolean argument, or a special value. By default (unset), <command>systemd</command>
and related utilities will use colors in their output if possible. If <varname>$COLORTERM</varname>
is set to <literal>truecolor</literal> or <literal>24bit</literal>, 24-bit colors will be enabled,
256 colors otherwise, unless <varname>$NO_COLOR</varname> or <varname>$TERM</varname> indicates
colors are disabled.</para>
<variablelist>
<varlistentry>
<term><option>true</option></term>
<listitem><para>The default. <command>systemd</command> and related utilities will use colors in
their output if possible. Same as <literal>auto-24bit</literal> if <varname>$COLORTERM</varname>
is set to <literal>truecolor</literal> or <literal>24bit</literal>; same as
<literal>auto-256</literal> otherwise.</para></listitem>
<listitem><para>Same as unset, except that <varname>$NO_COLOR</varname> is ignored.</para></listitem>
</varlistentry>
<varlistentry>

View File

@@ -55,11 +55,11 @@ 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 (IN_SET(m, COLOR_OFF, COLOR_16, COLOR_256, COLOR_24BIT))
if (m >= 0 && m < _COLOR_MODE_FIXED_MAX)
return m;
/* Next, check for the presence of $NO_COLOR; value is ignored. */
if (getenv("NO_COLOR"))
if (m != COLOR_TRUE && getenv("NO_COLOR"))
return COLOR_OFF;
/* If the above didn't work, we turn colors off unless we are on a TTY. And if we are on a TTY we
@@ -92,13 +92,14 @@ static ColorMode get_color_mode_impl(void) {
}
ColorMode get_color_mode(void) {
if (cached_color_mode < 0)
if (cached_color_mode < 0) {
cached_color_mode = get_color_mode_impl();
assert(cached_color_mode >= 0 && cached_color_mode < _COLOR_MODE_FIXED_MAX);
}
return cached_color_mode;
}
static const char* const color_mode_table[_COLOR_MODE_MAX] = {
[COLOR_OFF] = "off",
[COLOR_16] = "16",
@@ -107,9 +108,10 @@ static const char* const color_mode_table[_COLOR_MODE_MAX] = {
[COLOR_AUTO_16] = "auto-16",
[COLOR_AUTO_256] = "auto-256",
[COLOR_AUTO_24BIT] = "auto-24bit",
[COLOR_TRUE] = "true",
};
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_24BIT);
DEFINE_STRING_TABLE_LOOKUP_WITH_BOOLEAN(color_mode, ColorMode, COLOR_TRUE);
/*
* Check that the string is formatted like an ANSI color code, i.e. that it consists of one or more

View File

@@ -9,9 +9,17 @@ typedef enum ColorMode {
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_MODE_FIXED_MAX,
/* The "AUTO" modes are as the above, but subject to suitable settings for the environment variables
* TERM and NO_COLOR. */
COLOR_AUTO_16 = _COLOR_MODE_FIXED_MAX,
COLOR_AUTO_256,
COLOR_AUTO_24BIT,
/* Same as default (unset), except that $NO_COLOR is ignored/overridden */
COLOR_TRUE,
_COLOR_MODE_MAX,
_COLOR_MODE_INVALID = -EINVAL,
} ColorMode;

View File

@@ -849,14 +849,18 @@ TEST(table_ansi) {
TABLE_STRING_WITH_ANSI, ANSI_GREY "thisisgrey"));
unsigned saved_columns = columns();
bool saved_color = colors_enabled();
_cleanup_free_ char *saved_term = NULL;
const char *e = getenv("TERM");
_cleanup_free_ char *saved_term = NULL, *saved_color = NULL;
const char *e;
e = getenv("TERM");
if (e)
ASSERT_NOT_NULL((saved_term = strdup(e)));
e = getenv("SYSTEMD_COLORS");
if (e)
ASSERT_NOT_NULL((saved_color = strdup(e)));
ASSERT_OK_ERRNO(setenv("COLUMNS", "200", /* overwrite= */ true));
ASSERT_OK_ERRNO(setenv("SYSTEMD_COLORS", "1", /* overwrite= */ true));
ASSERT_OK_ERRNO(setenv("SYSTEMD_COLORS", "24bit", /* overwrite= */ true));
ASSERT_OK_ERRNO(setenv("TERM", FALLBACK_TERM, /* overwrite= */ true));
reset_terminal_feature_caches();
@@ -904,7 +908,7 @@ TEST(table_ansi) {
ASSERT_OK(sd_json_variant_dump(j, SD_JSON_FORMAT_COLOR_AUTO|SD_JSON_FORMAT_PRETTY_AUTO, /* f= */ NULL, /* prefix= */ NULL));
ASSERT_OK(setenvf("COLUMNS", /* overwrite= */ true, "%u", saved_columns));
ASSERT_OK(setenvf("SYSTEMD_COLORS", /* overwrite= */ true, "%i", saved_color));
ASSERT_OK(set_unset_env("SYSTEMD_COLORS", saved_color, /* overwrite= */ true));
ASSERT_OK(set_unset_env("TERM", saved_term, /* overwrite= */ true));
}

View File

@@ -11,7 +11,7 @@ static int run(int argc, char **argv) {
test_setup_logging(LOG_DEBUG);
assert_se(setenv("SYSTEMD_COLORS", "1", 1) == 0); /* Force the qrcode to be printed */
assert_se(setenv("SYSTEMD_COLORS", "24bit", 1) == 0); /* Force the qrcode to be printed */
r = print_qrcode(stdout, "This should say \"TEST\"", "TEST");
if (r == -EOPNOTSUPP)

View File

@@ -332,18 +332,19 @@ TEST(get_color_mode) {
test_get_color_mode_with_env("SYSTEMD_COLORS", "no", COLOR_OFF);
test_get_color_mode_with_env("SYSTEMD_COLORS", "16", COLOR_16);
test_get_color_mode_with_env("SYSTEMD_COLORS", "256", COLOR_256);
test_get_color_mode_with_env("SYSTEMD_COLORS", "1", COLOR_24BIT);
test_get_color_mode_with_env("SYSTEMD_COLORS", "yes", COLOR_24BIT);
test_get_color_mode_with_env("SYSTEMD_COLORS", "24bit", COLOR_24BIT);
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-16", terminal_is_dumb() ? COLOR_OFF : COLOR_16);
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-256", terminal_is_dumb() ? COLOR_OFF : COLOR_256);
ASSERT_OK_ERRNO(setenv("COLORTERM", "truecolor", true));
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-24bit", terminal_is_dumb() ? COLOR_OFF : COLOR_24BIT);
ASSERT_OK_ERRNO(setenv("COLORTERM", "truecolor", true));
test_get_color_mode_with_env("SYSTEMD_COLORS", "1", terminal_is_dumb() ? COLOR_OFF : COLOR_24BIT);
test_get_color_mode_with_env("SYSTEMD_COLORS", "yes", terminal_is_dumb() ? COLOR_OFF : COLOR_24BIT);
ASSERT_OK_ERRNO(unsetenv("COLORTERM"));
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-24bit", terminal_is_dumb() ? COLOR_OFF : COLOR_256);
test_get_color_mode_with_env("SYSTEMD_COLORS", "true", terminal_is_dumb() ? COLOR_OFF : COLOR_256);
ASSERT_OK_ERRNO(setenv("NO_COLOR", "1", true));
test_get_color_mode_with_env("SYSTEMD_COLORS", "true", terminal_is_dumb() ? COLOR_OFF : COLOR_256);
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-16", COLOR_OFF);
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-256", COLOR_OFF);
test_get_color_mode_with_env("SYSTEMD_COLORS", "auto-24bit", COLOR_OFF);