Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable Console.print() to deal with ANSI #11

Merged
merged 1 commit into from
Nov 17, 2020
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
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ repos:
additional_dependencies:
- pytest>=6.1.2
- packaging
- rich
- rich>=9.2.0
- repo: https://github.com/pre-commit/mirrors-pylint
rev: v2.6.0
hooks:
- id: pylint
additional_dependencies:
- pytest>=6.1.2
- rich
- rich>=9.2.0
- typing
- typing-extensions
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ console = Console(soft_wrap=True)
console.print(...) # no longer need to pass soft_wrap to each print
```

## Console.print can also deal with ANSI escapes

Extends Rich Console to detect if original text already had ANSI escapes and
decodes it before processing it. This solves the case where printing
output captured from other processes that contained ANSI escapes would brake.
[upstream-404](https://github.com/willmcgugan/rich/discussions/404)

## Soft-wrapping logger

Rich logger assumes that you always have a fixed width console and it does
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ setup_requires =

# These are required in actual runtime:
install_requires =
rich >= 8.0
rich >= 9.2 # first version adding AnsiDecoder

[options.extras_require]
test =
Expand Down
9 changes: 9 additions & 0 deletions src/enrich/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import IO, Any, List, Union

import rich.console as rich_console
from rich.ansi import AnsiDecoder


# Base on private utility class from
Expand Down Expand Up @@ -78,4 +79,12 @@ def print(self, *args, **kwargs) -> None: # type: ignore
# https://github.com/willmcgugan/rich/pull/347/files
if self.soft_wrap and "soft_wrap" not in kwargs:
kwargs["soft_wrap"] = self.soft_wrap

# Currently rich is unable to render ANSI escapes with print so if
# we detect their presence, we decode them.
# https://github.com/willmcgugan/rich/discussions/404
if "\033" in args[0]:
text = format(*args) + "\n"
decoder = AnsiDecoder()
args = list(decoder.decode(text)) # type: ignore
super().print(*args, **kwargs)
15 changes: 15 additions & 0 deletions src/enrich/test/test_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,18 @@ def test_console_soft_wrap() -> None:
console.print(text)
result = console.export_text()
assert text in result


def test_console_print_ansi() -> None:
"""Validates that Console.print() with ANSI does not make break them."""
console = Console(force_terminal=True, record=True, soft_wrap=True, redirect=True)
text = "\033[92mfuture is green!\033[0m"
console.print(text)
text_result = console.export_text(clear=False)
assert "future is green!" in text_result
html_result = console.export_html()
assert "#00ff00" in html_result


if __name__ == "__main__":
test_console_print_ansi()
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ passenv =
setenv =
PIP_DISABLE_VERSION_CHECK=1
PIP_USE_FEATURE={env:PIP_USE_FEATURE:2020-resolver}
PYTEST_REQPASS=4
PYTEST_REQPASS=5
PYTHONDONTWRITEBYTECODE=1
PYTHONUNBUFFERED=1
commands =
Expand Down