Skip to content

Commit fc65237

Browse files
committed
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.
1 parent 1d6910a commit fc65237

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

cmd2/rich_utils.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,11 @@ def __init__(
166166
force_terminal: bool | None = None
167167
force_interactive: bool | None = None
168168

169+
color_system: str | None = "auto"
170+
169171
if ALLOW_STYLE == AllowStyle.ALWAYS:
170172
force_terminal = True
173+
color_system = "truecolor"
171174

172175
# Turn off interactive mode if dest is not a terminal which supports it.
173176
tmp_console = Console(file=file)
@@ -179,6 +182,7 @@ def __init__(
179182
file=file,
180183
force_terminal=force_terminal,
181184
force_interactive=force_interactive,
185+
color_system=color_system,
182186
theme=APP_THEME,
183187
**kwargs,
184188
)
@@ -414,6 +418,7 @@ def rich_text_to_string(text: Text) -> str:
414418

415419
console = Console(
416420
force_terminal=True,
421+
color_system="truecolor",
417422
soft_wrap=True,
418423
no_color=False,
419424
theme=APP_THEME,

tests/test_cmd2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3756,6 +3756,9 @@ def test_ansi_terminal_tty(mocker, capsys) -> None:
37563756
app = AnsiApp()
37573757
mocker.patch.object(app.stdout, 'isatty', return_value=True)
37583758
mocker.patch.object(sys.stderr, 'isatty', return_value=True)
3759+
# Simulate a color-capable terminal: TERMINAL mode respects the TERM env var,
3760+
# so TERM=dumb would suppress colors even with isatty=True.
3761+
mocker.patch.dict('os.environ', {'TERM': 'xterm-256color'})
37593762

37603763
app.onecmd_plus_hooks('echo_error oopsie')
37613764
# if colors are on, the output should have some ANSI style sequences in it

0 commit comments

Comments
 (0)