Have EvTable width correctly handle asian alphabets

This commit is contained in:
Griatch 2020-07-15 23:16:57 +02:00
parent a0ce1ea821
commit 101152abde
4 changed files with 74 additions and 15 deletions

View file

@ -9,7 +9,7 @@ be of use when designing your own game.
import os
import gc
import sys
import copy
import copy
import types
import math
import re
@ -20,6 +20,7 @@ import traceback
import importlib
import importlib.util
import importlib.machinery
from unicodedata import east_asian_width
from twisted.internet.task import deferLater
from twisted.internet.defer import returnValue # noqa - used as import target
from os.path import join as osjoin
@ -1819,7 +1820,7 @@ def m_len(target):
back to normal len for other objects.
Args:
target (string): A string with potential MXP components
target (str): A string with potential MXP components
to search.
Returns:
@ -1834,6 +1835,35 @@ def m_len(target):
return len(target)
def display_len(target):
"""
Calculate the 'visible width' of text. This is not necessarily the same as the
number of characters in the case of certain asian characters. This will also
strip MXP patterns.
Args:
target (string): A string with potential MXP components
to search.
Return:
int: The visible width of the target.
"""
# Would create circular import if in module root.
from evennia.utils.ansi import ANSI_PARSER
if inherits_from(target, str):
# str or ANSIString
target = ANSI_PARSER.strip_mxp(target)
target = ANSI_PARSER.parse_ansi(target, strip_ansi=True)
extra_wide = ("F", "W")
return sum(2 if east_asian_width(char) in extra_wide else 1 for char in target)
else:
return len(target)
# -------------------------------------------------------------------
# Search handler function
# -------------------------------------------------------------------