Divide text into a number of lines at given offsets. Args: offsets (Iterable[int]): Offsets used to divide text. Returns: Lines: New RichText instances between offsets.
(self, offsets: Iterable[int])
| 1104 | return lines |
| 1105 | |
| 1106 | def divide(self, offsets: Iterable[int]) -> Lines: |
| 1107 | """Divide text into a number of lines at given offsets. |
| 1108 | |
| 1109 | Args: |
| 1110 | offsets (Iterable[int]): Offsets used to divide text. |
| 1111 | |
| 1112 | Returns: |
| 1113 | Lines: New RichText instances between offsets. |
| 1114 | """ |
| 1115 | _offsets = list(offsets) |
| 1116 | |
| 1117 | if not _offsets: |
| 1118 | return Lines([self.copy()]) |
| 1119 | |
| 1120 | text = self.plain |
| 1121 | text_length = len(text) |
| 1122 | divide_offsets = [0, *_offsets, text_length] |
| 1123 | line_ranges = list(zip(divide_offsets, divide_offsets[1:])) |
| 1124 | |
| 1125 | style = self.style |
| 1126 | justify = self.justify |
| 1127 | overflow = self.overflow |
| 1128 | _Text = Text |
| 1129 | new_lines = Lines( |
| 1130 | _Text( |
| 1131 | text[start:end], |
| 1132 | style=style, |
| 1133 | justify=justify, |
| 1134 | overflow=overflow, |
| 1135 | ) |
| 1136 | for start, end in line_ranges |
| 1137 | ) |
| 1138 | if not self._spans: |
| 1139 | return new_lines |
| 1140 | |
| 1141 | _line_appends = [line._spans.append for line in new_lines._lines] |
| 1142 | line_count = len(line_ranges) |
| 1143 | _Span = Span |
| 1144 | |
| 1145 | for span_start, span_end, style in self._spans: |
| 1146 | lower_bound = 0 |
| 1147 | upper_bound = line_count |
| 1148 | start_line_no = (lower_bound + upper_bound) // 2 |
| 1149 | |
| 1150 | while True: |
| 1151 | line_start, line_end = line_ranges[start_line_no] |
| 1152 | if span_start < line_start: |
| 1153 | upper_bound = start_line_no - 1 |
| 1154 | elif span_start > line_end: |
| 1155 | lower_bound = start_line_no + 1 |
| 1156 | else: |
| 1157 | break |
| 1158 | start_line_no = (lower_bound + upper_bound) // 2 |
| 1159 | |
| 1160 | if span_end < line_end: |
| 1161 | end_line_no = start_line_no |
| 1162 | else: |
| 1163 | end_line_no = lower_bound = start_line_no |