From 2947af0fe52a226afa5b5fbe31e90a52f2c33f30 Mon Sep 17 00:00:00 2001 From: "Matt Johnson (via Claude)" Date: Tue, 9 Jun 2026 14:39:39 +0000 Subject: [PATCH] =?UTF-8?q?fix:=20swpc=20=5Frender()=20=E2=80=94=20truncat?= =?UTF-8?q?e=20line=202=20message=20at=20120-char=20word=20boundary?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds _trunc() helper that cuts at last space before 120 chars and appends ellipsis. Applied at extraction site and all four _render line2 branches. Line 3 (SWPC · timestamp) is a formatted fixed string — left unchanged. Co-Authored-By: Claude Opus 4.6 --- meshai/central/swpc_handler.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/meshai/central/swpc_handler.py b/meshai/central/swpc_handler.py index c899089..5cb94ac 100644 --- a/meshai/central/swpc_handler.py +++ b/meshai/central/swpc_handler.py @@ -54,6 +54,16 @@ _S_SCALE_THRESHOLDS = [ ] +def _trunc(s: str, limit: int = 120) -> str: + """Truncate *s* at the last word boundary at or before *limit* chars.""" + if len(s) <= limit: + return s + cut = s[:limit].rsplit(" ", 1)[0] + if not cut: + cut = s[:limit] + return cut + "…" + + def _now() -> int: return int(time.time()) @@ -300,7 +310,7 @@ def handle_swpc(envelope: dict, subject: str, # Extract optional detail and time tag for multi-line render. _detail = d.get("message") or d.get("description") or "" if isinstance(_detail, str): - _detail = _detail.strip()[:120] + _detail = _trunc(_detail.strip()) else: _detail = "" _time_tag = "" @@ -335,19 +345,19 @@ def _render(event_kind, scale_code, label, scalar_str, if event_kind == "geomag": line1 = f"🧲 {prefix} {scale_code} Geomagnetic Storm — {scalar_str}" - line2 = detail[:120] if detail else "HF degraded, aurora possible" + line2 = _trunc(detail) if detail else "HF degraded, aurora possible" line3 = f"SWPC · {time_tag}" if time_tag else "SWPC" elif event_kind == "flare": line1 = f"☀️ {prefix} {scalar_str} Solar Flare — {scale_code}" - line2 = detail[:120] if detail else "HF radio fading, GPS may glitch" + line2 = _trunc(detail) if detail else "HF radio fading, GPS may glitch" line3 = f"SWPC · {time_tag}" if time_tag else "SWPC" elif event_kind == "proton": line1 = f"☢️ {prefix} {scale_code} Radiation Storm — {scalar_str}" - line2 = detail[:120] if detail else "Polar HF radio affected" + line2 = _trunc(detail) if detail else "Polar HF radio affected" line3 = f"SWPC · {time_tag}" if time_tag else "SWPC" else: line1 = f"⚠️ {prefix} Space Weather Event — {scale_code or '?'}" - line2 = detail[:120] if detail else None + line2 = _trunc(detail) if detail else None line3 = f"SWPC · {time_tag}" if time_tag else "SWPC" return "\n".join(l for l in [line1, line2, line3] if l)