@@ -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
0 commit comments