Slice a RichTextLines object. The object itself is not changed. A sliced instance is returned. Args: begin: (int) Beginning line index (inclusive). Must be >= 0. end: (int) Ending line index (exclusive). Must be >= 0. Returns: (RichTextLines) Sliced output instance o
(self, begin, end)
| 228 | return len(self._lines) |
| 229 | |
| 230 | def slice(self, begin, end): |
| 231 | """Slice a RichTextLines object. |
| 232 | |
| 233 | The object itself is not changed. A sliced instance is returned. |
| 234 | |
| 235 | Args: |
| 236 | begin: (int) Beginning line index (inclusive). Must be >= 0. |
| 237 | end: (int) Ending line index (exclusive). Must be >= 0. |
| 238 | |
| 239 | Returns: |
| 240 | (RichTextLines) Sliced output instance of RichTextLines. |
| 241 | |
| 242 | Raises: |
| 243 | ValueError: If begin or end is negative. |
| 244 | """ |
| 245 | |
| 246 | if begin < 0 or end < 0: |
| 247 | raise ValueError("Encountered negative index.") |
| 248 | |
| 249 | # Copy lines. |
| 250 | lines = self.lines[begin:end] |
| 251 | |
| 252 | # Slice font attribute segments. |
| 253 | font_attr_segs = {} |
| 254 | for key in self.font_attr_segs: |
| 255 | if key >= begin and key < end: |
| 256 | font_attr_segs[key - begin] = self.font_attr_segs[key] |
| 257 | |
| 258 | # Slice annotations. |
| 259 | annotations = {} |
| 260 | for key in self.annotations: |
| 261 | if not isinstance(key, int): |
| 262 | # Annotations can contain keys that are not line numbers. |
| 263 | annotations[key] = self.annotations[key] |
| 264 | elif key >= begin and key < end: |
| 265 | annotations[key - begin] = self.annotations[key] |
| 266 | |
| 267 | return RichTextLines( |
| 268 | lines, font_attr_segs=font_attr_segs, annotations=annotations) |
| 269 | |
| 270 | def extend(self, other): |
| 271 | """Extend this instance of RichTextLines with another instance. |