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

feat: receive custom depth on rich.pretty ✨ #1605

Closed
Closed
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 CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ The following people have contributed to the development of Rich:

<!-- Add your name below, sort alphabetically by surname. Link to Github profile / your home page. -->

- [Meysam Azad](https://github.com/meysam81)
- [Gregory Beauregard](https://github.com/GBeauregard/pyffstream)
- [Pete Davison](https://github.com/pd93)
- [Oleksis Fraga](https://github.com/oleksis)
Expand Down
7 changes: 6 additions & 1 deletion rich/pretty.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import builtins
import os
from rich.repr import RichReprResult
Expand Down Expand Up @@ -821,6 +822,10 @@ def __repr__(self) -> str:
}
data["foo"].append(data) # type: ignore

parser = argparse.ArgumentParser()
parser.add_argument("-d", "--depth", type=int, default=4)
parsed = parser.parse_args(sys.argv[1:])

from rich import print

print(Pretty(data, indent_guides=True, max_string=20))
print(Pretty(data, indent_guides=True, max_string=20, indent_size=parsed.depth))
34 changes: 34 additions & 0 deletions tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,40 @@ def test_indent_lines():
assert result == expected


def test_custom_depth():
custom_indent = 16

data = {
"key": "value",
"list": ["el1", "el2"],
"dict": {"list": ["sub-el1", "sub-el2"]},
}
expected = """{
│ 'key': 'value',
│ 'list': [
│ │ 'el1',
│ │ 'el2'
│ ],
│ 'dict': {
│ │ 'list': [
│ │ │ 'sub-el1',
│ │ │ 'sub-el2'
│ │ ]
│ }
}
"""

console = Console(width=100, color_system=None)
console.begin_capture()
console.print(
Pretty(data, indent_guides=True, expand_all=True, indent_size=custom_indent)
)
result = console.end_capture()
print(repr(result))
print(result)
assert result == expected


def test_pprint():
console = Console(color_system=None)
console.begin_capture()
Expand Down