Skip to content

Commit fc0ce00

Browse files
authored
Merge pull request #598 from python-cmd2/rename_display_width
Renamed display_width to ansi_safe_wcswidth
2 parents 61da7e5 + a827431 commit fc0ce00

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

cmd2/cmd2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,7 +1301,7 @@ def _display_matches_gnu_readline(self, substitution: str, matches: List[str],
13011301
longest_match_length = 0
13021302

13031303
for cur_match in matches_to_display:
1304-
cur_length = utils.display_width(cur_match)
1304+
cur_length = utils.ansi_safe_wcswidth(cur_match)
13051305
if cur_length > longest_match_length:
13061306
longest_match_length = cur_length
13071307
else:
@@ -2661,7 +2661,7 @@ def _print_topics(self, header: str, cmds: List[str], verbose: bool) -> None:
26612661
widest = 0
26622662
# measure the commands
26632663
for command in cmds:
2664-
width = utils.display_width(command)
2664+
width = utils.ansi_safe_wcswidth(command)
26652665
if width > widest:
26662666
widest = width
26672667
# add a 4-space pad
@@ -3478,13 +3478,13 @@ def async_alert(self, alert_msg: str, new_prompt: Optional[str] = None) -> None:
34783478

34793479
if update_terminal:
34803480
# Get the display width of the prompt
3481-
prompt_width = utils.display_width(current_prompt)
3481+
prompt_width = utils.ansi_safe_wcswidth(current_prompt)
34823482

34833483
# Get the size of the terminal
34843484
terminal_size = shutil.get_terminal_size()
34853485

34863486
# Figure out how many lines the prompt and user input take up
3487-
total_str_size = prompt_width + utils.display_width(readline.get_line_buffer())
3487+
total_str_size = prompt_width + utils.ansi_safe_wcswidth(readline.get_line_buffer())
34883488
num_input_lines = int(total_str_size / terminal_size.columns) + 1
34893489

34903490
# Get the cursor's offset from the beginning of the first input line

cmd2/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@ def strip_ansi(text: str) -> str:
2222
return constants.ANSI_ESCAPE_RE.sub('', text)
2323

2424

25-
def display_width(text: str) -> int:
25+
def ansi_safe_wcswidth(text: str) -> int:
2626
"""
27-
Return the printable length of a string. This can be different than character count in unicode strings.
27+
Wraps wcswidth to make it compatible with colored strings
28+
2829
:param text: the string being measured
2930
"""
3031
# Strip ANSI escape codes since they cause wcswidth to return -1

tests/test_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def test_strip_ansi():
2121
assert base_str != ansi_str
2222
assert base_str == cu.strip_ansi(ansi_str)
2323

24-
def test_display_width():
24+
def test_ansi_safe_wcswidth():
2525
base_str = HELLO_WORLD
2626
ansi_str = Fore.GREEN + base_str + Fore.RESET
27-
assert cu.display_width(ansi_str) != len(ansi_str)
27+
assert cu.ansi_safe_wcswidth(ansi_str) != len(ansi_str)
2828

2929
def test_strip_quotes_no_quotes():
3030
base_str = HELLO_WORLD

0 commit comments

Comments
 (0)