Skip to content

Commit b0e5aab

Browse files
committed
Renamed ansi_safe_wcswidth() to style_aware_wcswidth()
Renamed ansi_aware_write() to style_aware_write()
1 parent 1a26c02 commit b0e5aab

File tree

9 files changed

+35
-32
lines changed

9 files changed

+35
-32
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
## 0.9.23 (TBD, 2019)
22
* Bug Fixes
33
* Fixed bug where startup script containing a single quote in its file name was incorrectly quoted
4+
* Breaking changes
5+
* Renamed the following `ansi` members for accuracy in what types of ANSI escape sequences are handled
6+
* `ansi.allow_ansi` -> `ansi.allow_style`
7+
* `ansi.ansi_safe_wcswidth()` -> `ansi.style_aware_wcswidth()`
8+
* `ansi.ansi_aware_write()` -> `ansi.style_aware_write()`
49

510
## 0.9.22 (December 9, 2019)
611
* Bug Fixes

cmd2/ansi.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,18 @@ def strip_style(text: str) -> str:
9090
return ANSI_STYLE_RE.sub('', text)
9191

9292

93-
def ansi_safe_wcswidth(text: str) -> int:
93+
def style_aware_wcswidth(text: str) -> int:
9494
"""
9595
Wrap wcswidth to make it compatible with strings that contains ANSI style sequences
96-
9796
:param text: the string being measured
9897
"""
9998
# Strip ANSI style sequences since they cause wcswidth to return -1
10099
return wcswidth(strip_style(text))
101100

102101

103-
def ansi_aware_write(fileobj: IO, msg: str) -> None:
102+
def style_aware_write(fileobj: IO, msg: str) -> None:
104103
"""
105104
Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
106-
107105
:param fileobj: the file object being written to
108106
:param msg: the string being written
109107
"""
@@ -114,8 +112,8 @@ def ansi_aware_write(fileobj: IO, msg: str) -> None:
114112

115113

116114
def fg_lookup(fg_name: str) -> str:
117-
"""Look up ANSI escape codes based on foreground color name.
118-
115+
"""
116+
Look up ANSI escape codes based on foreground color name.
119117
:param fg_name: foreground color name to look up ANSI escape code(s) for
120118
:return: ANSI escape code(s) associated with this color
121119
:raises ValueError if the color cannot be found
@@ -128,8 +126,8 @@ def fg_lookup(fg_name: str) -> str:
128126

129127

130128
def bg_lookup(bg_name: str) -> str:
131-
"""Look up ANSI escape codes based on background color name.
132-
129+
"""
130+
Look up ANSI escape codes based on background color name.
133131
:param bg_name: background color name to look up ANSI escape code(s) for
134132
:return: ANSI escape code(s) associated with this color
135133
:raises ValueError if the color cannot be found
@@ -142,8 +140,8 @@ def bg_lookup(bg_name: str) -> str:
142140

143141

144142
def style(text: Any, *, fg: str = '', bg: str = '', bold: bool = False, underline: bool = False) -> str:
145-
"""Styles a string with ANSI colors and/or styles and returns the new string.
146-
143+
"""
144+
Apply ANSI colors and/or styles to a string and return it.
147145
The styling is self contained which means that at the end of the string reset code(s) are issued
148146
to undo whatever styling was done at the beginning.
149147
@@ -210,14 +208,14 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
210208
# That will be included in the input lines calculations since that is where the cursor is.
211209
num_prompt_terminal_lines = 0
212210
for line in prompt_lines[:-1]:
213-
line_width = ansi_safe_wcswidth(line)
211+
line_width = style_aware_wcswidth(line)
214212
num_prompt_terminal_lines += int(line_width / terminal_columns) + 1
215213

216214
# Now calculate how many terminal lines are take up by the input
217215
last_prompt_line = prompt_lines[-1]
218-
last_prompt_line_width = ansi_safe_wcswidth(last_prompt_line)
216+
last_prompt_line_width = style_aware_wcswidth(last_prompt_line)
219217

220-
input_width = last_prompt_line_width + ansi_safe_wcswidth(line)
218+
input_width = last_prompt_line_width + style_aware_wcswidth(line)
221219

222220
num_input_terminal_lines = int(input_width / terminal_columns) + 1
223221

cmd2/argparse_completer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,11 @@ def _format_completions(self, action, completions: List[Union[str, CompletionIte
444444
completions.sort(key=self._cmd2_app.default_sort_key)
445445
self._cmd2_app.matches_sorted = True
446446

447-
token_width = ansi.ansi_safe_wcswidth(action.dest)
447+
token_width = ansi.style_aware_wcswidth(action.dest)
448448
completions_with_desc = []
449449

450450
for item in completions:
451-
item_width = ansi.ansi_safe_wcswidth(item)
451+
item_width = ansi.style_aware_wcswidth(item)
452452
if item_width > token_width:
453453
token_width = item_width
454454

@@ -585,7 +585,7 @@ def _complete_for_arg(self, arg_action: argparse.Action,
585585
def _print_message(msg: str) -> None:
586586
"""Print a message instead of tab completions and redraw the prompt and input line"""
587587
import sys
588-
ansi.ansi_aware_write(sys.stdout, msg + '\n')
588+
ansi.style_aware_write(sys.stdout, msg + '\n')
589589
rl_force_redisplay()
590590

591591
def _print_arg_hint(self, arg_action: argparse.Action) -> None:

cmd2/argparse_custom.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,11 +802,11 @@ def format_help(self) -> str:
802802
return formatter.format_help() + '\n'
803803

804804
def _print_message(self, message, file=None):
805-
# Override _print_message to use ansi_aware_write() since we use ANSI escape characters to support color
805+
# Override _print_message to use style_aware_write() since we use ANSI escape characters to support color
806806
if message:
807807
if file is None:
808808
file = sys.stderr
809-
ansi.ansi_aware_write(file, message)
809+
ansi.style_aware_write(file, message)
810810

811811

812812
# The default ArgumentParser class for a cmd2 app

cmd2/cmd2.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ def poutput(self, msg: Any = '', *, end: str = '\n') -> None:
439439
:param end: string appended after the end of the message, default a newline
440440
"""
441441
try:
442-
ansi.ansi_aware_write(self.stdout, "{}{}".format(msg, end))
442+
ansi.style_aware_write(self.stdout, "{}{}".format(msg, end))
443443
except BrokenPipeError:
444444
# This occurs if a command's output is being piped to another
445445
# process and that process closes before the command is
@@ -462,7 +462,7 @@ def perror(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) ->
462462
final_msg = ansi.style_error(msg)
463463
else:
464464
final_msg = "{}".format(msg)
465-
ansi.ansi_aware_write(sys.stderr, final_msg + end)
465+
ansi.style_aware_write(sys.stderr, final_msg + end)
466466

467467
def pwarning(self, msg: Any = '', *, end: str = '\n', apply_style: bool = True) -> None:
468468
"""Wraps perror, but applies ansi.style_warning by default
@@ -1096,7 +1096,7 @@ def _display_matches_gnu_readline(self, substitution: str, matches: List[str],
10961096
longest_match_length = 0
10971097

10981098
for cur_match in matches_to_display:
1099-
cur_length = ansi.ansi_safe_wcswidth(cur_match)
1099+
cur_length = ansi.style_aware_wcswidth(cur_match)
11001100
if cur_length > longest_match_length:
11011101
longest_match_length = cur_length
11021102
else:
@@ -2653,7 +2653,7 @@ def _print_topics(self, header: str, cmds: List[str], verbose: bool) -> None:
26532653
widest = 0
26542654
# measure the commands
26552655
for command in cmds:
2656-
width = ansi.ansi_safe_wcswidth(command)
2656+
width = ansi.style_aware_wcswidth(command)
26572657
if width > widest:
26582658
widest = width
26592659
# add a 4-space pad
@@ -3737,7 +3737,7 @@ class TestMyAppCase(Cmd2TestCase):
37373737
test_results = runner.run(testcase)
37383738
execution_time = time.time() - start_time
37393739
if test_results.wasSuccessful():
3740-
ansi.ansi_aware_write(sys.stderr, stream.read())
3740+
ansi.style_aware_write(sys.stderr, stream.read())
37413741
finish_msg = ' {0} transcript{1} passed in {2:.3f} seconds '.format(num_transcripts, plural, execution_time)
37423742
finish_msg = ansi.style_success(utils.align_center(finish_msg, fill_char='='))
37433743
self.poutput(finish_msg)

cmd2/rl_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ def rl_set_prompt(prompt: str) -> None: # pragma: no cover
193193

194194

195195
def rl_make_safe_prompt(prompt: str) -> str: # pragma: no cover
196-
"""Overcome bug in GNU Readline in relation to calculation of prompt length in presence of ANSI escape codes.
196+
"""Overcome bug in GNU Readline in relation to calculation of prompt length in presence of ANSI escape codes
197197
198198
:param prompt: original prompt
199199
:return: prompt safe to pass to GNU Readline

cmd2/utils.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
669669
if len(fill_char) != 1:
670670
raise TypeError("Fill character must be exactly one character long")
671671

672-
fill_char_width = ansi.ansi_safe_wcswidth(fill_char)
672+
fill_char_width = ansi.style_aware_wcswidth(fill_char)
673673
if fill_char_width == -1:
674674
raise (ValueError("Fill character is an unprintable character"))
675675

@@ -687,9 +687,9 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
687687
if index > 0:
688688
text_buf.write('\n')
689689

690-
# Use ansi_safe_wcswidth to support characters with display widths
690+
# Use style_aware_wcswidth to support characters with display widths
691691
# greater than 1 as well as ANSI style sequences
692-
line_width = ansi.ansi_safe_wcswidth(line)
692+
line_width = ansi.style_aware_wcswidth(line)
693693
if line_width == -1:
694694
raise(ValueError("Text to align contains an unprintable character"))
695695

@@ -717,8 +717,8 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
717717

718718
# In cases where the fill character display width didn't divide evenly into
719719
# the gaps being filled, pad the remainder with spaces.
720-
left_fill += ' ' * (left_fill_width - ansi.ansi_safe_wcswidth(left_fill))
721-
right_fill += ' ' * (right_fill_width - ansi.ansi_safe_wcswidth(right_fill))
720+
left_fill += ' ' * (left_fill_width - ansi.style_aware_wcswidth(left_fill))
721+
right_fill += ' ' * (right_fill_width - ansi.style_aware_wcswidth(right_fill))
722722

723723
text_buf.write(left_fill + line + right_fill)
724724

tests/test_ansi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ def test_strip_style():
1717
assert base_str == ansi.strip_style(ansi_str)
1818

1919

20-
def test_ansi_safe_wcswidth():
20+
def test_style_aware_wcswidth():
2121
base_str = HELLO_WORLD
2222
ansi_str = ansi.style(base_str, fg='green')
23-
assert ansi.ansi_safe_wcswidth(ansi_str) != len(ansi_str)
23+
assert ansi.style_aware_wcswidth(ansi_str) != len(ansi_str)
2424

2525

2626
def test_style_none():

tests/test_utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_align_text_term_width():
349349
fill_char = ' '
350350

351351
term_width = shutil.get_terminal_size().columns
352-
expected_fill = (term_width - ansi.ansi_safe_wcswidth(text)) * fill_char
352+
expected_fill = (term_width - ansi.style_aware_wcswidth(text)) * fill_char
353353

354354
aligned = cu.align_text(text, fill_char=fill_char, alignment=cu.TextAlignment.LEFT)
355355
assert aligned == text + expected_fill

0 commit comments

Comments
 (0)