Skip to content

Commit 9175392

Browse files
committed
New environment var
1 parent 2b5d648 commit 9175392

File tree

4 files changed

+176
-95
lines changed

4 files changed

+176
-95
lines changed

.pre-commit-config.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ repos:
2020
- repo: https://github.com/pre-commit/pygrep-hooks
2121
rev: v1.10.0
2222
hooks:
23-
- id: python-check-mock-methods
2423
- id: python-no-log-warn
2524
- id: python-use-type-annotations
2625
- id: rst-directive-colons

docs/source/console.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,11 +420,15 @@ Rich respects some standard environment variables.
420420

421421
Setting the environment variable ``TERM`` to ``"dumb"`` or ``"unknown"`` will disable color/style and some features that require moving the cursor, such as progress bars.
422422

423-
If the environment variable ``FORCE_COLOR`` is set, then color/styles will be enabled regardless of the value of ``TERM``. This is useful on CI systems which aren't terminals but can none-the-less display ANSI escape sequences.
423+
If the environment variable ``FORCE_COLOR`` is set and non-empty, then color/styles will be enabled regardless of the value of ``TERM``.
424424

425-
If the environment variable ``NO_COLOR`` is set, Rich will disable all color in the output. This takes precedence over ``FORCE_COLOR``. See `no_color <https://no-color.org/>`_ for details.
425+
If the environment variable ``NO_COLOR`` is set, Rich will disable all color in the output. ``NO_COLOR`` takes precedence over ``FORCE_COLOR``. See `no_color <https://no-color.org/>`_ for details.
426426

427427
.. note::
428428
The ``NO_COLOR`` environment variable removes *color* only. Styles such as dim, bold, italic, underline etc. are preserved.
429429

430-
If ``width`` / ``height`` arguments are not explicitly provided as arguments to ``Console`` then the environment variables ``COLUMNS``/``LINES`` can be used to set the console width/height. ``JUPYTER_COLUMNS``/``JUPYTER_LINES`` behave similarly and are used in Jupyter.
430+
The environment variable ``TTY_COMPATIBLE`` is used to override Rich's auto-detection of terminal support. If ``TTY_COMPATIBLE`` is set to ``1`` then rich will assume it is writing to a terminal (or a device that can handle escape sequences). If ``TTY_COMPATIBLE`` is set to ``"0"``, then Rich will assume that it is not writing to a terminal. If the variable is not set, or any other value, then Rich will attempt to auto-detect terminal support. If you want Rich output in CI or Github Actions, then you should set ``TTY_COMPATIBLE=1``.
431+
432+
Note that these variable set the default behavior. If you explicitly set ``force_terminal`` in the Console constructor, then this will take precedence over the environment variable.
433+
434+
If ``width`` / ``height`` arguments are not explicitly provided as arguments to ``Console`` then the environment variables ``COLUMNS`` / ``LINES`` can be used to set the console width / height. ``JUPYTER_COLUMNS`` / ``JUPYTER_LINES`` behave similarly and are used in Jupyter.

rich/console.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def group(fit: bool = True) -> Callable[..., Callable[..., Group]]:
500500
"""
501501

502502
def decorator(
503-
method: Callable[..., Iterable[RenderableType]]
503+
method: Callable[..., Iterable[RenderableType]],
504504
) -> Callable[..., Group]:
505505
"""Convert a method that returns an iterable of renderables in to a Group."""
506506

@@ -933,11 +933,13 @@ def is_terminal(self) -> bool:
933933
934934
Returns:
935935
bool: True if the console writing to a device capable of
936-
understanding terminal codes, otherwise False.
936+
understanding escape sequences, otherwise False.
937937
"""
938+
# If dev has explicitly set this value, return it
938939
if self._force_terminal is not None:
939940
return self._force_terminal
940941

942+
# Fudge for Idle
941943
if hasattr(sys.stdin, "__module__") and sys.stdin.__module__.startswith(
942944
"idlelib"
943945
):
@@ -948,12 +950,22 @@ def is_terminal(self) -> bool:
948950
# return False for Jupyter, which may have FORCE_COLOR set
949951
return False
950952

951-
# If FORCE_COLOR env var has any value at all, we assume a terminal.
952-
force_color = self._environ.get("FORCE_COLOR")
953-
if force_color is not None:
954-
self._force_terminal = True
953+
environ = self._environ
954+
955+
tty_compatible = environ.get("TTY_COMPATIBLE", "")
956+
# 0 indicates device is not tty compatible
957+
if tty_compatible == "0":
958+
return False
959+
# 1 indicates device is tty compatible
960+
if tty_compatible == "1":
955961
return True
956962

963+
# https://force-color.org/
964+
force_color = environ.get("FORCE_COLOR")
965+
if force_color is not None:
966+
return force_color != ""
967+
968+
# Any other value defaults to auto detect
957969
isatty: Optional[Callable[[], bool]] = getattr(self.file, "isatty", None)
958970
try:
959971
return False if isatty is None else isatty()

0 commit comments

Comments
 (0)