From f8171dc1fb0a80c3c85fed6eb1bfe8ae19b912d7 Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Tue, 15 Aug 2023 11:28:48 +0800 Subject: [PATCH 1/3] Add functions for unicode string alignment --- archinstall/lib/output.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 9f2a2ae31d..c3aa4d6b75 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -1,6 +1,7 @@ import logging import os import sys +import unicodedata from enum import Enum from pathlib import Path @@ -326,3 +327,33 @@ def log( if level != logging.DEBUG or storage.get('arguments', {}).get('verbose', False): sys.stdout.write(f"{text}\n") sys.stdout.flush() + +def _count_wchars(string: str) -> int: + "Count the total number of wide characters contained in a string" + return sum(unicodedata.east_asian_width(c) in 'FW' for c in string) + +def unicode_ljust(string: str, width: int, fillbyte: str = ' ') -> str: + """Return a left-justified unicode string of length width. + >>> unicode_ljust('Hello', 15, '*') + 'Hello**********' + >>> unicode_ljust('你好', 15, '*') + '你好***********' + >>> unicode_ljust('안녕하세요', 15, '*') + '안녕하세요*****' + >>> unicode_ljust('こんにちは', 15, '*') + 'こんにちは*****' + """ + return string.ljust(width - _count_wchars(string), fillbyte) + +def unicode_rjust(string: str, width: int, fillbyte: str = ' ') -> str: + """Return a right-justified unicode string of length width. + >>> unicode_rjust('Hello', 15, '*') + '**********Hello' + >>> unicode_rjust('你好', 15, '*') + '***********你好' + >>> unicode_rjust('안녕하세요', 15, '*') + '*****안녕하세요' + >>> unicode_rjust('こんにちは', 15, '*') + '*****こんにちは' + """ + return string.rjust(width - _count_wchars(string), fillbyte) From 135d78152651f873e224a4290864b806cb94dc39 Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Tue, 15 Aug 2023 11:31:42 +0800 Subject: [PATCH 2/3] use unicode alignment function to show menu --- archinstall/lib/menu/abstract_menu.py | 21 ++------------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/archinstall/lib/menu/abstract_menu.py b/archinstall/lib/menu/abstract_menu.py index f12d2b733d..306c500ac5 100644 --- a/archinstall/lib/menu/abstract_menu.py +++ b/archinstall/lib/menu/abstract_menu.py @@ -1,32 +1,15 @@ from __future__ import annotations -import unicodedata from typing import Callable, Any, List, Iterator, Tuple, Optional, Dict, TYPE_CHECKING from .menu import Menu, MenuSelectionType from ..output import error +from ..output import unicode_ljust from ..translationhandler import TranslationHandler, Language 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__( self, @@ -145,7 +128,7 @@ def menu_text(self, padding: int = 0) -> str: if current: padding += 5 - description = cjkljust(str(self._description), padding, ' ') + description = unicode_ljust(str(self._description), padding, ' ') current = current else: description = self._description From e614f2ec5cc1b4f3635b805181d8e869f12012cf Mon Sep 17 00:00:00 2001 From: Xiaotian Wu Date: Tue, 15 Aug 2023 11:50:58 +0800 Subject: [PATCH 3/3] Allow table content to support unicode text alignment --- archinstall/lib/output.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index c3aa4d6b75..63d9c1fb6e 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -84,7 +84,7 @@ def as_table( if capitalize: key = key.capitalize() - key_list.append(key.ljust(width)) + key_list.append(unicode_ljust(key, width)) output += ' | '.join(key_list) + '\n' output += '-' * len(output) + '\n' @@ -100,9 +100,9 @@ def as_table( value = '*' * width if isinstance(value, (int, float)) or (isinstance(value, str) and value.isnumeric()): - obj_data.append(str(value).rjust(width)) + obj_data.append(unicode_rjust(str(value), width)) else: - obj_data.append(str(value).ljust(width)) + obj_data.append(unicode_ljust(str(value), width)) output += ' | '.join(obj_data) + '\n'