Skip to content

Commit a49094a

Browse files
committed
Added method to pretty print.
1 parent b8651b0 commit a49094a

2 files changed

Lines changed: 46 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ prompt is displayed.
8888
- Add support for Python 3.15 by fixing various bugs related to internal `argparse` changes
8989
- Added `common_prefix` method to `cmd2.string_utils` module as a replacement for
9090
`os.path.commonprefix` since that is now deprecated in Python 3.15
91+
- Added `Cmd.ppretty()` as a cmd2-compatible replacement for `rich.pretty.pprint()`.
9192

9293
## 3.4.0 (March 3, 2026)
9394

cmd2/cmd2.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
RenderableType,
8989
)
9090
from rich.highlighter import ReprHighlighter
91+
from rich.pretty import Pretty
9192
from rich.rule import Rule
9293
from rich.style import (
9394
Style,
@@ -1822,6 +1823,50 @@ def ppaged(
18221823
rich_print_kwargs=rich_print_kwargs,
18231824
)
18241825

1826+
def ppretty(
1827+
self,
1828+
obj: Any,
1829+
*,
1830+
file: IO[str] | None = None,
1831+
indent_size: int = 4,
1832+
indent_guides: bool = True,
1833+
max_length: int | None = None,
1834+
max_string: int | None = None,
1835+
max_depth: int | None = None,
1836+
expand_all: bool = False,
1837+
) -> None:
1838+
"""Pretty print an object.
1839+
1840+
This is a cmd2-compatible replacement for rich.pretty.pprint().
1841+
1842+
:param obj: object to pretty print
1843+
:param file: file stream being written to or None for self.stdout.
1844+
Defaults to None.
1845+
:param indent_size: number of spaces in indent. Defaults to 4.
1846+
:param indent_guides: enable indentation guides. Defaults to True.
1847+
:param max_length: maximum length of containers before abbreviating, or None for no abbreviation.
1848+
Defaults to None.
1849+
:param max_string: maximum length of strings before truncating, or None to disable. Defaults to None.
1850+
:param max_depth: maximum depth for nested data structures, or None for unlimited depth. Defaults to None.
1851+
:param expand_all: Expand all containers. Defaults to False.
1852+
"""
1853+
pretty_obj = Pretty(
1854+
obj,
1855+
indent_size=indent_size,
1856+
indent_guides=indent_guides,
1857+
max_length=max_length,
1858+
max_string=max_string,
1859+
max_depth=max_depth,
1860+
expand_all=expand_all,
1861+
overflow="ignore",
1862+
)
1863+
1864+
self.print_to(
1865+
file or self.stdout,
1866+
pretty_obj,
1867+
soft_wrap=True,
1868+
)
1869+
18251870
def get_bottom_toolbar(self) -> list[str | tuple[str, str]] | None:
18261871
"""Get the bottom toolbar content.
18271872

0 commit comments

Comments
 (0)