57 lines
1.3 KiB
Python
57 lines
1.3 KiB
Python
|
|
"""Provides a pretty-printing widget."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
from rich.pretty import Pretty as PrettyRenderable
|
||
|
|
|
||
|
|
from textual.app import RenderResult
|
||
|
|
from textual.widget import Widget
|
||
|
|
|
||
|
|
|
||
|
|
class Pretty(Widget):
|
||
|
|
"""A pretty-printing widget.
|
||
|
|
|
||
|
|
Used to pretty-print any object.
|
||
|
|
"""
|
||
|
|
|
||
|
|
DEFAULT_CSS = """
|
||
|
|
Pretty {
|
||
|
|
height: auto;
|
||
|
|
}
|
||
|
|
"""
|
||
|
|
|
||
|
|
def __init__(
|
||
|
|
self,
|
||
|
|
object: Any,
|
||
|
|
*,
|
||
|
|
name: str | None = None,
|
||
|
|
id: str | None = None,
|
||
|
|
classes: str | None = None,
|
||
|
|
) -> None:
|
||
|
|
"""Initialise the `Pretty` widget.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
object: The object to pretty-print.
|
||
|
|
name: The name of the pretty widget.
|
||
|
|
id: The ID of the pretty in the DOM.
|
||
|
|
classes: The CSS classes of the pretty.
|
||
|
|
"""
|
||
|
|
super().__init__(name=name, id=id, classes=classes)
|
||
|
|
self.shrink = False
|
||
|
|
self._pretty_renderable = PrettyRenderable(object)
|
||
|
|
|
||
|
|
def render(self) -> RenderResult:
|
||
|
|
return self._pretty_renderable
|
||
|
|
|
||
|
|
def update(self, object: object) -> None:
|
||
|
|
"""Update the content of the pretty widget.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
object: The object to pretty-print.
|
||
|
|
"""
|
||
|
|
self._pretty_renderable = PrettyRenderable(object)
|
||
|
|
self.clear_cached_dimensions()
|
||
|
|
self.refresh(layout=True)
|