diff --git a/archinstall/lib/output.py b/archinstall/lib/output.py index 1c4b4950..adfe2e69 100644 --- a/archinstall/lib/output.py +++ b/archinstall/lib/output.py @@ -1,16 +1,15 @@ import logging import os import sys -import unicodedata from collections.abc import Callable from dataclasses import asdict, is_dataclass from datetime import UTC, datetime from enum import Enum -from functools import lru_cache from pathlib import Path from typing import TYPE_CHECKING, Any from .storage import storage +from .utils.unicode import unicode_ljust, unicode_rjust if TYPE_CHECKING: from _typeshed import DataclassInstance @@ -335,41 +334,3 @@ def log( if level != logging.DEBUG: from archinstall.tui.curses_menu import Tui Tui.print(text) - - -@lru_cache(maxsize=128) -def _is_wide_character(char: str) -> bool: - return unicodedata.east_asian_width(char) in 'FW' - - -def _count_wchars(string: str) -> int: - "Count the total number of wide characters contained in a string" - return sum(_is_wide_character(c) 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) diff --git a/archinstall/lib/utils/unicode.py b/archinstall/lib/utils/unicode.py new file mode 100644 index 00000000..402c14a5 --- /dev/null +++ b/archinstall/lib/utils/unicode.py @@ -0,0 +1,40 @@ +import unicodedata +from functools import lru_cache + + +@lru_cache(maxsize=128) +def _is_wide_character(char: str) -> bool: + return unicodedata.east_asian_width(char) in 'FW' + + +def _count_wchars(string: str) -> int: + "Count the total number of wide characters contained in a string" + return sum(_is_wide_character(c) 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) diff --git a/archinstall/tui/menu_item.py b/archinstall/tui/menu_item.py index 22c51310..c651c845 100644 --- a/archinstall/tui/menu_item.py +++ b/archinstall/tui/menu_item.py @@ -5,7 +5,7 @@ from dataclasses import dataclass, field from functools import cached_property from typing import TYPE_CHECKING, Any, ClassVar -from ..lib.output import unicode_ljust +from ..lib.utils.unicode import unicode_ljust if TYPE_CHECKING: from archinstall.lib.translationhandler import DeferredTranslation