Files
brov-macbook/ops/brovops/fmt.py
T
maksarsanjeev e47b97a462 ops: dates shown as DD.MM.YYYY (bot, digest, reminders, BroV)
Storage and the API stay ISO; ru_day() formats every user-facing date, Claude is asked
to write dates in its replies the same way.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 03:21:24 +03:00

73 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""Plain-text rendering for Telegram (HTML parse mode)."""
from datetime import date
from html import escape
from .core import ru_day
ICON = {"task": "📌", "sub": "💳", "routine": "🔁"}
def human_day(iso: str, today: date) -> str:
d = date.fromisoformat(iso)
delta = (d - today).days
if delta == 0:
return "сегодня"
if delta == 1:
return "завтра"
if delta == -1:
return "вчера"
if delta < 0:
return f"просрочено на {-delta} дн."
return d.strftime("%d.%m.%Y")
def item_line(it: dict, today: date) -> str:
extra = f" — {it['amount']:g} {it['currency']}" if it["kind"] == "sub" else ""
client = f" [{it['client']}]" if it.get("client") else ""
return f"{ICON[it['kind']]} {escape(it['title'])}{client}{extra} · {human_day(it['date'], today)}"
def agenda(a: dict) -> str:
today = date.fromisoformat(a["today"])
out = []
if a["running"]:
out.append(f"⏱ Сейчас: <b>{escape(a['running']['title'])}</b> ({a['running']['minutes']} мин)")
for key, title in (("overdue", "🔥 Просрочено"), ("due_today", "📅 Сегодня"), ("soon", "🗓 Ближайшая неделя")):
if a[key]:
out.append(f"\n<b>{title}</b>")
out += [item_line(it, today) for it in a[key]]
if not (a["overdue"] or a["due_today"] or a["soon"]):
out.append("На ближайшую неделю ничего не горит 👌")
if a["monthly_cost"]:
out.append("\nПодписки в месяц: " + ", ".join(f"{v:g} {k}" for k, v in a["monthly_cost"].items()))
return "\n".join(out).strip()
def tree(nodes: list, depth: int = 0) -> list[str]:
out = []
for t in nodes:
mark = "⏱" if t["running"] else ("▶️" if t["status"] == "doing" else "▫️")
due = f" · до {ru_day(t['due'])}" if t["due"] else ""
mins = f" · {t['minutes']} мин" if t["minutes"] else ""
client = f" [{t['client']}]" if t["client"] else ""
out.append(f"{' ' * depth}{mark} #{t['id']} {escape(t['title'])}{client}{due}{mins}")
out += tree(t["children"], depth + 1)
return out
def subs(rows: list, monthly: dict, today: date) -> str:
if not rows:
return "Подписок пока нет. Скажи, например: «хостинг 900 рублей в месяц, списание 15-го»."
lines = [f"💳 {escape(s['name'])} — {s['amount']:g} {s['currency']} / "
f"{s['every'] if s['every'] > 1 else ''}{ {'day':'день','week':'нед.','month':'мес.','year':'год'}[s['period']] }"
f" · {human_day(s['next_date'], today)}" for s in rows]
lines.append("\nВ месяц: " + ", ".join(f"{v:g} {k}" for k, v in monthly.items()))
return "\n".join(lines)
def routines(rows: list, today: date) -> str:
if not rows:
return "Регламентных дел пока нет. Скажи: «бэкап NAS раз в неделю»."
return "\n".join(f"🔁 #{r['id']} {escape(r['title'])} · раз в {r['every_days']} дн. · {human_day(r['next_date'], today)}"
+ (f" · последний раз {ru_day(r['last_done'])}" if r["last_done"] else "") for r in rows)