MCPcopy
hub / github.com/Textualize/rich / truncate

Method truncate

rich/text.py:859–884  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

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."""

Callers 8

test_truncate_ellipsisFunction · 0.95
alignMethod · 0.95
__rich_console__Method · 0.95
_rule_lineMethod · 0.95
wrapMethod · 0.45
justifyMethod · 0.45
align_textMethod · 0.45

Calls 2

cell_lenFunction · 0.85
set_cell_sizeFunction · 0.85

Tested by 2

test_truncate_ellipsisFunction · 0.76