mirror of
https://github.com/Textualize/rich.git
synced 2025-08-17 17:00:21 +00:00
restored pretty.py
This commit is contained in:
parent
c69cc4d056
commit
1716115b97
2 changed files with 28 additions and 30 deletions
|
@ -1,30 +0,0 @@
|
||||||
from dataclasses import dataclass
|
|
||||||
from typing import Any, NamedTuple, Optional, Iterable, List, Tuple
|
|
||||||
|
|
||||||
from .text import Text
|
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Node:
|
|
||||||
braces: Tuple[str, str] = ("", "")
|
|
||||||
children: List["Node"] = []
|
|
||||||
|
|
||||||
|
|
||||||
class StackEntry:
|
|
||||||
node: Node
|
|
||||||
iter_children: Optional[Iterable[Any]] = None
|
|
||||||
|
|
||||||
|
|
||||||
def pretty_format(root_obj: Any):
|
|
||||||
stack: List[StackEntry] = []
|
|
||||||
root = Node()
|
|
||||||
obj = root_obj
|
|
||||||
|
|
||||||
while stack:
|
|
||||||
entry = stack.pop()
|
|
||||||
node, iter_children = entry
|
|
||||||
if iter_children is not None:
|
|
||||||
next(iter_children, None)
|
|
||||||
|
|
||||||
if isinstance(obj, list):
|
|
||||||
node
|
|
28
rich/pretty.py
Normal file
28
rich/pretty.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
from typing import Any, TYPE_CHECKING
|
||||||
|
|
||||||
|
from pprintpp import pformat
|
||||||
|
|
||||||
|
from .measure import Measurement
|
||||||
|
from .highlighter import ReprHighlighter
|
||||||
|
from .text import Text
|
||||||
|
|
||||||
|
if TYPE_CHECKING: # pragma: no cover
|
||||||
|
from .console import Console, ConsoleOptions, HighlighterType, RenderResult
|
||||||
|
|
||||||
|
|
||||||
|
class Pretty:
|
||||||
|
def __init__(self, _object: Any, highlighter: "HighlighterType" = None) -> None:
|
||||||
|
self._object = _object
|
||||||
|
self.highlighter = highlighter or Text
|
||||||
|
|
||||||
|
def __console__(
|
||||||
|
self, console: "Console", options: "ConsoleOptions"
|
||||||
|
) -> "RenderResult":
|
||||||
|
pretty_str = pformat(self._object, width=options.max_width)
|
||||||
|
pretty_str = pretty_str.replace("\r", "")
|
||||||
|
pretty_text = self.highlighter(pretty_str)
|
||||||
|
yield pretty_text
|
||||||
|
|
||||||
|
def __measure__(self, console: "Console", max_width: int) -> "Measurement":
|
||||||
|
text = Text(pformat(self._object, width=max_width))
|
||||||
|
return text.__measure__(console, max_width)
|
Loading…
Add table
Add a link
Reference in a new issue