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

Parse escape codes in Pretty repr #2470

Merged
merged 6 commits into from Aug 17, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Parse ANSI escape sequences in pretty repr https://github.com/Textualize/rich/pull/2470
- Add support for `FORCE_COLOR` env var https://github.com/Textualize/rich/pull/2449

### Fixed
Expand Down
10 changes: 8 additions & 2 deletions rich/pretty.py
Expand Up @@ -333,7 +333,7 @@ def __rich_console__(
max_depth=self.max_depth,
expand_all=self.expand_all,
)
pretty_text = Text(
pretty_text = Text.from_ansi(
pretty_str,
justify=self.justify or options.justify,
overflow=self.overflow or options.overflow,
Expand Down Expand Up @@ -1007,4 +1007,10 @@ class StockKeepingUnit(NamedTuple):

from rich import print

print(Pretty(data, indent_guides=True, max_string=20))
# print(Pretty(data, indent_guides=True, max_string=20))

class Thing:
def __repr__(self) -> str:
return "Hello\x1b[38;5;239m World!"

print(Pretty(Thing()))
9 changes: 3 additions & 6 deletions rich/repr.py
@@ -1,21 +1,18 @@
from functools import partial
import inspect
import sys

from functools import partial
from typing import (
Any,
Callable,
Iterable,
List,
Optional,
overload,
Union,
Tuple,
Type,
TypeVar,
Union,
overload,
)


T = TypeVar("T")


Expand Down
14 changes: 14 additions & 0 deletions tests/test_pretty.py
Expand Up @@ -266,6 +266,20 @@ def test_small_width():
assert result == expected


def test_ansi_in_pretty_repr():
class Hello:
def __repr__(self):
return "Hello \x1b[38;5;239mWorld!"

pretty = Pretty(Hello())

console = Console(file=io.StringIO(), record=True)
console.print(pretty)
result = console.export_text()

assert result == "Hello World!\n"


@skip_py36
def test_broken_repr():
class BrokenRepr:
Expand Down