Skip to content

Commit b8a2cf0

Browse files
authored
Merge pull request #2449 from Textualize/force-color
Force color
2 parents 5d3f600 + 193266c commit b8a2cf0

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
89
## [Unreleased]
910

11+
### Added
12+
13+
- Add support for `FORCE_COLOR` env var https://github.com/Textualize/rich/pull/2449
14+
1015
### Fixed
1116

1217
- Fix NO_COLOR support on legacy Windows https://github.com/Textualize/rich/pull/2458

rich/console.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,12 @@ def __init__(
697697
self._height = height
698698

699699
self._color_system: Optional[ColorSystem]
700-
self._force_terminal = force_terminal
700+
701+
if force_terminal is not None:
702+
self._force_terminal = force_terminal
703+
else:
704+
self._force_terminal = self._environ.get("FORCE_COLOR") is not None
705+
701706
self._file = file
702707
self.quiet = quiet
703708
self.stderr = stderr

tests/test_console.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,3 +895,11 @@ def test_render_lines_height_minus_vertical_pad_is_negative():
895895

896896
# Ensuring that no exception is raised...
897897
console.render_lines(Padding("hello", pad=(1, 0)), options=options)
898+
899+
900+
@mock.patch.dict(os.environ, {"FORCE_COLOR": "anything"})
901+
def test_force_color():
902+
# Even though we use a non-tty file, the presence of FORCE_COLOR env var
903+
# means is_terminal returns True.
904+
console = Console(file=io.StringIO())
905+
assert console.is_terminal

0 commit comments

Comments
 (0)