various plugin refactors and fixes
This commit is contained in:
@@ -5,6 +5,7 @@ import html
|
||||
import ipaddress
|
||||
import socket
|
||||
import logging
|
||||
from wcwidth import wcswidth
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@@ -80,3 +81,58 @@ async def send_html_message(bot, room_id, html_body, markdown_fallback):
|
||||
message_type="m.room.message",
|
||||
content=content
|
||||
)
|
||||
|
||||
|
||||
def code_block(title: str, sections: list) -> str:
|
||||
"""
|
||||
Build a Markdown code block with perfectly aligned columns (emoji‑aware).
|
||||
|
||||
Args:
|
||||
title: header line inside the code block
|
||||
sections: list of dicts with keys 'title' (str) and 'rows'
|
||||
rows is a list of (emoji, label, value) tuples
|
||||
|
||||
Returns:
|
||||
Markdown string with triple backticks and aligned content.
|
||||
"""
|
||||
labelled = []
|
||||
for sec in sections:
|
||||
for emoji, text, value in sec["rows"]:
|
||||
if text.strip() or emoji.strip():
|
||||
labelled.append((emoji, text, value))
|
||||
|
||||
max_label_width = max((len(str(t)) for _, t, _ in labelled), default=0)
|
||||
|
||||
emoji_widths = {}
|
||||
for emoji, _, _ in labelled:
|
||||
if emoji:
|
||||
w = wcswidth(emoji) or 1
|
||||
emoji_widths[emoji] = w
|
||||
else:
|
||||
emoji_widths[emoji] = 0
|
||||
max_emoji_width = max(emoji_widths.values()) if emoji_widths else 0
|
||||
|
||||
prefix_width = max_emoji_width + 1 + max_label_width + 3 # "E label : "
|
||||
separator = "=" * (prefix_width + 30)
|
||||
lines = [title, separator]
|
||||
|
||||
for sec in sections:
|
||||
# Only print a section header if the title is not empty
|
||||
if sec["title"].strip():
|
||||
lines.append("")
|
||||
lines.append(f"── {sec['title']} ──")
|
||||
for emoji, text, value in sec["rows"]:
|
||||
if text.strip() or emoji.strip():
|
||||
if emoji:
|
||||
actual_w = emoji_widths.get(emoji, 0)
|
||||
pad = max_emoji_width - actual_w
|
||||
emoji_field = emoji + " " * pad
|
||||
else:
|
||||
emoji_field = " " * max_emoji_width
|
||||
padded_label = f"{text:<{max_label_width}}"
|
||||
lines.append(f"{emoji_field} {padded_label} : {value}")
|
||||
else:
|
||||
lines.append(f"{' ' * prefix_width}{value}")
|
||||
lines.append("")
|
||||
lines.append(separator)
|
||||
return "```\n" + "\n".join(lines) + "\n```"
|
||||
|
||||
Reference in New Issue
Block a user