tools: add support for NO_COLOR/FORCE_COLOR

Environment variables to control whether the output should not have
color or must have color, regardless of the tty-ness of the output
stream.

Part-of: <https://gitlab.freedesktop.org/libinput/libinput/-/merge_requests/1348>
This commit is contained in:
Peter Hutterer 2025-10-27 12:35:05 +10:00
parent 141f571aae
commit 07a9161ef2

View file

@ -62,14 +62,20 @@ log_handler(struct libinput *li,
const char *format, const char *format,
va_list args) va_list args)
{ {
static int is_tty = -1; static int use_color = -1;
static uint32_t last_dispatch_no = 0; static uint32_t last_dispatch_no = 0;
static bool color_toggle = false; static bool color_toggle = false;
if (is_tty == -1) if (use_color == -1) {
is_tty = isatty(STDOUT_FILENO); if (getenv("NO_COLOR"))
use_color = 0;
else if (getenv("FORCE_COLOR"))
use_color = 1;
else
use_color = isatty(STDOUT_FILENO);
}
if (is_tty) { if (use_color) {
if (priority >= LIBINPUT_LOG_PRIORITY_ERROR) { if (priority >= LIBINPUT_LOG_PRIORITY_ERROR) {
if (strstr(format, "client bug: ") || if (strstr(format, "client bug: ") ||
strstr(format, "libinput bug: ") || strstr(format, "libinput bug: ") ||
@ -97,7 +103,7 @@ log_handler(struct libinput *li,
} }
vprintf(format, args); vprintf(format, args);
if (is_tty) if (use_color)
printf(ANSI_NORMAL); printf(ANSI_NORMAL);
log_serial++; log_serial++;