diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b2800e89..4b95a4790 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,8 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). + ## [Unreleased] +### Added + +- Add support for `FORCE_COLOR` env var https://github.com/Textualize/rich/pull/2449 + ### Fixed - Fix NO_COLOR support on legacy Windows https://github.com/Textualize/rich/pull/2458 diff --git a/rich/console.py b/rich/console.py index 4f4675b10..4a3ebb559 100644 --- a/rich/console.py +++ b/rich/console.py @@ -697,7 +697,12 @@ def __init__( self._height = height self._color_system: Optional[ColorSystem] - self._force_terminal = force_terminal + + if force_terminal is not None: + self._force_terminal = force_terminal + else: + self._force_terminal = self._environ.get("FORCE_COLOR") is not None + self._file = file self.quiet = quiet self.stderr = stderr diff --git a/tests/test_console.py b/tests/test_console.py index 2db88af0b..07692f198 100644 --- a/tests/test_console.py +++ b/tests/test_console.py @@ -895,3 +895,11 @@ def test_render_lines_height_minus_vertical_pad_is_negative(): # Ensuring that no exception is raised... console.render_lines(Padding("hello", pad=(1, 0)), options=options) + + +@mock.patch.dict(os.environ, {"FORCE_COLOR": "anything"}) +def test_force_color(): + # Even though we use a non-tty file, the presence of FORCE_COLOR env var + # means is_terminal returns True. + console = Console(file=io.StringIO()) + assert console.is_terminal