Text content with marked up spans. This object can be considered immutable, although it might update its internal state in a way that is consistent with immutability.
| 131 | @rich.repr.auto |
| 132 | @total_ordering |
| 133 | class Content(Visual): |
| 134 | """Text content with marked up spans. |
| 135 | |
| 136 | This object can be considered immutable, although it might update its internal state |
| 137 | in a way that is consistent with immutability. |
| 138 | |
| 139 | """ |
| 140 | |
| 141 | __slots__ = ["_text", "_spans", "_cell_length"] |
| 142 | |
| 143 | _NORMALIZE_TEXT_ALIGN = {"start": "left", "end": "right", "justify": "full"} |
| 144 | |
| 145 | def __init__( |
| 146 | self, |
| 147 | text: str = "", |
| 148 | spans: list[Span] | None = None, |
| 149 | cell_length: int | None = None, |
| 150 | strip_control_codes: bool = True, |
| 151 | ) -> None: |
| 152 | """ |
| 153 | Initialize a Content object. |
| 154 | |
| 155 | Args: |
| 156 | text: text content. |
| 157 | spans: Optional list of spans. |
| 158 | cell_length: Cell length of text if known, otherwise `None`. |
| 159 | strip_control_codes: Strip control codes that may break output? |
| 160 | """ |
| 161 | |
| 162 | self._text: str = ( |
| 163 | _strip_control_codes(text) if strip_control_codes and text else text |
| 164 | ) |
| 165 | self._spans: list[Span] = [] if spans is None else spans |
| 166 | self._cell_length = cell_length |
| 167 | self._optimal_width_cache: int | None = None |
| 168 | self._minimal_width_cache: int | None = None |
| 169 | self._height_cache: tuple[tuple[int, str, bool] | None, int] = (None, 0) |
| 170 | self._divide_cache: ( |
| 171 | FIFOCache[Sequence[int], list[tuple[Span, int, int]]] | None |
| 172 | ) = None |
| 173 | self._split_cache: FIFOCache[tuple[str, bool, bool], list[Content]] | None = ( |
| 174 | None |
| 175 | ) |
| 176 | # If there are 1 or 0 spans, it can't be simplified further |
| 177 | self._simplified = len(self._spans) <= 1 |
| 178 | |
| 179 | def __str__(self) -> str: |
| 180 | return self._text |
| 181 | |
| 182 | @property |
| 183 | def _is_regular(self) -> bool: |
| 184 | """Check if the line is regular (spans.end > span.start for all spans). |
| 185 | |
| 186 | This is a debugging aid, and unlikely to be useful in your app. |
| 187 | |
| 188 | Returns: |
| 189 | `True` if the content is regular, `False` if it is not (and broken). |
| 190 | """ |
no outgoing calls
searching dependent graphs…