Files
MeshTerm/meshtools/ui/device_picker.py
T
Jean-Pierre MartineauandClaude Opus 4.8 2a9facc56e Replace questionary with a full-screen prompt_toolkit TUI
Introduce a UI surface abstraction (ctx.ui) that tools use for all input
and output instead of touching questionary or the console directly. Two
backends implement it: PlainUi for scripted CLI runs (prints immediately,
byte-for-byte unchanged) and TuiUi for the interactive menu (bounded,
scrollable result windows over a full-screen TuiSession).

The new meshtools/ui/tui package reimplements the slice of questionary the
app used — validated text input, confirm, autocomplete, and selectable
lists — plus framing, progress, and screen management. Swap the dependency
from questionary to prompt_toolkit.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-05 14:09:18 -04:00

64 lines
2.3 KiB
Python

"""Interactive companion-device picker, rendered in the full-screen session.
Shown at the start of the interactive menu (and from the ``devices`` tool) when no port was
given explicitly. It lists the discovered devices, marks the remembered "last known good"
one, and preselects it as the default. The chosen device becomes the session's active
device; it is persisted as the new default once it actually connects.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Optional
from rich.text import Text
from ..core.device_store import RememberedDevice
from ..core.discovery import DiscoveredDevice
from .tui import Choice, Separator
if TYPE_CHECKING:
from .surface import Ui
async def prompt_device(
ui: "Ui",
devices: list[DiscoveredDevice],
remembered: Optional[RememberedDevice],
) -> Optional[DiscoveredDevice]:
"""Prompt the user to choose a companion device.
Args:
ui: The interactive UI surface used to render the picker.
devices: Discovered devices (likely-LoRa first).
remembered: The remembered default, if any, used to mark and preselect a row.
Returns:
The chosen :class:`DiscoveredDevice`, or ``None`` if the user cancelled or chose to
skip device selection (e.g. to run against ``--mock`` / configure later).
"""
if not devices:
await ui.view(
Text.from_markup(
"[warn]No serial devices detected.[/warn]\n"
"Plug one in, pass [accent]--port[/accent], or run with [accent]--mock[/accent]."
),
title="Select a companion device",
)
return None
default: Optional[DiscoveredDevice] = None
items: list = []
for device in devices:
is_remembered = remembered is not None and remembered.matches(device)
star = "★ " if is_remembered else " "
vendor = f" [{device.vendor_label}]" if device.vendor_label else ""
lora = " · likely LoRa" if device.is_likely_lora else ""
items.append(Choice(title=f"{star}{device.label}{vendor}{lora}", value=device))
if is_remembered:
default = device
items.append(Separator(" "))
items.append(Choice(title="skip (use --mock / configure later)", value=None))
return await ui.select("Select a companion device:", items, default=default)