Files
arma-modlist-tools/gui/_io.py
Tran G. (Revernomad) Khoa 85bc406236 fix: smooth GUI during pipeline downloads and harden wizard connection test
GUI log batching (_poll_log now drains queue into a single CTkTextbox.insert
call per 80 ms tick instead of N calls, each with see("end") scroll).

_QueueWriter strips ANSI/CSI escape codes and bare \r before enqueuing so
tqdm progress output is legible in the log textbox. OSC sequences terminated
by both BEL (\x07) and ST (\x1b\) are handled.

Wizard "Test Connection" moved off the main thread: requests.get runs in a
daemon thread; result posted back via after(0, ...). Widget refs captured
before thread launch to prevent stale updates if user navigates away. Bare
except narrowed to TclError (destroyed-widget guard only).

Code quality: import os moved to module level in app.py; _read_raw_config()
helper extracted to deduplicate dual raw config.json reads; return type
annotations added to _get_view_class, _get_dashboard, and cfg property.

Tests: 11 new unit tests for _QueueWriter (RED -> GREEN on OSC-ST fix).
2026-04-08 17:27:25 +07:00

32 lines
984 B
Python

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