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

Method append

rich/text.py:964–1006  ·  rich/text.py::Text.append

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
    )

Source from the content-addressed store, hash-verified

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

Callers 15

test_plain_propertyFunction · 0.95
test_copyFunction · 0.95
test_set_lengthFunction · 0.95
test_appendFunction · 0.95
test_splitFunction · 0.95
test_divideFunction · 0.95
test_right_cropFunction · 0.95
test_strip_control_codesFunction · 0.95
walk_directoryFunction · 0.95
__call__Method · 0.95

Calls 4

strip_control_codesFunction · 0.85
SpanClass · 0.85
extendMethod · 0.45
copyMethod · 0.45

Tested by 12

test_plain_propertyFunction · 0.76
test_copyFunction · 0.76
test_set_lengthFunction · 0.76
test_appendFunction · 0.76
test_splitFunction · 0.76
test_divideFunction · 0.76
test_right_cropFunction · 0.76
test_strip_control_codesFunction · 0.76