diff options
-rw-r--r-- | cmd2/utils.py | 28 | ||||
-rw-r--r-- | tests/test_utils.py | 30 |
2 files changed, 29 insertions, 29 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index 32781e28..3e154bd2 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -652,8 +652,8 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ', :param width: display width of the aligned text. Defaults to width of the terminal. :param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will be converted to a space. - :param truncate: if True, then text will be shortened to fit within the display width. The truncated portion is - replaced by a '…' character. Defaults to False. + :param truncate: if True, then each line will be shortened to fit within the display width. The truncated + portions are replaced by a '…' character. Defaults to False. :return: aligned text :raises: TypeError if fill_char is more than one character ValueError if text or fill_char contains an unprintable character @@ -694,7 +694,7 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ', text_buf.write('\n') if truncate: - line = truncate_string(line, width) + line = truncate_line(line, width) line_width = ansi.style_aware_wcswidth(line) if line_width == -1: @@ -801,36 +801,36 @@ def align_right(text: str, *, fill_char: str = ' ', width: Optional[int] = None, tab_width=tab_width, truncate=truncate) -def truncate_string(text: str, max_width: int, *, tab_width: int = 4) -> str: +def truncate_line(line: str, max_width: int, *, tab_width: int = 4) -> str: """ Truncate a single line to fit within a given display width. Any portion of the string that is truncated is replaced by a '…' character. Supports characters with display widths greater than 1. ANSI style sequences are safely ignored and do not count toward the display width. This means colored text is supported. - :param text: text to truncate + :param line: text to truncate :param max_width: the maximum display width the resulting string is allowed to have :param tab_width: any tabs in the text will be replaced with this many spaces - :return: string that has a display width less than or equal to width + :return: line that has a display width less than or equal to width :raises: ValueError if text contains an unprintable character like a new line ValueError if max_width is less than 1 """ from . import ansi # Handle tabs - text = text.replace('\t', ' ' * tab_width) + line = line.replace('\t', ' ' * tab_width) - if ansi.style_aware_wcswidth(text) == -1: + if ansi.style_aware_wcswidth(line) == -1: raise (ValueError("text contains an unprintable character")) if max_width < 1: raise ValueError("max_width must be at least 1") - if ansi.style_aware_wcswidth(text) > max_width: + if ansi.style_aware_wcswidth(line) > max_width: # Remove characters until we fit. Leave room for the ellipsis. - text = text[:max_width - 1] - while ansi.style_aware_wcswidth(text) > max_width - 1: - text = text[:-1] + line = line[:max_width - 1] + while ansi.style_aware_wcswidth(line) > max_width - 1: + line = line[:-1] - text += "\N{HORIZONTAL ELLIPSIS}" + line += "\N{HORIZONTAL ELLIPSIS}" - return text + return line diff --git a/tests/test_utils.py b/tests/test_utils.py index 08b282b7..c2fb9c2c 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -293,34 +293,34 @@ def test_context_flag_exit_err(context_flag): context_flag.__exit__() -def test_truncate_string(): - text = 'long' +def test_truncate_line(): + line = 'long' max_width = 3 - truncated = cu.truncate_string(text, max_width) + truncated = cu.truncate_line(line, max_width) assert truncated == 'lo\N{HORIZONTAL ELLIPSIS}' -def test_truncate_string_newline_in_text(): - text = 'fo\no' +def test_truncate_line_with_newline(): + line = 'fo\no' max_width = 2 with pytest.raises(ValueError): - cu.truncate_string(text, max_width) + cu.truncate_line(line, max_width) -def test_truncate_string_width_is_too_small(): - text = 'foo' +def test_truncate_line_width_is_too_small(): + line = 'foo' max_width = 0 with pytest.raises(ValueError): - cu.truncate_string(text, max_width) + cu.truncate_line(line, max_width) -def test_truncate_string_wide_text(): - text = '苹苹other' +def test_truncate_line_wide_text(): + line = '苹苹other' max_width = 6 - truncated = cu.truncate_string(text, max_width) + truncated = cu.truncate_line(line, max_width) assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}' -def test_truncate_string_tabs(): - text = 'has\ttab' +def test_truncate_line_tabs(): + line = 'has\ttab' max_width = 9 - truncated = cu.truncate_string(text, max_width) + truncated = cu.truncate_line(line, max_width) assert truncated == 'has t\N{HORIZONTAL ELLIPSIS}' def test_align_text_fill_char_is_tab(): |