diff --git a/man/common-variables.xml b/man/common-variables.xml
index 93f18d453e5..0f807b2cde1 100644
--- a/man/common-variables.xml
+++ b/man/common-variables.xml
@@ -225,15 +225,16 @@
$SYSTEMD_COLORS
- Takes a boolean argument, or a special value.
+ Takes a boolean argument, or a special value. By default (unset), systemd
+ and related utilities will use colors in their output if possible. If $COLORTERM
+ is set to truecolor or 24bit, 24-bit colors will be enabled,
+ 256 colors otherwise, unless $NO_COLOR or $TERM indicates
+ colors are disabled.
- The default. systemd and related utilities will use colors in
- their output if possible. Same as auto-24bit if $COLORTERM
- is set to truecolor or 24bit; same as
- auto-256 otherwise.
+ Same as unset, except that $NO_COLOR is ignored.
diff --git a/src/basic/ansi-color.c b/src/basic/ansi-color.c
index 5b04199318a..e5c78c93458 100644
--- a/src/basic/ansi-color.c
+++ b/src/basic/ansi-color.c
@@ -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
diff --git a/src/basic/ansi-color.h b/src/basic/ansi-color.h
index fb6b2bba0c2..e686e1167b1 100644
--- a/src/basic/ansi-color.h
+++ b/src/basic/ansi-color.h
@@ -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;
diff --git a/src/test/test-format-table.c b/src/test/test-format-table.c
index a2e142791ff..7b501d11e42 100644
--- a/src/test/test-format-table.c
+++ b/src/test/test-format-table.c
@@ -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));
}
diff --git a/src/test/test-qrcode-util.c b/src/test/test-qrcode-util.c
index 3545c656fb2..dc2573b4f84 100644
--- a/src/test/test-qrcode-util.c
+++ b/src/test/test-qrcode-util.c
@@ -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)
diff --git a/src/test/test-terminal-util.c b/src/test/test-terminal-util.c
index d2bab189499..4e2b751ad26 100644
--- a/src/test/test-terminal-util.c
+++ b/src/test/test-terminal-util.c
@@ -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);