diff options
author | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-11-15 15:12:01 -0500 |
---|---|---|
committer | Kevin Van Brunt <kmvanbrunt@gmail.com> | 2018-11-15 15:12:01 -0500 |
commit | bced087d58ec5a7e0ad5e8d7c3af6e6e84df56ef (patch) | |
tree | e2c4610ea6da0cca3d4c38eddd8c610a363c365f /cmd2/utils.py | |
parent | b6c39365e0331ab64aad074565fe73fb074a3a7f (diff) | |
download | cmd2-git-bced087d58ec5a7e0ad5e8d7c3af6e6e84df56ef.tar.gz |
Added wrapper for wcswidth that removes ansi escape characters
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 |