1from typing
import Iterator, List, Optional, Tuple
3from ._loop
import loop_first, loop_last
4from .console
import Console, ConsoleOptions, RenderableType, RenderResult
5from .jupyter
import JupyterMixin
6from .measure
import Measurement
7from .segment
import Segment
8from .style
import Style, StyleStack, StyleType
9from .styled
import Styled
13 """A renderable for a tree structure.
16 label (RenderableType): The renderable or str for the tree label.
17 style (StyleType, optional): Style of this tree. Defaults to "tree".
18 guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
19 expanded (bool, optional): Also display children. Defaults to True.
20 highlight (bool, optional): Highlight renderable (if str). Defaults to False.
25 label: RenderableType,
27 style: StyleType =
"tree",
28 guide_style: StyleType =
"tree.line",
29 expanded: bool =
True,
30 highlight: bool =
False,
31 hide_root: bool =
False,
36 self.children: List[Tree] = []
43 label: RenderableType,
45 style: Optional[StyleType] =
None,
46 guide_style: Optional[StyleType] =
None,
47 expanded: bool =
True,
48 highlight: Optional[bool] =
False,
53 label (RenderableType): The renderable or str for the tree label.
54 style (StyleType, optional): Style of this tree. Defaults to "tree".
55 guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
56 expanded (bool, optional): Also display children. Defaults to True.
57 highlight (Optional[bool], optional): Highlight renderable (if str). Defaults to False.
60 Tree: A new child Tree, which may be further modified.
64 style=self.
style if style
is None else style,
65 guide_style=self.
guide_style if guide_style
is None else guide_style,
67 highlight=self.
highlight if highlight
is None else highlight,
69 self.children.append(node)
73 self, console:
"Console", options:
"ConsoleOptions"
76 stack: List[Iterator[Tuple[bool, Tree]]] = []
83 guide_style = get_style(self.
guide_style, default=
"")
or null_style
84 SPACE, CONTINUE, FORK, END =
range(4)
86 ASCII_GUIDES = (
" ",
"| ",
"+-- ",
"`-- ")
88 (
" ",
"│ ",
"├── ",
"└── "),
89 (
" ",
"┃ ",
"┣━━ ",
"┗━━ "),
90 (
" ",
"║ ",
"╠══ ",
"╚══ "),
94 def make_guide(index: int, style: Style) -> Segment:
95 """Make a Segment for a level of the guide lines."""
97 line = ASCII_GUIDES[index]
103 levels: List[Segment] = [
make_guide(CONTINUE, guide_style)]
104 push(iter(loop_last([self])))
108 remove_guide_styles =
Style(bold=
False, underline2=
False)
115 last, node = next(stack_node)
116 except StopIteration:
119 guide_style = levels[-1].style
or null_style
126 levels[-1] =
make_guide(END, levels[-1].style
or null_style)
130 prefix = levels[(2
if self.
hide_root else 1) :]
143 for first, line
in loop_first(renderable_lines):
148 post_style=remove_guide_styles,
154 SPACE
if last
else CONTINUE, prefix[-1].style
or null_style
159 SPACE
if last
else CONTINUE, levels[-1].style
or null_style
170 self, console:
"Console", options:
"ConsoleOptions"
172 stack: List[Iterator[Tree]] = [iter([self])]
182 tree = next(iter_tree)
183 except StopIteration:
187 min_measure, max_measure = measure(console, options,
tree.label)
189 minimum = max(min_measure + indent, minimum)
190 maximum = max(max_measure + indent, maximum)
197if __name__ ==
"__main__":
205 table =
Table(row_styles=[
"",
"dim"])
211 table.add_row(
"Dec 20, 2019",
"Star Wars: The Rise of Skywalker",
"$952,110,690")
212 table.add_row(
"May 25, 2018",
"Solo: A Star Wars Story",
"$393,151,347")
213 table.add_row(
"Dec 15, 2017",
"Star Wars Ep. V111: The Last Jedi",
"$1,332,539,889")
214 table.add_row(
"Dec 16, 2016",
"Rogue One: A Star Wars Story",
"$1,332,439,889")
217class Segment(NamedTuple):
219 style: Optional[Style] = None
220 is_control: bool = False
222 syntax =
Syntax(code,
"python", theme=
"monokai", line_numbers=
True)
229> Markdown _all_ the things
233 root =
Tree(
"🌲 [b green]Rich Tree", highlight=
True, hide_root=
True)
235 node =
root.add(
":file_folder: Renderables", guide_style=
"red")
236 simple_node =
node.add(
":file_folder: [bold yellow]Atomic", guide_style=
"uu green")
241 ":file_folder: [bold magenta]Containers", guide_style=
"bold magenta"
"Measurement" __rich_measure__(self, "Console" console, "ConsoleOptions" options)
None __init__(self, RenderableType label, *StyleType style="tree", StyleType guide_style="tree.line", bool expanded=True, bool highlight=False, bool hide_root=False)
"RenderResult" __rich_console__(self, "Console" console, "ConsoleOptions" options)
"Tree" add(self, RenderableType label, *Optional[StyleType] style=None, Optional[StyleType] guide_style=None, bool expanded=True, Optional[bool] highlight=False)