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

Class Span

rich/text.py:47–115  ·  view source on GitHub ↗

A marked up region in some text.

Source from the content-addressed store, hash-verified

45
46
47class Span(NamedTuple):
48 """A marked up region in some text."""
49
50 start: int
51 """Span start index."""
52 end: int
53 """Span end index."""
54 style: Union[str, Style]
55 """Style associated with the span."""
56
57 def __repr__(self) -> str:
58 return f"Span({self.start}, {self.end}, {self.style!r})"
59
60 def __bool__(self) -> bool:
61 return self.end > self.start
62
63 def split(self, offset: int) -> Tuple["Span", Optional["Span"]]:
64 """Split a span in to 2 from a given offset."""
65
66 if offset < self.start:
67 return self, None
68 if offset >= self.end:
69 return self, None
70
71 start, end, style = self
72 span1 = Span(start, min(end, offset), style)
73 span2 = Span(span1.end, end, style)
74 return span1, span2
75
76 def move(self, offset: int) -> "Span":
77 """Move start and end by a given offset.
78
79 Args:
80 offset (int): Number of characters to add to start and end.
81
82 Returns:
83 TextSpan: A new TextSpan with adjusted position.
84 """
85 start, end, style = self
86 return Span(start + offset, end + offset, style)
87
88 def right_crop(self, offset: int) -> "Span":
89 """Crop the span at the given offset.
90
91 Args:
92 offset (int): A value between start and end.
93
94 Returns:
95 Span: A new (possibly smaller) span.
96 """
97 start, end, style = self
98 if offset >= end:
99 return self
100 return Span(start, min(offset, end), style)
101
102 def extend(self, cells: int) -> "Span":
103 """Extend the span by the given number of cells.
104

Callers 15

test_decodeFunction · 0.90
test_richFunction · 0.90
test_renderFunction · 0.90
test_render_linkFunction · 0.90
test_render_combineFunction · 0.90
test_render_overlapFunction · 0.90

Calls

no outgoing calls

Tested by 15

test_decodeFunction · 0.72
test_richFunction · 0.72
test_renderFunction · 0.72
test_render_linkFunction · 0.72
test_render_combineFunction · 0.72
test_render_overlapFunction · 0.72
test_adjointFunction · 0.72