Method: rich.tree.Tree.__rich_console__
Calls: 1, Exceptions: 1, Paths: 1Back
Path 1: 1 calls (1.0)
Console (1)
ConsoleOptions (1)
Segment (45)
StopIteration (1)
1def __rich_console__(
2 self, console: "Console", options: "ConsoleOptions"
3 ) -> "RenderResult":
4
5 stack: List[Iterator[Tuple[bool, Tree]]] = []
6 pop = stack.pop
7 push = stack.append
8 new_line = Segment.line()
9
10 get_style = console.get_style
11 null_style = Style.null()
12 guide_style = get_style(self.guide_style, default="") or null_style
13 SPACE, CONTINUE, FORK, END = range(4)
14
15 ASCII_GUIDES = (" ", "| ", "+-- ", "`-- ")
16 TREE_GUIDES = [
17 (" ", "│ ", "├── ", "└── "),
18 (" ", "┃ ", "┣━━ ", "┗━━ "),
19 (" ", "║ ", "╠══ ", "╚══ "),
20 ]
21 _Segment = Segment
22
23 def make_guide(index: int, style: Style) -> Segment:
24 """Make a Segment for a level of the guide lines."""
25 if options.ascii_only:
26 line = ASCII_GUIDES[index]
27 else:
28 guide = 1 if style.bold else (2 if style.underline2 else 0)
29 line = TREE_GUIDES[0 if options.legacy_windows else guide][index]
30 return _Segment(line, style)
31
32 levels: List[Segment] = [make_guide(CONTINUE, guide_style)]
33 push(iter(loop_last([self])))
34
35 guide_style_stack = StyleStack(get_style(self.guide_style))
36 style_stack = StyleStack(get_style(self.style))
37 remove_guide_styles = Style(bold=False, underline2=False)
38
39 depth = 0
40
41 while stack:
42 stack_node = pop()
43 try:
44 last, node = next(stack_node)
45 except StopIteration:
46 levels.pop()
47 if levels:
48 guide_style = levels[-1].style or null_style
49 levels[-1] = make_guide(FORK, guide_style)
50 guide_style_stack.pop()
51 style_stack.pop()
52 continue
53 push(stack_node)
54 if last:
55 levels[-1] = make_guide(END, levels[-1].style or null_style)
56
57 guide_style = guide_style_stack.current + get_style(node.guide_style)
58 style = style_stack.current + get_style(node.style)
59 prefix = levels[(2 if self.hide_root else 1) :]
60 renderable_lines = console.render_lines(
61 Styled(node.label, style),
62 options.update(
63 width=options.max_width
64 - sum(level.cell_length for level in prefix),
65 highlight=self.highlight,
66 height=None,
67 ),
68 pad=options.justify is not None,
69 )
70
71 if not (depth == 0 and self.hide_root):
72 for first, line in loop_first(renderable_lines):
73 if prefix:
74 yield from _Segment.apply_style(
75 prefix,
76 style.background_style,
77 post_style=remove_guide_styles,
78 )
79 yield from line
80 yield new_line
81 if first and prefix:
82 prefix[-1] = make_guide(
83 SPACE if last else CONTINUE, prefix[-1].style or null_style
84 )
85
86 if node.expanded and node.children:
87 levels[-1] = make_guide(
88 SPACE if last else CONTINUE, levels[-1].style or null_style
89 )
90 levels.append(
91 make_guide(END if len(node.children) == 1 else FORK, guide_style)
92 )
93 style_stack.push(get_style(node.style))
94 guide_style_stack.push(get_style(node.guide_style))
95 push(iter(loop_last(node.children)))
96 depth += 1