Split out unicode_ljust and unicode_rjust to break import cycle (#3388)

* Split out unicode_ljust and unicode_rjust to break import cycle

Previously, there was an import cycle between tui.menu_item and
lib.output.

* Move unicode.py from lib/ to lib/utils/
This commit is contained in:
correctmost 2025-04-13 05:47:33 +00:00 committed by GitHub
parent e281c2fa6a
commit 130d1a6ff8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 41 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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