From 4a3b76ab2b97ebddbe31ffd651854ba79898f4b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BF=98=E5=BF=A7=E5=8C=97=E8=90=B1=E8=8D=89?= Date: Wed, 1 May 2024 06:27:07 +0000 Subject: [PATCH] feat: unicode display width support --- requirements.txt | 1 + sources/graphics_list_formatter.py | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f792c5ab..a62cb4ee 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,6 +5,7 @@ GitPython~=3.1 # Markdown visualization modules: pytz~=2022.7 humanize~=4.6 +wcwidth~=0.2 # Graphs drawing modules: matplotlib~=3.7 diff --git a/sources/graphics_list_formatter.py b/sources/graphics_list_formatter.py index d7c0a28b..9e1cc69e 100644 --- a/sources/graphics_list_formatter.py +++ b/sources/graphics_list_formatter.py @@ -3,6 +3,7 @@ from datetime import datetime from pytz import timezone, utc +from wcwidth import wcswidth, wcwidth from manager_environment import EnvironmentManager as EM from manager_file import FileManager as FM @@ -71,7 +72,7 @@ def make_list(data: List = None, names: List[str] = None, texts: List[str] = Non data = list(zip(names, texts, percents)) top_data = sorted(data[:top_num], key=lambda record: record[2], reverse=True) if sort else data[:top_num] - data_list = [f"{n[:25]}{' ' * (25 - len(n))}{t}{' ' * (20 - len(t))}{make_graph(p)} {p:05.2f} % " for n, t, p in top_data] + data_list = [f"{pad_string(n, 25)}{pad_string(t, 20)}{make_graph(p)} {p:05.2f} % " for n, t, p in top_data] return "\n".join(data_list) @@ -141,3 +142,17 @@ def make_language_per_repo_list(repositories: Dict) -> str: top_language = max(list(language_count.keys()), key=lambda x: language_count[x]["count"]) title = f"**{FM.t('I Mostly Code in') % top_language}** \n\n" if len(repos_with_language) > 0 else "" return f"{title}```text\n{make_list(names=names, texts=texts, percents=percents)}\n```\n\n" + +def pad_string(string: str, length: int) -> str: + """ + Pad string with spaces to the specified length, or truncate it if it's too long. + + :param string: The string to pad. + :param length: The length to pad the string to. + :returns: The padded or truncated string. + """ + width = wcswidth(string) + while width > length: + width -= wcwidth(string[-1]) + string = string[:-1] + return string + " " * (length - width)