From a95587b056eb042866c150b2f390f8dcebce3506 Mon Sep 17 00:00:00 2001 From: Henry Hsiao Date: Fri, 28 Jun 2024 23:41:50 +0800 Subject: [PATCH 1/2] Update utils.py Added use_display_len to m_len, default to False so it won't change behavior for those using it. Justify now properly handle wide character, by using the new use_display_len for m_len. --- evennia/utils/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index cc92baee47..4ec99ac204 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -290,8 +290,8 @@ def justify(text, width=None, align="l", indent=0, fillchar=" "): # absolute mode - just crop or fill to width abs_lines = [] for line in text.split("\n"): - nlen = m_len(line) - if m_len(line) < width: + nlen = m_len(line, True) + if m_len(line, True) < width: line += sp * (width - nlen) else: line = crop(line, width=width, suffix="") @@ -306,7 +306,7 @@ def justify(text, width=None, align="l", indent=0, fillchar=" "): for ip, paragraph in enumerate(paragraphs): if ip > 0: words.append(("\n", 0)) - words.extend((word, m_len(word)) for word in paragraph.split()) + words.extend((word, m_len(word, True)) for word in paragraph.split()) if not words: # Just whitespace! @@ -2299,7 +2299,7 @@ def calledby(callerdepth=1): return "\n".join(out[::-1]) -def m_len(target): +def m_len(target, use_display_len=False): """ Provides length checking for strings with MXP patterns, and falls back to normal len for other objects. @@ -2316,8 +2316,8 @@ def m_len(target): from evennia.utils.ansi import ANSI_PARSER if inherits_from(target, str) and "|lt" in target: - return len(ANSI_PARSER.strip_mxp(target)) - return len(target) + return display_len(ANSI_PARSER.strip_mxp(target)) if use_display_len else len(ANSI_PARSER.strip_mxp(target)) + return display_len(target) if use_display_len else len(target) def display_len(target): From 23b4ce7dca07cdedab036c6940d35bb4e7b46f19 Mon Sep 17 00:00:00 2001 From: Henry Hsiao Date: Thu, 11 Jul 2024 07:39:41 +0900 Subject: [PATCH 2/2] Update utils.py Added arguments description for use_display_len --- evennia/utils/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/evennia/utils/utils.py b/evennia/utils/utils.py index 4ec99ac204..a0344cbd21 100644 --- a/evennia/utils/utils.py +++ b/evennia/utils/utils.py @@ -2307,6 +2307,9 @@ def m_len(target, use_display_len=False): Args: target (str): A string with potential MXP components to search. + use_display_len (bool, optional): If `True`, use display_len + instead of len. Default is `False`, use `True` if target + contain east asian characters. Returns: length (int): The length of `target`, ignoring MXP components.