<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>
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
[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
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;
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();
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));
}
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)
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);