Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: unicode display width support #502

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 16 additions & 1 deletion sources/graphics_list_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)


Expand Down Expand Up @@ -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)