Skip to content

Commit

Permalink
feat(stream_output): respect FORCE_COLOR (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
branchvincent authored Nov 25, 2024
1 parent aefc890 commit af5eb92
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
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

0 comments on commit af5eb92

Please sign in to comment.