Truncate text if it is longer that a given width. Args: max_width (int): Maximum number of characters in text. overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow. pad (bool, optional): Pad with
(
self,
max_width: int,
*,
overflow: Optional["OverflowMethod"] = None,
pad: bool = False,
)
| 857 | self._spans[:] = result._spans |
| 858 | |
| 859 | def truncate( |
| 860 | self, |
| 861 | max_width: int, |
| 862 | *, |
| 863 | overflow: Optional["OverflowMethod"] = None, |
| 864 | pad: bool = False, |
| 865 | ) -> None: |
| 866 | """Truncate text if it is longer that a given width. |
| 867 | |
| 868 | Args: |
| 869 | max_width (int): Maximum number of characters in text. |
| 870 | overflow (str, optional): Overflow method: "crop", "fold", or "ellipsis". Defaults to None, to use self.overflow. |
| 871 | pad (bool, optional): Pad with spaces if the length is less than max_width. Defaults to False. |
| 872 | """ |
| 873 | _overflow = overflow or self.overflow or DEFAULT_OVERFLOW |
| 874 | if _overflow != "ignore": |
| 875 | length = cell_len(self.plain) |
| 876 | if length > max_width: |
| 877 | if _overflow == "ellipsis": |
| 878 | self.plain = set_cell_size(self.plain, max_width - 1) + "…" |
| 879 | else: |
| 880 | self.plain = set_cell_size(self.plain, max_width) |
| 881 | if pad and length < max_width: |
| 882 | spaces = max_width - length |
| 883 | self._text = [f"{self.plain}{' ' * spaces}"] |
| 884 | self._length = len(self.plain) |
| 885 | |
| 886 | def _trim_spans(self) -> None: |
| 887 | """Remove or modify any spans that are over the end of the text.""" |