fix: good

Signed-off-by: yihong0618 <zouzou0208@gmail.com>
This commit is contained in:
yihong0618
2025-07-15 19:36:39 +08:00
parent 1c7aba4333
commit fee37fc96f
4 changed files with 39 additions and 7 deletions

View File

@ -8,6 +8,8 @@ import shlex
import telegramify_markdown import telegramify_markdown
from telebot import TeleBot from telebot import TeleBot
from telebot.types import Message from telebot.types import Message
from wcwidth import wcswidth
from wcwidth import wcswidth
from config import settings from config import settings
from handlers._utils import non_llm_handler from handlers._utils import non_llm_handler
@ -19,6 +21,12 @@ logger = logging.getLogger("bot")
store = MessageStore("data/messages.db") 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 @non_llm_handler
def handle_message(message: Message): def handle_message(message: Message):
logger.debug( logger.debug(
@ -85,16 +93,25 @@ def stats_command(message: Message, bot: TeleBot):
bot.reply_to(message, "没有找到任何统计信息。") bot.reply_to(message, "没有找到任何统计信息。")
return return
# 格式化消息数量和日期对齐 # 计算消息数量的最大宽度
max_count_width = max(len(f"{entry.message_count} messages") for entry in stats)
stats_text = "\n".join( 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_stats = store.get_user_stats(message.chat.id)
# 格式化消息数量和用户名对齐 if user_stats:
user_text = "\n".join( # 计算用户消息数量的最大宽度
f"{entry.message_count:>4} messages - {entry.user_name}" for entry in 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( bot.reply_to(
message, message,

15
pdm.lock generated
View File

@ -5,7 +5,7 @@
groups = ["default"] groups = ["default"]
strategy = ["inherit_metadata"] strategy = ["inherit_metadata"]
lock_version = "4.5.0" lock_version = "4.5.0"
content_hash = "sha256:a5037de5a2e7cf7ddeebb972d0ba1b88228137cfde32d1e82d05c7e52e1f5843" content_hash = "sha256:f3f7644ef037372cb6c84589f90f91578f2b069afffea47795496ca997de5ce1"
[[metadata.targets]] [[metadata.targets]]
requires_python = ">=3.10" requires_python = ">=3.10"
@ -2569,6 +2569,19 @@ files = [
{file = "urllib3-2.2.1.tar.gz", hash = "sha256:d0570876c61ab9e520d776c38acbbb5b05a776d3f9ff98a5c8fd5162a444cf19"}, {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]] [[package]]
name = "webencodings" name = "webencodings"
version = "0.5.1" version = "0.5.1"

View File

@ -26,6 +26,7 @@ dependencies = [
"pydantic>=2.11.7", "pydantic>=2.11.7",
"telethon>=1.40.0", "telethon>=1.40.0",
"pysocks>=1.7.1", "pysocks>=1.7.1",
"wcwidth>=0.2.13",
] ]
requires-python = ">=3.10" requires-python = ">=3.10"

View File

@ -127,5 +127,6 @@ uritemplate==4.1.1
uritools==4.0.2 uritools==4.0.2
urlextract==1.9.0 urlextract==1.9.0
urllib3==2.2.1 urllib3==2.2.1
wcwidth==0.2.13
webencodings==0.5.1 webencodings==0.5.1
yarl==1.9.4 yarl==1.9.4