Files
libretro/installer/detection_notes.py
T
Abdessamad Derraz 4cb82adce3 refactor: keep detection lines as data
Detection now keeps what it says in DETECTION_NOTES and prints it only in
line mode. The full screen reads the list under detection_held() instead
of capturing stdout and stderr and dropping refusals by comparing text,
which said a refusal twice once screen_text escaped its folder.
2026-11-21 16:44:02 +01:00

40 lines
1.4 KiB
Python

"""What detection says, kept as lines the screen reads and printed as it comes in line mode."""
from __future__ import annotations
import contextlib
import sys
from collections.abc import Iterator
from typing import TextIO
# Each line detection said, in order, other than a folder it could not look
# in (_DETECTION_REFUSED keeps those): a system found with no folder, a
# configuration naming a folder nobody can type, an Android folder out of
# reach, a card that fits two systems.
DETECTION_NOTES: list[str] = []
# Set while the full screen runs detection: it shows the notes and the
# refusals itself, and a line printed under it would tear the screen.
_HELD = {"active": False}
def echo_detection(line: str, stream: TextIO | None = None) -> None:
"""Print a line detection says, unless the screen shows it itself."""
if not _HELD["active"]:
print(line, file=stream if stream is not None else sys.stderr)
def note_detection(line: str, stream: TextIO | None = None) -> None:
"""Keep a line detection says, and print it in line mode."""
DETECTION_NOTES.append(line)
echo_detection(line, stream)
@contextlib.contextmanager
def detection_held() -> Iterator[None]:
"""Detection that keeps its lines without printing them."""
outer = _HELD["active"]
_HELD["active"] = True
try:
yield
finally:
_HELD["active"] = outer