from __future__ import annotations import io import queue import re # Strip ANSI escape sequences and normalise carriage returns so tqdm output # is readable in the log textbox (which has no terminal emulation). _ANSI_RE = re.compile( r"\x1b\[[0-9;]*[A-Za-z]" # CSI sequences e.g. \x1b[32m r"|\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)" # OSC sequences, BEL or ST terminator ) class _QueueWriter(io.TextIOBase): """Redirect sys.stdout / sys.stderr into a Queue for the Logs panel.""" def __init__(self, q: queue.Queue[str]) -> None: self._q = q def write(self, text: str) -> int: # type: ignore[override] if text: cleaned = _ANSI_RE.sub("", text) cleaned = cleaned.replace("\r\n", "\n") # Windows CRLF → LF cleaned = cleaned.replace("\r", "\n") # bare CR → newline if cleaned: self._q.put(cleaned) return len(text) def flush(self) -> None: pass