Add a TYPE column and name known devices on the picker
Reworks the startup device picker so it reads honestly at a glance: - VENDOR is now just the hardware maker. A BLE advert rarely carries one (confirmed against real hardware: the companion advertises no manufacturer_data), so it stays blank instead of mislabelling the transport "Bluetooth" as a vendor. - A new TYPE column marks the transport with an icon: a plug emoji for wired serial, and the Bjarkan rune the Bluetooth logo is drawn from, rendered white on Bluetooth blue and flanked by half-block slivers so it reads as a slim rounded badge. - Every confirmed companion (not just the most-recent one) is shown by the mesh node name learned at connect time, so a known COM11 reads as its node name rather than the OS's generic "USB Serial Device". - Confirmed companions sort to the top, most-recently-used first, and wear their name in white so they stand out from merely-detected ports. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
7850587e2a
commit
6d51b4e2ab
5 files changed
+179
-24
No files matched your search
@@ -147,13 +147,17 @@ class DiscoveredDevice:
|
||||
|
||||
@property
|
||||
def vendor_label(self) -> str:
|
||||
"""A friendly vendor/transport name for display.
|
||||
"""The hardware maker's name for the VENDOR column, or ``""`` when unknown.
|
||||
|
||||
``"Bluetooth"`` for a BLE device, else a known-VID vendor name or the USB
|
||||
manufacturer string.
|
||||
This is deliberately *just the vendor* — the transport (USB vs Bluetooth) is a
|
||||
separate concern shown in its own TYPE column, so a BLE advert (which rarely
|
||||
exposes a maker) reports whatever manufacturer string it carries and otherwise
|
||||
stays blank rather than mislabelling the transport as a vendor. A serial port maps
|
||||
its USB vendor ID to a friendly name, falling back to the driver's manufacturer
|
||||
string.
|
||||
"""
|
||||
if self.is_ble:
|
||||
return "Bluetooth"
|
||||
return self.manufacturer or ""
|
||||
if self.vid in KNOWN_LORA_VIDS:
|
||||
return KNOWN_LORA_VIDS[self.vid]
|
||||
return self.manufacturer or ""
|
||||
|
||||
+104
-19
@@ -3,9 +3,14 @@
|
||||
Shown once at the start of the interactive menu when no port was given explicitly. Unlike the
|
||||
in-menu prompts, it is drawn as a chromeless splash — the MeshTerm wordmark centered above a
|
||||
content-sized box, with no header/footer status bars. It lists the discovered devices in
|
||||
aligned columns, tags the ones already confirmed as MeshCore companions, marks the remembered
|
||||
aligned columns — name, connection target, a TYPE glyph (wired serial vs Bluetooth), and the
|
||||
hardware vendor — tags the ones already confirmed as MeshCore companions, marks the remembered
|
||||
"last known good" one, and preselects it as the default.
|
||||
|
||||
Confirmed companions are sorted to the top, most-recently-used first, and shown by the mesh
|
||||
node name we learned when we last talked to them (in white, so they stand out from ports we've
|
||||
merely detected). Everything else follows in discovery order.
|
||||
|
||||
Selecting a device runs an immediate smoke test (via the ``verify`` callback): a genuine
|
||||
MeshCore companion is remembered — forever — as confirmed and becomes the session's active
|
||||
device; anything else sends the user back to the list to choose another.
|
||||
@@ -36,6 +41,40 @@ Verify = Callable[[DiscoveredDevice], Awaitable[Optional[dict]]]
|
||||
#: device, which the caller treats as "exit the program".
|
||||
_QUIT = object()
|
||||
|
||||
#: TYPE-column glyphs marking how a device connects. Kept as module constants so the splash's
|
||||
#: look can be retuned without touching the row-building logic. ``ᛒ`` is the *Bjarkan* rune the
|
||||
#: Bluetooth logo is drawn from — rendered white on the Bluetooth blue (see the ``bluetooth``
|
||||
#: theme style), flanked by the half-blocks below so it reads as a slim rounded badge — and
|
||||
#: ``🔌`` is a plain plug for a wired serial link.
|
||||
_BLE_ICON = "ᛒ"
|
||||
_SERIAL_ICON = "🔌"
|
||||
|
||||
#: Half-block glyphs that taper the Bluetooth badge: ``▐`` fills a cell's right half (so it
|
||||
#: hugs the rune's left edge) and ``▌`` its left half (hugging the right edge). Drawn in the
|
||||
#: badge's blue over the terminal background, they widen the blue by half a cell on each side.
|
||||
_BADGE_LEFT = "▐"
|
||||
_BADGE_RIGHT = "▌"
|
||||
|
||||
|
||||
def _type_cell(device: DiscoveredDevice) -> Text:
|
||||
"""The TYPE-column badge for ``device`` as a styled fragment.
|
||||
|
||||
Serial is a bare plug emoji (two cells, its own colour). Bluetooth is the rune on its blue
|
||||
badge, flanked by half-block slivers in the same blue so the fill reads as a slightly
|
||||
rounded chip a touch wider than the lone rune rather than a single hard-edged cell.
|
||||
|
||||
The plug carries a leading space so its two cells sit centred under the "TYPE" heading,
|
||||
lining up with the Bluetooth rune (which the badge's left half-block already nudges in a
|
||||
cell) rather than hugging the column's left edge.
|
||||
"""
|
||||
if not device.is_ble:
|
||||
return Text(" " + _SERIAL_ICON)
|
||||
cell = Text()
|
||||
cell.append(_BADGE_LEFT, style="bluetooth.edge")
|
||||
cell.append(_BLE_ICON, style="bluetooth")
|
||||
cell.append(_BADGE_RIGHT, style="bluetooth.edge")
|
||||
return cell
|
||||
|
||||
|
||||
def _copyright() -> str:
|
||||
"""The splash's muted copyright line, dated to the current year."""
|
||||
@@ -58,6 +97,22 @@ def _hardware_name(device: DiscoveredDevice) -> str:
|
||||
return name
|
||||
|
||||
|
||||
def _display_name(
|
||||
device: DiscoveredDevice, registry: dict[str, "RememberedDevice"]
|
||||
) -> str:
|
||||
"""The name to show for ``device``: its remembered mesh node name, else the hardware name.
|
||||
|
||||
Any device we've confirmed before — not just the single most-recent one — is shown by the
|
||||
mesh node name we learned at connect time, so a known ``COM11`` reads as "BaseStation"
|
||||
rather than the OS's generic "USB Serial Device". Devices with no record (or an empty
|
||||
remembered name) fall back to their hardware/product name.
|
||||
"""
|
||||
record = registry.get(device.stable_id)
|
||||
if record is not None and record.node_name:
|
||||
return record.node_name
|
||||
return _hardware_name(device)
|
||||
|
||||
|
||||
def _where(device: DiscoveredDevice) -> str:
|
||||
"""The connection target shown in the middle column: serial port or BLE address."""
|
||||
return device.target
|
||||
@@ -108,7 +163,9 @@ async def prompt_device(
|
||||
return None
|
||||
|
||||
remembered = store.load()
|
||||
known: set[str] = set(store.load_all())
|
||||
# The full registry (not just the single last device) so *every* confirmed companion can
|
||||
# be named, highlighted, and sorted to the top — keyed by stable_id.
|
||||
registry = store.load_all()
|
||||
# Preselect the remembered "last known good" device when it is currently attached.
|
||||
default = next((d for d in devices if remembered and remembered.matches(d)), None)
|
||||
|
||||
@@ -119,7 +176,7 @@ async def prompt_device(
|
||||
while True:
|
||||
chosen = await ui.select_startup(
|
||||
"Select a companion device",
|
||||
_build_items(devices, remembered, known),
|
||||
_build_items(devices, remembered, registry),
|
||||
default=default,
|
||||
banner=load_logo(),
|
||||
footnote=footnote,
|
||||
@@ -132,9 +189,7 @@ async def prompt_device(
|
||||
# this point (the smoke-test spinner, any failure notice, and the re-opened picker).
|
||||
footnote = None
|
||||
|
||||
name = remembered.node_name if (
|
||||
remembered and remembered.matches(chosen) and remembered.node_name
|
||||
) else _hardware_name(chosen)
|
||||
name = _display_name(chosen, registry)
|
||||
# "over Bluetooth" reads better than an address; a serial device names its port.
|
||||
where = "over Bluetooth" if chosen.is_ble else f"on {chosen.port}"
|
||||
# Smoke-test the choice in place: the splash keeps its wordmark and box, only the box
|
||||
@@ -166,30 +221,45 @@ async def prompt_device(
|
||||
)
|
||||
continue
|
||||
|
||||
# Confirmed: remember it forever, and reflect that on any re-entry of the loop.
|
||||
# Confirmed: remember it forever. We return straight away, so there's no need to
|
||||
# fold it back into the local registry for a re-render.
|
||||
store.remember(chosen, node_name=_node_name_from(info))
|
||||
known.add(chosen.stable_id)
|
||||
return chosen
|
||||
|
||||
|
||||
def _order(
|
||||
devices: list[DiscoveredDevice], registry: dict[str, RememberedDevice]
|
||||
) -> list[DiscoveredDevice]:
|
||||
"""Confirmed companions first (most-recently-used first), then everything else as found.
|
||||
|
||||
The devices we've actually spoken to are the ones the user almost always wants, so they
|
||||
rise to the top ordered by their last-connected timestamp (newest first). Unknown ports
|
||||
keep their incoming discovery order — a stable sort preserves it since they all tie.
|
||||
"""
|
||||
known = [d for d in devices if d.stable_id in registry]
|
||||
others = [d for d in devices if d.stable_id not in registry]
|
||||
known.sort(key=lambda d: registry[d.stable_id].last_connected, reverse=True)
|
||||
return known + others
|
||||
|
||||
|
||||
def _build_items(
|
||||
devices: list[DiscoveredDevice],
|
||||
remembered: Optional[RememberedDevice],
|
||||
known: set[str],
|
||||
registry: dict[str, RememberedDevice],
|
||||
) -> list:
|
||||
"""Build the aligned splash rows (a muted header + one :class:`Choice` per device).
|
||||
|
||||
The row for each device leads with its display name (the remembered node's name when
|
||||
known, else the hardware name); columns are padded to a shared width so they align.
|
||||
Devices already confirmed as MeshCore companions carry a bright tag; the remembered
|
||||
default is starred. A trailing Quit row (like the menu's) lets the user exit from here.
|
||||
known, else the hardware name), followed by its connection target, a TYPE glyph marking
|
||||
the transport, and the hardware vendor; columns are padded to a shared width so they
|
||||
align. Confirmed companions sort to the top (most-recent first), wear their name in white
|
||||
and a bright tag; the remembered default is starred. A trailing Quit row (like the menu's)
|
||||
lets the user exit from here.
|
||||
"""
|
||||
def _name(device: DiscoveredDevice) -> str:
|
||||
if remembered is not None and remembered.matches(device) and remembered.node_name:
|
||||
return remembered.node_name
|
||||
return _hardware_name(device)
|
||||
devices = _order(devices, registry)
|
||||
known: set[str] = set(registry)
|
||||
|
||||
name_w = max(cell_len(_name(d)) for d in devices)
|
||||
name_w = max(cell_len(_display_name(d, registry)) for d in devices)
|
||||
name_w = max(name_w, len("DEVICE"))
|
||||
# The middle column holds a serial port or a BLE address; label it for whichever kinds
|
||||
# are present so a Bluetooth address never sits under a bare "PORT" heading.
|
||||
@@ -198,6 +268,9 @@ def _build_items(
|
||||
port_label = "ADDRESS" if all(d.is_ble for d in devices) else "PORT / ADDRESS"
|
||||
port_w = max(cell_len(_where(d)) for d in devices)
|
||||
port_w = max(port_w, len(port_label))
|
||||
# The TYPE column holds a small transport badge (at most 3 cells); its heading is wider,
|
||||
# so the four-cell "TYPE" label sets the column width and every badge pads out to it.
|
||||
type_w = len("TYPE")
|
||||
vendor_w = max(cell_len(d.vendor_label) for d in devices)
|
||||
vendor_w = max(vendor_w, len("VENDOR"))
|
||||
|
||||
@@ -207,24 +280,36 @@ def _build_items(
|
||||
" "
|
||||
+ _pad("DEVICE", name_w)
|
||||
+ " " + _pad(port_label, port_w)
|
||||
+ " " + _pad("TYPE", type_w)
|
||||
+ " " + "VENDOR"
|
||||
)
|
||||
|
||||
items: list = [header]
|
||||
for device in devices:
|
||||
is_remembered = remembered is not None and remembered.matches(device)
|
||||
is_known = device.stable_id in known
|
||||
row = Text()
|
||||
row.append("★" if is_remembered else " ", style="warn" if is_remembered else "")
|
||||
row.append(" ")
|
||||
row.append(_pad(_name(device), name_w))
|
||||
# A confirmed companion wears its name in white so it stands out from mere detections.
|
||||
row.append(_pad(_display_name(device, registry), name_w),
|
||||
style="device.known" if is_known else "")
|
||||
row.append(" ")
|
||||
row.append(_pad(_where(device), port_w), style="muted")
|
||||
row.append(" ")
|
||||
# The TYPE badge marks the transport: a plug emoji for serial, or the Bluetooth rune
|
||||
# on its blue badge for BLE. It's built with its own colours, then the column is
|
||||
# padded with plain spaces — so the blue fill hugs just the badge, and the differing
|
||||
# badge widths (emoji 2 cells, rune-plus-edges 3) still line up under "TYPE".
|
||||
cell = _type_cell(device)
|
||||
row.append_text(cell)
|
||||
row.append(" " * max(0, type_w - cell.cell_len))
|
||||
row.append(" ")
|
||||
row.append(_pad(device.vendor_label, vendor_w), style="muted")
|
||||
# Only devices we've actually confirmed are billed as MeshCore companions; a USB
|
||||
# vendor ID (or a BLE advert) is a sort hint, not a claim. A bare serial bridge earns
|
||||
# an honest label; a MeshCore-named BLE advert is flagged as a likely companion.
|
||||
if device.stable_id in known:
|
||||
if is_known:
|
||||
row.append(" ")
|
||||
row.append("· MeshCore device", style="ok")
|
||||
elif device.is_ble:
|
||||
|
||||
@@ -20,6 +20,16 @@ MESH_THEME = Theme(
|
||||
# Our own messages in a chat: pure white, deliberately outside the per-sender hue
|
||||
# palette (see meshterm.ui.chat._SENDER_COLORS) so "you" is always easy to spot.
|
||||
"you": "bold #ffffff",
|
||||
# A confirmed companion's name on the startup device picker: pure white so the
|
||||
# devices we've actually talked to before jump out above the merely-detected ports.
|
||||
"device.known": "bold #ffffff",
|
||||
# The picker's Bluetooth TYPE badge: a white rune on the official Bluetooth blue
|
||||
# (Pantone 300, #0057b8), echoing the real logo so BLE reads at a glance.
|
||||
"bluetooth": "bold #ffffff on #0057b8",
|
||||
# The badge's tapered edges: half-block glyphs drawn in the same blue as the
|
||||
# foreground (over the terminal's own background), so only their inner half fills and
|
||||
# the badge reads a touch wider than the single rune cell without a hard rectangle.
|
||||
"bluetooth.edge": "#0057b8",
|
||||
"ok": "bold #4ade80",
|
||||
"warn": "bold #fbbf24",
|
||||
"err": "bold #f87171",
|
||||
|
||||
+10
-1
@@ -136,10 +136,19 @@ def test_ble_device_identity_and_labels() -> None:
|
||||
assert dev.target == "AA:BB:CC:DD:EE:FF" # the connection identifier is the address
|
||||
assert dev.stable_id == "ble:aa:bb:cc:dd:ee:ff" # stable across sessions, case-folded
|
||||
assert dev.confidence == "board" and dev.is_likely_lora # a MeshCore advert is confident
|
||||
assert dev.vendor_label == "Bluetooth"
|
||||
# The VENDOR column is *just* the hardware maker: a BLE advert rarely carries one, so it
|
||||
# stays blank rather than mislabelling the transport ("Bluetooth") as a vendor. The
|
||||
# transport is shown in its own TYPE column on the picker instead.
|
||||
assert dev.vendor_label == ""
|
||||
assert dev.label == "MeshCore-Base (BLE)"
|
||||
|
||||
|
||||
def test_ble_vendor_label_uses_manufacturer_when_present() -> None:
|
||||
"""A BLE advert that does expose a manufacturer string reports it as the vendor."""
|
||||
dev = DiscoveredDevice(transport="ble", address="AA:BB", name="MeshCore", manufacturer="Heltec")
|
||||
assert dev.vendor_label == "Heltec"
|
||||
|
||||
|
||||
def test_ble_and_serial_stable_ids_never_collide() -> None:
|
||||
"""A BLE address and a serial number/port can't map to the same remembered device."""
|
||||
assert _ble().stable_id != DiscoveredDevice("COM5", serial_number="SN1").stable_id
|
||||
|
||||
@@ -535,6 +535,53 @@ def test_device_picker_builds_aligned_columns(tmp_path) -> None:
|
||||
assert device_rows[0].index("COM5") == device_rows[1].index("/dev/ttyUSB0")
|
||||
|
||||
|
||||
def test_device_picker_names_and_sorts_known_devices(tmp_path) -> None:
|
||||
"""A confirmed device shows its node name (in white), sorts to the top, and marks type."""
|
||||
from meshterm.core.device_store import DeviceStore
|
||||
from meshterm.core.discovery import DiscoveredDevice
|
||||
from meshterm.ui.device_picker import _BLE_ICON, _SERIAL_ICON, prompt_device
|
||||
|
||||
# A previously-confirmed serial node, an unknown serial port, and a BLE companion.
|
||||
known = DiscoveredDevice(
|
||||
port="COM11", serial_number="SN1", description="USB Serial Device (COM11)"
|
||||
)
|
||||
unknown = DiscoveredDevice(port="COM3", product="Some Adapter", vid=0x1234)
|
||||
ble = DiscoveredDevice(
|
||||
transport="ble", address="AA:BB:CC:DD:EE:FF", name="MeshCore-Roam", product="MeshCore-Roam"
|
||||
)
|
||||
devices = [unknown, known, ble] # discovery order: known is *not* first
|
||||
|
||||
store = DeviceStore(tmp_path / "devices.json")
|
||||
store.remember(known, node_name="BaseStation")
|
||||
|
||||
captured: dict = {}
|
||||
|
||||
class _Ui:
|
||||
async def select_startup(self, title, items, *, default=None, banner=None, footnote=None):
|
||||
captured["items"] = items
|
||||
return None # skip past the smoke test
|
||||
|
||||
async def _never(_device):
|
||||
raise AssertionError("verify should not run when selection is skipped")
|
||||
|
||||
asyncio.run(prompt_device(_Ui(), devices, store, _never))
|
||||
rows = [it.title for it in captured["items"] if isinstance(it, Choice)]
|
||||
device_rows = rows[:-1] # drop the trailing Quit row
|
||||
|
||||
# The confirmed device sorts to the very top and is shown by its mesh node name, not the
|
||||
# OS's generic "USB Serial Device" description.
|
||||
top = device_rows[0]
|
||||
assert "BaseStation" in top.plain
|
||||
assert "USB Serial Device" not in top.plain
|
||||
# …and that name is painted white ("device.known") so it stands out.
|
||||
assert any(span.style == "device.known" for span in top.spans)
|
||||
|
||||
# The TYPE column marks the transport: a serial glyph for the wired node, the Bluetooth
|
||||
# rune for the companion advertised over BLE.
|
||||
assert _SERIAL_ICON in top.plain
|
||||
assert any(_BLE_ICON in row.plain for row in device_rows)
|
||||
|
||||
|
||||
class _PickerUi:
|
||||
"""A fake splash UI that always selects the first device, then dismisses messages."""
|
||||
|
||||
|
||||
Reference in new issue
Block a user