diff --git a/handlers/summary/__init__.py b/handlers/summary/__init__.py index 6fa2c4b..b15ea62 100644 --- a/handlers/summary/__init__.py +++ b/handlers/summary/__init__.py @@ -8,6 +8,8 @@ import shlex import telegramify_markdown from telebot import TeleBot from telebot.types import Message +from wcwidth import wcswidth +from wcwidth import wcswidth from config import settings from handlers._utils import non_llm_handler @@ -19,6 +21,12 @@ logger = logging.getLogger("bot") store = MessageStore("data/messages.db") +def get_display_width(text: str) -> int: + """获取字符串的显示宽度,考虑中文字符""" + width = wcswidth(text) + return width if width is not None else len(text) + + @non_llm_handler def handle_message(message: Message): logger.debug( @@ -85,16 +93,25 @@ def stats_command(message: Message, bot: TeleBot): bot.reply_to(message, "没有找到任何统计信息。") return - # 格式化消息数量和日期对齐 + # 计算消息数量的最大宽度 + max_count_width = max(len(f"{entry.message_count} messages") for entry in stats) stats_text = "\n".join( - f"{entry.message_count:>4} messages - {entry.date}" for entry in stats + f"{f'{entry.message_count} messages':<{max_count_width}} - {entry.date}" + for entry in stats ) user_stats = store.get_user_stats(message.chat.id) - # 格式化消息数量和用户名对齐 - user_text = "\n".join( - f"{entry.message_count:>4} messages - {entry.user_name}" for entry in user_stats - ) + if user_stats: + # 计算用户消息数量的最大宽度 + max_user_count_width = max( + len(f"{entry.message_count} messages") for entry in user_stats + ) + user_text = "\n".join( + f"{f'{entry.message_count} messages':<{max_user_count_width}} - {entry.user_name}" + for entry in user_stats + ) + else: + user_text = "" bot.reply_to( message, diff --git a/pdm.lock b/pdm.lock index d56663f..9a5fd07 100644 --- a/pdm.lock +++ b/pdm.lock @@ -5,7 +5,7 @@ groups = ["default"] strategy = ["inherit_metadata"] lock_version = "4.5.0" -content_hash = "sha256:a5037de5a2e7cf7ddeebb972d0ba1b88228137cfde32d1e82d05c7e52e1f5843" +content_hash = "sha256:f3f7644ef037372cb6c84589f90f91578f2b069afffea47795496ca997de5ce1" [[metadata.targets]] requires_python = ">=3.10" @@ -2569,6 +2569,19 @@ files = [ {file = "urllib3-2.2.1.tar.gz", hash = "sha256:d0570876c61ab9e520d776c38acbbb5b05a776d3f9ff98a5c8fd5162a444cf19"}, ] +[[package]] +name = "wcwidth" +version = "0.2.13" +summary = "Measures the displayed width of unicode strings in a terminal" +groups = ["default"] +dependencies = [ + "backports-functools-lru-cache>=1.2.1; python_version < \"3.2\"", +] +files = [ + {file = "wcwidth-0.2.13-py2.py3-none-any.whl", hash = "sha256:3da69048e4540d84af32131829ff948f1e022c1c6bdb8d6102117aac784f6859"}, + {file = "wcwidth-0.2.13.tar.gz", hash = "sha256:72ea0c06399eb286d978fdedb6923a9eb47e1c486ce63e9b4e64fc18303972b5"}, +] + [[package]] name = "webencodings" version = "0.5.1" diff --git a/pyproject.toml b/pyproject.toml index 2dfa530..6a6a67f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ dependencies = [ "pydantic>=2.11.7", "telethon>=1.40.0", "pysocks>=1.7.1", + "wcwidth>=0.2.13", ] requires-python = ">=3.10" diff --git a/requirements.txt b/requirements.txt index f4018b2..985e329 100644 --- a/requirements.txt +++ b/requirements.txt @@ -127,5 +127,6 @@ uritemplate==4.1.1 uritools==4.0.2 urlextract==1.9.0 urllib3==2.2.1 +wcwidth==0.2.13 webencodings==0.5.1 yarl==1.9.4