Skip to content

Commit

Permalink
Add functions for CJK string alignment
Browse files Browse the repository at this point in the history
  • Loading branch information
yetist committed Aug 15, 2023
1 parent 81ef09c commit db4450c
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions archinstall/lib/output.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
import sys
import unicodedata
from enum import Enum

from pathlib import Path
Expand Down Expand Up @@ -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_cjk_chars(string: str) -> int:
"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 cjk_ljust(string: str, width: int, fillbyte: str = ' ') -> str:
"""Return a left-justified CJK string of length width.
>>> cjkljust('Hello', 15, '*')
'Hello**********'
>>> cjkljust('你好', 15, '*')
'你好***********'
>>> cjkljust('안녕하세요', 15, '*')
'안녕하세요*****'
>>> cjkljust('こんにちは', 15, '*')
'こんにちは*****'
"""
return string.ljust(width - _count_cjk_chars(string), fillbyte)

def cjk_rjust(string: str, width: int, fillbyte: str = ' ') -> str:
"""Return a right-justified CJK string of length width.
>>> cjkrjust('Hello', 15, '*')
'**********Hello'
>>> cjkrjust('你好', 15, '*')
'***********你好'
>>> cjkrjust('안녕하세요', 15, '*')
'*****안녕하세요'
>>> cjkrjust('こんにちは', 15, '*')
'*****こんにちは'
"""
return string.rjust(width - _count_cjk_chars(string), fillbyte)

0 comments on commit db4450c

Please sign in to comment.