Skip to content

Commit 2cb95e2

Browse files
committed
Fixed handling of NO_COLOR environment variable.
1 parent cf99bc3 commit 2cb95e2

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

‎cmd2/pt_utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ def pt_filter_style(text: str | ANSI) -> str | ANSI:
7979

8080
def pt_resolve_color_depth() -> ColorDepth:
8181
"""Determine the prompt-toolkit ColorDepth based on NO_COLOR and ru.ALLOW_STYLE."""
82-
if "NO_COLOR" in os.environ or ru.ALLOW_STYLE == ru.AllowStyle.NEVER:
82+
if os.environ.get("NO_COLOR") or ru.ALLOW_STYLE == ru.AllowStyle.NEVER:
8383
return ColorDepth.DEPTH_1_BIT
8484
return ColorDepth.TRUE_COLOR
8585

‎tests/test_pt_utils.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,15 @@ def test_pt_filter_style_never() -> None:
100100

101101
@with_ansi_style(ru.AllowStyle.ALWAYS)
102102
def test_pt_resolve_color_depth_always(monkeypatch) -> None:
103-
# Ensure any host NO_COLOR environment variable doesn't affect the test
103+
# Clear NO_COLOR to ensure colors are not suppressed
104104
monkeypatch.delenv("NO_COLOR", raising=False)
105105
assert pt_resolve_color_depth() == ColorDepth.TRUE_COLOR
106106

107107

108108
@with_ansi_style(ru.AllowStyle.TERMINAL)
109109
def test_pt_resolve_color_depth_terminal(monkeypatch) -> None:
110-
# Ensure any host NO_COLOR environment variable doesn't affect the test
111-
monkeypatch.delenv("NO_COLOR", raising=False)
110+
# Mock NO_COLOR to an empty string to ensure colors are not suppressed
111+
monkeypatch.setenv("NO_COLOR", "")
112112
assert pt_resolve_color_depth() == ColorDepth.TRUE_COLOR
113113

114114

@@ -121,7 +121,7 @@ def test_pt_resolve_color_depth_never(monkeypatch) -> None:
121121

122122
@with_ansi_style(ru.AllowStyle.ALWAYS)
123123
def test_pt_resolve_color_depth_no_color(monkeypatch) -> None:
124-
# Mock NO_COLOR=1 to ensure colors are suppressed
124+
# Mock NO_COLOR to ensure colors are suppressed
125125
monkeypatch.setenv("NO_COLOR", "1")
126126
assert pt_resolve_color_depth() == ColorDepth.DEPTH_1_BIT
127127

0 commit comments

Comments
 (0)