summaryrefslogtreecommitdiff
path: root/cmd2
diff options
context:
space:
mode:
authorKevin Van Brunt <kmvanbrunt@gmail.com>2018-11-15 15:12:01 -0500
committerKevin Van Brunt <kmvanbrunt@gmail.com>2018-11-15 15:12:01 -0500
commitbced087d58ec5a7e0ad5e8d7c3af6e6e84df56ef (patch)
treee2c4610ea6da0cca3d4c38eddd8c610a363c365f /cmd2
parentb6c39365e0331ab64aad074565fe73fb074a3a7f (diff)
downloadcmd2-git-bced087d58ec5a7e0ad5e8d7c3af6e6e84df56ef.tar.gz
Added wrapper for wcswidth that removes ansi escape characters
Diffstat (limited to 'cmd2')
-rw-r--r--cmd2/cmd2.py11
-rw-r--r--cmd2/utils.py11
2 files changed, 16 insertions, 6 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index 6f9350c6..9254612d 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -43,7 +43,6 @@ from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Type, Un
import colorama
from colorama import Fore
-from wcwidth import wcswidth
from . import constants
from . import plugin
@@ -1302,7 +1301,7 @@ class Cmd(cmd.Cmd):
longest_match_length = 0
for cur_match in matches_to_display:
- cur_length = wcswidth(cur_match)
+ cur_length = utils.display_width(cur_match)
if cur_length > longest_match_length:
longest_match_length = cur_length
else:
@@ -2662,7 +2661,7 @@ class Cmd(cmd.Cmd):
widest = 0
# measure the commands
for command in cmds:
- width = len(command)
+ width = utils.display_width(command)
if width > widest:
widest = width
# add a 4-space pad
@@ -3478,14 +3477,14 @@ a..b, a:b, a:, ..b items by indices (inclusive)
update_terminal = True
if update_terminal:
- # Remove ansi characters to get the visible width of the prompt
- prompt_width = wcswidth(utils.strip_ansi(current_prompt))
+ # Get the display width of the prompt
+ prompt_width = utils.display_width(current_prompt)
# Get the size of the terminal
terminal_size = shutil.get_terminal_size()
# Figure out how many lines the prompt and user input take up
- total_str_size = prompt_width + wcswidth(readline.get_line_buffer())
+ total_str_size = prompt_width + utils.display_width(readline.get_line_buffer())
num_input_lines = int(total_str_size / terminal_size.columns) + 1
# Get the cursor's offset from the beginning of the first input line
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