Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/343.feat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANSI coloring detection now respects a `FORCE_COLOR` environment variable.
2 changes: 2 additions & 0 deletions src/cleo/io/outputs/stream_output.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ def _has_color_support(self) -> bool:
# Follow https://no-color.org/
if "NO_COLOR" in os.environ:
return False
if "FORCE_COLOR" in os.environ:
return True

if os.getenv("TERM_PROGRAM") == "Hyper":
return True
Expand Down
35 changes: 35 additions & 0 deletions tests/io/outputs/test_stream_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from __future__ import annotations

import os

from io import StringIO

import pytest

from cleo.io.outputs.stream_output import StreamOutput


@pytest.fixture()
def stream() -> StringIO:
return StringIO()


@pytest.mark.parametrize(
["env", "is_decorated"],
[
({"NO_COLOR": "1"}, False),
({"FORCE_COLOR": "1"}, True),
],
)
def test_is_decorated_respects_environment(
stream: StringIO, environ: None, env: dict[str, str], is_decorated: bool
) -> None:
os.environ.update(env)

output = StreamOutput(stream)
output.write_line("<fg=blue>FooBar</>")
stream.seek(0)

assert output.is_decorated() == is_decorated
expected = "\x1b[34mFooBar\x1b[39m\n" if is_decorated else "FooBar\n"
assert stream.read() == expected