diff --git a/cmd2/rich_utils.py b/cmd2/rich_utils.py index 1c46a1573..3646cf4d1 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