Adds indent guide lines to text. Args: indent_size (Optional[int]): Size of indentation, or None to auto detect. Defaults to None. character (str, optional): Character to use for indentation. Defaults to "│". style (Union[Style, str], optional): Style of
(
self,
indent_size: Optional[int] = None,
*,
character: str = "│",
style: StyleType = "dim green",
)
| 1287 | return indentation |
| 1288 | |
| 1289 | def with_indent_guides( |
| 1290 | self, |
| 1291 | indent_size: Optional[int] = None, |
| 1292 | *, |
| 1293 | character: str = "│", |
| 1294 | style: StyleType = "dim green", |
| 1295 | ) -> "Text": |
| 1296 | """Adds indent guide lines to text. |
| 1297 | |
| 1298 | Args: |
| 1299 | indent_size (Optional[int]): Size of indentation, or None to auto detect. Defaults to None. |
| 1300 | character (str, optional): Character to use for indentation. Defaults to "│". |
| 1301 | style (Union[Style, str], optional): Style of indent guides. |
| 1302 | |
| 1303 | Returns: |
| 1304 | Text: New text with indentation guides. |
| 1305 | """ |
| 1306 | |
| 1307 | _indent_size = self.detect_indentation() if indent_size is None else indent_size |
| 1308 | |
| 1309 | text = self.copy() |
| 1310 | text.expand_tabs() |
| 1311 | indent_line = f"{character}{' ' * (_indent_size - 1)}" |
| 1312 | |
| 1313 | re_indent = re.compile(r"^( *)(.*)$") |
| 1314 | new_lines: List[Text] = [] |
| 1315 | add_line = new_lines.append |
| 1316 | blank_lines = 0 |
| 1317 | for line in text.split(allow_blank=True): |
| 1318 | match = re_indent.match(line.plain) |
| 1319 | if not match or not match.group(2): |
| 1320 | blank_lines += 1 |
| 1321 | continue |
| 1322 | indent = match.group(1) |
| 1323 | full_indents, remaining_space = divmod(len(indent), _indent_size) |
| 1324 | new_indent = f"{indent_line * full_indents}{' ' * remaining_space}" |
| 1325 | line.plain = new_indent + line.plain[len(new_indent) :] |
| 1326 | line.stylize(style, 0, len(new_indent)) |
| 1327 | if blank_lines: |
| 1328 | new_lines.extend([Text(new_indent, style=style)] * blank_lines) |
| 1329 | blank_lines = 0 |
| 1330 | add_line(line) |
| 1331 | if blank_lines: |
| 1332 | new_lines.extend([Text("", style=style)] * blank_lines) |
| 1333 | |
| 1334 | new_text = text.blank_copy("\n").join(new_lines) |
| 1335 | return new_text |
| 1336 | |
| 1337 | |
| 1338 | if __name__ == "__main__": # pragma: no cover |