From 04a645cce5e3645e023c186179e503017e7fac42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rg=20Behrmann?= Date: Sat, 3 Oct 2026 15:55:33 +0200 Subject: [PATCH] ukify: check terminal colour support more thoroughly This adds an extended version of mkosi's terminal_is_dumb that checks that both stdout and stderr are connected to a TTY and also checks that the TERM environment variable is not set to dumb. Additionally this adds support for the NO_COLOR variable. (cherry picked from commit 2c46b11b319e42acb113f659098fad8eeac51b69) --- src/ukify/ukify.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/ukify/ukify.py b/src/ukify/ukify.py index 2277714c17..6fc4f0de29 100755 --- a/src/ukify/ukify.py +++ b/src/ukify/ukify.py @@ -86,12 +86,21 @@ COMMON_NAME_MAX_LEN = 64 INITRD_PCR_POLICY: tuple[list[str], str] = (['enter-initrd'], 'initrd') +def terminal_is_dumb() -> bool: + return ( + not sys.stdout.isatty() + or not sys.stderr.isatty() + or os.getenv('TERM', '') == 'dumb' + or bool(os.getenv('NO_COLOR')) + ) + + class Style: - bold = '\033[0;1;39m' if sys.stderr.isatty() else '' - gray = '\033[0;38;5;245m' if sys.stderr.isatty() else '' - red = '\033[31;1m' if sys.stderr.isatty() else '' - yellow = '\033[33;1m' if sys.stderr.isatty() else '' - reset = '\033[0m' if sys.stderr.isatty() else '' + bold = '\033[0;1;39m' if not terminal_is_dumb() else '' + gray = '\033[0;38;5;245m' if not terminal_is_dumb() else '' + red = '\033[31;1m' if not terminal_is_dumb() else '' + yellow = '\033[33;1m' if not terminal_is_dumb() else '' + reset = '\033[0m' if not terminal_is_dumb() else '' def guess_efi_arch() -> str: