diff options
Diffstat (limited to 'cmd2/utils.py')
-rw-r--r-- | cmd2/utils.py | 11 |
1 files changed, 11 insertions, 0 deletions
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 |