From 21f762d668d49499885dab0c6b5958b6d700d020 Mon Sep 17 00:00:00 2001 From: The MMGen Project Date: Mon, 9 Nov 2026 09:33:33 +0000 Subject: [PATCH] color: clean up `get_terminfo_colors()` --- mmgen/color.py | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/mmgen/color.py b/mmgen/color.py index b5c0d365..9c3ddec6 100644 --- a/mmgen/color.py +++ b/mmgen/color.py @@ -51,24 +51,16 @@ def nocolor(s): return s def get_terminfo_colors(term=None): - from subprocess import run, PIPE - cmd = ['infocmp', '-0'] - if term: - cmd.append(term) - + from subprocess import run, PIPE, CalledProcessError + cmd = ['infocmp', '-1'] + ([term] if term else []) try: cmdout = run(cmd, stdout=PIPE, check=True, text=True).stdout - except FileNotFoundError: + except (FileNotFoundError, CalledProcessError): return None else: - s = next(iter(e.split('#', 1)[1] for e in cmdout.split(',') if e.startswith('colors'))) - from .util import is_hex_str - if s.isdecimal(): - return int(s) - elif s.startswith('0x') and is_hex_str(s[2:]): - return int(s, 16) - else: - return None + s = next(iter(line.strip().removeprefix('colors#').removesuffix(',') + for line in cmdout.splitlines() if line.lstrip().startswith('colors#'))) + return int(s, 16) if s.startswith('0x') else int(s) if s.isdecimal() else None def init_color(num_colors='auto'): assert num_colors in ('auto', 8, 16, 256, 0)