diff options
-rw-r--r-- | CHANGELOG.md | 3 | ||||
-rw-r--r-- | docs/api/utility_functions.rst | 2 | ||||
-rw-r--r-- | tests/test_utils.py | 15 |
3 files changed, 20 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index e5ee988f..73aa5b23 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ * Enhancements * Flushing stderr when setting the window title and printing alerts for better responsiveness in cases where stderr is not unbuffered. + * Added function to truncate a single line to fit within a given display width. `cmd2.utils.truncate_line` + supports characters with display widths greater than 1 and ANSI style sequences. + * Added line truncation support to `cmd2.utils` text alignment functions. ## 0.9.23 (January 9, 2020) * Bug Fixes diff --git a/docs/api/utility_functions.rst b/docs/api/utility_functions.rst index e083cafe..e2d6f036 100644 --- a/docs/api/utility_functions.rst +++ b/docs/api/utility_functions.rst @@ -17,6 +17,8 @@ Utility Functions .. autofunction:: cmd2.utils.align_right +.. autofunction:: cmd2.utils.truncate_line + .. autofunction:: cmd2.utils.strip_quotes .. autofunction:: cmd2.utils.namedtuple_with_defaults diff --git a/tests/test_utils.py b/tests/test_utils.py index c2fb9c2c..9dd54ee2 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -317,6 +317,13 @@ def test_truncate_line_wide_text(): truncated = cu.truncate_line(line, max_width) assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}' +def test_truncate_line_split_wide_text(): + """Test when truncation results in a string which is shorter than max_width""" + line = '1苹2苹' + max_width = 3 + truncated = cu.truncate_line(line, max_width) + assert truncated == '1\N{HORIZONTAL ELLIPSIS}' + def test_truncate_line_tabs(): line = 'has\ttab' max_width = 9 @@ -379,6 +386,14 @@ def test_align_text_wider_than_width_truncate(): aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True) assert aligned == 'long te\N{HORIZONTAL ELLIPSIS}' +def test_align_text_wider_than_width_truncate_add_fill(): + """Test when truncation results in a string which is shorter than width and align_text adds filler""" + text = '1苹2苹' + fill_char = '-' + width = 3 + aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True) + assert aligned == '1\N{HORIZONTAL ELLIPSIS}-' + def test_align_text_has_unprintable(): text = 'foo\x02' fill_char = '-' |