From bced087d58ec5a7e0ad5e8d7c3af6e6e84df56ef Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 15 Nov 2018 15:12:01 -0500 Subject: Added wrapper for wcswidth that removes ansi escape characters --- cmd2/utils.py | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index d4a3db2f..909332ae 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -8,6 +8,8 @@ import re import unicodedata from typing import Any, Iterable, List, Optional, Union +from wcwidth import wcswidth + from . import constants @@ -20,6 +22,15 @@ def strip_ansi(text: str) -> str: return constants.ANSI_ESCAPE_RE.sub('', text) +def display_width(text: str) -> int: + """ + Return the printable length of a string. This can be different than character count in unicode strings. + :param text: the string being measured + """ + # Strip ANSI escape codes since they cause wcswidth to return -1 + return wcswidth(strip_ansi(text)) + + def is_quoted(arg: str) -> bool: """ Checks if a string is quoted -- cgit v1.2.1