A style applied to a range of character offsets.
| 81 | |
| 82 | @rich.repr.auto |
| 83 | class Span(NamedTuple): |
| 84 | """A style applied to a range of character offsets.""" |
| 85 | |
| 86 | start: int |
| 87 | end: int |
| 88 | style: Style | str |
| 89 | |
| 90 | def __rich_repr__(self) -> rich.repr.Result: |
| 91 | yield self.start |
| 92 | yield self.end |
| 93 | yield "style", self.style |
| 94 | |
| 95 | def extend(self, cells: int) -> "Span": |
| 96 | """Extend the span by the given number of cells. |
| 97 | |
| 98 | Args: |
| 99 | cells (int): Additional space to add to end of span. |
| 100 | |
| 101 | Returns: |
| 102 | Span: A span. |
| 103 | """ |
| 104 | if cells: |
| 105 | start, end, style = self |
| 106 | return Span(start, end + cells, style) |
| 107 | return self |
| 108 | |
| 109 | def _shift(self, distance: int) -> "Span": |
| 110 | """Shift a span a given distance. |
| 111 | |
| 112 | Note that the start offset is clamped to 0. |
| 113 | The end offset is not clamped, as it is assumed this has already been checked by the caller. |
| 114 | |
| 115 | Args: |
| 116 | distance: Number of characters to move. |
| 117 | |
| 118 | Returns: |
| 119 | New Span. |
| 120 | """ |
| 121 | if distance < 0: |
| 122 | start, end, style = self |
| 123 | return Span( |
| 124 | offset if (offset := start + distance) > 0 else 0, end + distance, style |
| 125 | ) |
| 126 | else: |
| 127 | start, end, style = self |
| 128 | return Span(start + distance, end + distance, style) |
| 129 | |
| 130 | |
| 131 | @rich.repr.auto |
no outgoing calls
searching dependent graphs…