From fc65237b019c0632b87bf79c9e8acbb10810b9d4 Mon Sep 17 00:00:00 2001 From: Josenilson Ferreira da SIlva Date: Sun, 12 Apr 2026 07:02:35 -0300 Subject: [PATCH] Fix ANSI colors stripped when TERM=dumb or color system undetectable These patches were developed during Debian rebuild testing with Python 3.13, where all ANSI sequences were silently stripped even with ALLOW_STYLE=ALWAYS. The root cause is that Rich's _detect_color_system() returns None when TERM=dumb, overriding force_terminal=True. Debian build infrastructure always sets TERM=dumb to prevent interactive terminal usage during builds. --- cmd2/rich_utils.py | 5 +++++ tests/test_cmd2.py | 3 +++ 2 files changed, 8 insertions(+) diff --git a/cmd2/rich_utils.py b/cmd2/rich_utils.py index 4708a4e61..5f26446ff 100644 --- a/cmd2/rich_utils.py +++ b/cmd2/rich_utils.py @@ -166,8 +166,11 @@ def __init__( force_terminal: bool | None = None force_interactive: bool | None = None + color_system: str | None = "auto" + if ALLOW_STYLE == AllowStyle.ALWAYS: force_terminal = True + color_system = "truecolor" # Turn off interactive mode if dest is not a terminal which supports it. tmp_console = Console(file=file) @@ -179,6 +182,7 @@ def __init__( file=file, force_terminal=force_terminal, force_interactive=force_interactive, + color_system=color_system, theme=APP_THEME, **kwargs, ) @@ -414,6 +418,7 @@ def rich_text_to_string(text: Text) -> str: console = Console( force_terminal=True, + color_system="truecolor", soft_wrap=True, no_color=False, theme=APP_THEME, diff --git a/tests/test_cmd2.py b/tests/test_cmd2.py index 944870298..a327be04e 100644 --- a/tests/test_cmd2.py +++ b/tests/test_cmd2.py @@ -3756,6 +3756,9 @@ def test_ansi_terminal_tty(mocker, capsys) -> None: app = AnsiApp() mocker.patch.object(app.stdout, 'isatty', return_value=True) mocker.patch.object(sys.stderr, 'isatty', return_value=True) + # Simulate a color-capable terminal: TERMINAL mode respects the TERM env var, + # so TERM=dumb would suppress colors even with isatty=True. + mocker.patch.dict('os.environ', {'TERM': 'xterm-256color'}) app.onecmd_plus_hooks('echo_error oopsie') # if colors are on, the output should have some ANSI style sequences in it