Add text with an optional style. Args: text (Union[Text, str]): A str or Text to append. style (str, optional): A style name. Defaults to None. Returns: Text: Returns self for chaining.
(
self, text: Union["Text", str], style: Optional[Union[str, "Style"]] = None
)
| 962 | self.pad_left(excess_space, character) |
| 963 | |
| 964 | def append( |
| 965 | self, text: Union[class="st">"Text", str], style: Optional[Union[str, class="st">"Style"]] = None |
| 966 | ) -> class="st">"Text": |
| 967 | class="st">"""Add text with an optional style. |
| 968 | |
| 969 | Args: |
| 970 | text (Union[Text, str]): A str or Text to append. |
| 971 | style (str, optional): A style name. Defaults to None. |
| 972 | |
| 973 | Returns: |
| 974 | Text: Returns self for chaining. |
| 975 | class="st">""" |
| 976 | |
| 977 | if not isinstance(text, (str, Text)): |
| 978 | raise TypeError(class="st">"Only str or Text can be appended to Text") |
| 979 | |
| 980 | if len(text): |
| 981 | if isinstance(text, str): |
| 982 | sanitized_text = strip_control_codes(text) |
| 983 | self._text.append(sanitized_text) |
| 984 | offset = len(self) |
| 985 | text_length = len(sanitized_text) |
| 986 | if style: |
| 987 | self._spans.append(Span(offset, offset + text_length, style)) |
| 988 | self._length += text_length |
| 989 | elif isinstance(text, Text): |
| 990 | _Span = Span |
| 991 | if style is not None: |
| 992 | raise ValueError( |
| 993 | class="st">"style must not be set when appending Text instance" |
| 994 | ) |
| 995 | text_length = self._length |
| 996 | if text.style: |
| 997 | self._spans.append( |
| 998 | _Span(text_length, text_length + len(text), text.style) |
| 999 | ) |
| 1000 | self._text.append(text.plain) |
| 1001 | self._spans.extend( |
| 1002 | _Span(start + text_length, end + text_length, style) |
| 1003 | for start, end, style in text._spans.copy() |
| 1004 | ) |
| 1005 | self._length += len(text) |
| 1006 | return self |
| 1007 | |
| 1008 | def append_text(self, text: class="st">"Text") -> class="st">"Text": |
| 1009 | class="st">"""Append another Text instance. This method is more performant than Text.append, but |