Skip to content

Commit e23f9f6

Browse files
committed
Updating unit tests and docs
1 parent 2c7e924 commit e23f9f6

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
* Enhancements
33
* Flushing stderr when setting the window title and printing alerts for better responsiveness in cases where
44
stderr is not unbuffered.
5+
* Added function to truncate a single line to fit within a given display width. `cmd2.utils.truncate_line`
6+
supports characters with display widths greater than 1 and ANSI style sequences.
7+
* Added line truncation support to `cmd2.utils` text alignment functions.
58

69
## 0.9.23 (January 9, 2020)
710
* Bug Fixes

docs/api/utility_functions.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ Utility Functions
1717

1818
.. autofunction:: cmd2.utils.align_right
1919

20+
.. autofunction:: cmd2.utils.truncate_line
21+
2022
.. autofunction:: cmd2.utils.strip_quotes
2123

2224
.. autofunction:: cmd2.utils.namedtuple_with_defaults

tests/test_utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,13 @@ def test_truncate_line_wide_text():
317317
truncated = cu.truncate_line(line, max_width)
318318
assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}'
319319

320+
def test_truncate_line_split_wide_text():
321+
"""Test when truncation results in a string which is shorter than max_width"""
322+
line = '1苹2苹'
323+
max_width = 3
324+
truncated = cu.truncate_line(line, max_width)
325+
assert truncated == '1\N{HORIZONTAL ELLIPSIS}'
326+
320327
def test_truncate_line_tabs():
321328
line = 'has\ttab'
322329
max_width = 9
@@ -379,6 +386,14 @@ def test_align_text_wider_than_width_truncate():
379386
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True)
380387
assert aligned == 'long te\N{HORIZONTAL ELLIPSIS}'
381388

389+
def test_align_text_wider_than_width_truncate_add_fill():
390+
"""Test when truncation results in a string which is shorter than width and align_text adds filler"""
391+
text = '1苹2苹'
392+
fill_char = '-'
393+
width = 3
394+
aligned = cu.align_text(text, cu.TextAlignment.LEFT, fill_char=fill_char, width=width, truncate=True)
395+
assert aligned == '1\N{HORIZONTAL ELLIPSIS}-'
396+
382397
def test_align_text_has_unprintable():
383398
text = 'foo\x02'
384399
fill_char = '-'

0 commit comments

Comments
 (0)