Skip to content

Commit 2c7e924

Browse files
committed
Renamed function
1 parent 242efbd commit 2c7e924

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

cmd2/utils.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,8 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
652652
:param width: display width of the aligned text. Defaults to width of the terminal.
653653
:param tab_width: any tabs in the text will be replaced with this many spaces. if fill_char is a tab, then it will
654654
be converted to a space.
655-
:param truncate: if True, then text will be shortened to fit within the display width. The truncated portion is
656-
replaced by a '…' character. Defaults to False.
655+
:param truncate: if True, then each line will be shortened to fit within the display width. The truncated
656+
portions are replaced by a '…' character. Defaults to False.
657657
:return: aligned text
658658
:raises: TypeError if fill_char is more than one character
659659
ValueError if text or fill_char contains an unprintable character
@@ -694,7 +694,7 @@ def align_text(text: str, alignment: TextAlignment, *, fill_char: str = ' ',
694694
text_buf.write('\n')
695695

696696
if truncate:
697-
line = truncate_string(line, width)
697+
line = truncate_line(line, width)
698698

699699
line_width = ansi.style_aware_wcswidth(line)
700700
if line_width == -1:
@@ -801,36 +801,36 @@ def align_right(text: str, *, fill_char: str = ' ', width: Optional[int] = None,
801801
tab_width=tab_width, truncate=truncate)
802802

803803

804-
def truncate_string(text: str, max_width: int, *, tab_width: int = 4) -> str:
804+
def truncate_line(line: str, max_width: int, *, tab_width: int = 4) -> str:
805805
"""
806806
Truncate a single line to fit within a given display width. Any portion of the string that is truncated
807807
is replaced by a '…' character. Supports characters with display widths greater than 1. ANSI style sequences are
808808
safely ignored and do not count toward the display width. This means colored text is supported.
809809
810-
:param text: text to truncate
810+
:param line: text to truncate
811811
:param max_width: the maximum display width the resulting string is allowed to have
812812
:param tab_width: any tabs in the text will be replaced with this many spaces
813-
:return: string that has a display width less than or equal to width
813+
:return: line that has a display width less than or equal to width
814814
:raises: ValueError if text contains an unprintable character like a new line
815815
ValueError if max_width is less than 1
816816
"""
817817
from . import ansi
818818

819819
# Handle tabs
820-
text = text.replace('\t', ' ' * tab_width)
820+
line = line.replace('\t', ' ' * tab_width)
821821

822-
if ansi.style_aware_wcswidth(text) == -1:
822+
if ansi.style_aware_wcswidth(line) == -1:
823823
raise (ValueError("text contains an unprintable character"))
824824

825825
if max_width < 1:
826826
raise ValueError("max_width must be at least 1")
827827

828-
if ansi.style_aware_wcswidth(text) > max_width:
828+
if ansi.style_aware_wcswidth(line) > max_width:
829829
# Remove characters until we fit. Leave room for the ellipsis.
830-
text = text[:max_width - 1]
831-
while ansi.style_aware_wcswidth(text) > max_width - 1:
832-
text = text[:-1]
830+
line = line[:max_width - 1]
831+
while ansi.style_aware_wcswidth(line) > max_width - 1:
832+
line = line[:-1]
833833

834-
text += "\N{HORIZONTAL ELLIPSIS}"
834+
line += "\N{HORIZONTAL ELLIPSIS}"
835835

836-
return text
836+
return line

tests/test_utils.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -293,34 +293,34 @@ def test_context_flag_exit_err(context_flag):
293293
context_flag.__exit__()
294294

295295

296-
def test_truncate_string():
297-
text = 'long'
296+
def test_truncate_line():
297+
line = 'long'
298298
max_width = 3
299-
truncated = cu.truncate_string(text, max_width)
299+
truncated = cu.truncate_line(line, max_width)
300300
assert truncated == 'lo\N{HORIZONTAL ELLIPSIS}'
301301

302-
def test_truncate_string_newline_in_text():
303-
text = 'fo\no'
302+
def test_truncate_line_with_newline():
303+
line = 'fo\no'
304304
max_width = 2
305305
with pytest.raises(ValueError):
306-
cu.truncate_string(text, max_width)
306+
cu.truncate_line(line, max_width)
307307

308-
def test_truncate_string_width_is_too_small():
309-
text = 'foo'
308+
def test_truncate_line_width_is_too_small():
309+
line = 'foo'
310310
max_width = 0
311311
with pytest.raises(ValueError):
312-
cu.truncate_string(text, max_width)
312+
cu.truncate_line(line, max_width)
313313

314-
def test_truncate_string_wide_text():
315-
text = '苹苹other'
314+
def test_truncate_line_wide_text():
315+
line = '苹苹other'
316316
max_width = 6
317-
truncated = cu.truncate_string(text, max_width)
317+
truncated = cu.truncate_line(line, max_width)
318318
assert truncated == '苹苹o\N{HORIZONTAL ELLIPSIS}'
319319

320-
def test_truncate_string_tabs():
321-
text = 'has\ttab'
320+
def test_truncate_line_tabs():
321+
line = 'has\ttab'
322322
max_width = 9
323-
truncated = cu.truncate_string(text, max_width)
323+
truncated = cu.truncate_line(line, max_width)
324324
assert truncated == 'has t\N{HORIZONTAL ELLIPSIS}'
325325

326326
def test_align_text_fill_char_is_tab():

0 commit comments

Comments
 (0)