|
88 | 88 | RenderableType, |
89 | 89 | ) |
90 | 90 | from rich.highlighter import ReprHighlighter |
| 91 | +from rich.pretty import Pretty |
91 | 92 | from rich.rule import Rule |
92 | 93 | from rich.style import ( |
93 | 94 | Style, |
@@ -1822,6 +1823,56 @@ def ppaged( |
1822 | 1823 | rich_print_kwargs=rich_print_kwargs, |
1823 | 1824 | ) |
1824 | 1825 |
|
| 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 | + end: str = "\n", |
| 1838 | + ) -> None: |
| 1839 | + """Pretty print an object. |
| 1840 | +
|
| 1841 | + This is a cmd2-compatible replacement for rich.pretty.pprint(). |
| 1842 | +
|
| 1843 | + :param obj: object to pretty print |
| 1844 | + :param file: file stream being written to or None for self.stdout. |
| 1845 | + Defaults to None. |
| 1846 | + :param indent_size: number of spaces in indent. Defaults to 4. |
| 1847 | + :param indent_guides: enable indentation guides. Defaults to True. |
| 1848 | + :param max_length: maximum length of containers before abbreviating, or None for no abbreviation. |
| 1849 | + Defaults to None. |
| 1850 | + :param max_string: maximum length of strings before truncating, or None to disable. Defaults to None. |
| 1851 | + :param max_depth: maximum depth for nested data structures, or None for unlimited depth. Defaults to None. |
| 1852 | + :param expand_all: Expand all containers. Defaults to False. |
| 1853 | + :param end: string to write at end of printed text. Defaults to a newline. |
| 1854 | + """ |
| 1855 | + # The overflow and soft_wrap values match those in rich.pretty.pprint(). |
| 1856 | + # This ensures long strings are neither truncated with ellipses nor broken |
| 1857 | + # up by injected newlines. |
| 1858 | + pretty_obj = Pretty( |
| 1859 | + obj, |
| 1860 | + indent_size=indent_size, |
| 1861 | + indent_guides=indent_guides, |
| 1862 | + max_length=max_length, |
| 1863 | + max_string=max_string, |
| 1864 | + max_depth=max_depth, |
| 1865 | + expand_all=expand_all, |
| 1866 | + overflow="ignore", |
| 1867 | + ) |
| 1868 | + |
| 1869 | + self.print_to( |
| 1870 | + file or self.stdout, |
| 1871 | + pretty_obj, |
| 1872 | + soft_wrap=True, |
| 1873 | + end=end, |
| 1874 | + ) |
| 1875 | + |
1825 | 1876 | def get_bottom_toolbar(self) -> list[str | tuple[str, str]] | None: |
1826 | 1877 | """Get the bottom toolbar content. |
1827 | 1878 |
|
|
0 commit comments