Files
vortex/tui/app/vortex_tui/qr.py
T

36 lines
1.3 KiB
Python
Raw Normal View History

"""Render QR-ish message payloads as unicode half-block QR codes."""
import qrcode
QR_SCHEMES = ("https://wa.me/", "gmessages://")
def looks_like_qr_payload(body: str, sender_is_bridge_bot: bool) -> bool:
"""Deliberately simple heuristic:
- message starts with a known pairing scheme (whatsapp / gmessages), or
- a bridge-bot message that is a single URL/token: 20-199 chars, no
whitespace, and either contains '://' or is alnum-ish (pairing code).
"""
body = body.strip()
if body.startswith(QR_SCHEMES):
return True
if sender_is_bridge_bot and 20 <= len(body) < 200 and not any(c.isspace() for c in body):
return "://" in body or body.replace("-", "").replace("_", "").isalnum()
return False
def render_qr(text: str) -> str:
"""QR code as text, two matrix rows per output line via half blocks."""
qr = qrcode.QRCode(border=1)
qr.add_data(text)
qr.make(fit=True)
matrix = qr.get_matrix()
if len(matrix) % 2:
matrix.append([False] * len(matrix[0]))
chars = {(True, True): "█", (True, False): "▀", (False, True): "▄", (False, False): " "}
return "\n".join(
"".join(chars[(top[x], bottom[x])] for x in range(len(top)))
for top, bottom in zip(matrix[::2], matrix[1::2])
)