Use Text.from_markup() for strings containing Rich tags so [red]...[/red] renders as colored text instead of raw tags. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
34 lines
1.0 KiB
Python
34 lines
1.0 KiB
Python
from rich.text import Text
|
|
from textual.app import ComposeResult
|
|
from textual.screen import ModalScreen
|
|
from textual.widgets import RichLog, Button, Static
|
|
from textual.containers import Vertical
|
|
|
|
|
|
class OperationLog(ModalScreen[None]):
|
|
|
|
BINDINGS = [("escape", "close", "Close")]
|
|
|
|
def __init__(self, title: str = "Operation Output"):
|
|
super().__init__()
|
|
self._title = title
|
|
|
|
def compose(self) -> ComposeResult:
|
|
with Vertical(id="op-log"):
|
|
yield Static(self._title, id="ol-title")
|
|
yield RichLog(id="ol-log", wrap=True, highlight=True, markup=True)
|
|
yield Button("Close", variant="primary", id="ol-close")
|
|
|
|
def on_button_pressed(self, event: Button.Pressed) -> None:
|
|
self.dismiss(None)
|
|
|
|
def action_close(self) -> None:
|
|
self.dismiss(None)
|
|
|
|
def write(self, text: str) -> None:
|
|
log = self.query_one("#ol-log", RichLog)
|
|
if "[" in text and "[/" in text:
|
|
log.write(Text.from_markup(text))
|
|
else:
|
|
log.write(text)
|