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

Improve the Chinese, Japanese and Korean text menu layout #1945

Merged
merged 1 commit into from
Jul 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 18 additions & 1 deletion archinstall/lib/menu/abstract_menu.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import unicodedata

from typing import Callable, Any, List, Iterator, Tuple, Optional, Dict, TYPE_CHECKING

Expand All @@ -9,6 +10,22 @@
if TYPE_CHECKING:
_: Any

def count_cjk_chars(string):
"Count the total number of CJK characters contained in a string"
return sum(unicodedata.east_asian_width(c) in 'FW' for c in string)

def cjkljust(string, width, fillbyte=' '):
"""Support left alignment of Chinese, Japanese, Korean text
>>> cjkljust('Hello', 15, '*')
'Hello**********'
>>> cjkljust('你好', 15, '*')
'你好***********'
>>> cjkljust('안녕하세요', 15, '*')
'안녕하세요*****'
>>> cjkljust('こんにちは', 15, '*')
'こんにちは*****'
"""
return string.ljust(width - count_cjk_chars(string), fillbyte)

class Selector:
def __init__(
Expand Down Expand Up @@ -128,7 +145,7 @@ def menu_text(self, padding: int = 0) -> str:

if current:
padding += 5
description = str(self._description).ljust(padding, ' ')
description = cjkljust(str(self._description), padding, ' ')
current = current
else:
description = self._description
Expand Down