We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Tree: Flag to hide root from display 
In certain situations it would be useful to hide the root of a tree, either to save on screen space or for clarity. A `hide_root` flag could allow this behavior.  For example: 
```
>>> tree = Tree("Dir to print")
>>> tree.add("dir1").add("file1")
>>> tree.add("file2")
>>> print(tree)

Dir to print
├── dir1                                                                                     
│   └── file1                                                                                
└── file2                                                                                    
```
could instead be display as
```
>>> tree = Tree("Dir to print", hide_root=True)
>>> tree.add("dir1").add("file1")
>>> tree.add("file2")
>>> print(tree)

dir1                                                                                     
└── file1                                                                                
file2                                                                                    
```

Thanks!

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `rich/tree.py`
Content:
```
1 from typing import Iterator, List, Optional, Tuple
2 
3 from ._loop import loop_first, loop_last
4 from .console import Console, ConsoleOptions, RenderableType, RenderResult
5 from .jupyter import JupyterMixin
6 from .measure import Measurement
7 from .segment import Segment
8 from .style import Style, StyleStack, StyleType
9 from .styled import Styled
10 
11 
12 class Tree(JupyterMixin):
13     """A renderable for a tree structure.
14 
15     Args:
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.
21     """
22 
23     def __init__(
24         self,
25         label: RenderableType,
26         *,
27         style: StyleType = "tree",
28         guide_style: StyleType = "tree.line",
29         expanded: bool = True,
30         highlight: bool = False,
31     ) -> None:
32         self.label = label
33         self.style = style
34         self.guide_style = guide_style
35         self.children: List[Tree] = []
36         self.expanded = expanded
37         self.highlight = highlight
38 
39     def add(
40         self,
41         label: RenderableType,
42         *,
43         style: Optional[StyleType] = None,
44         guide_style: Optional[StyleType] = None,
45         expanded: bool = True,
46         highlight: bool = False,
47     ) -> "Tree":
48         """Add a child tree.
49 
50         Args:
51             label (RenderableType): The renderable or str for the tree label.
52             style (StyleType, optional): Style of this tree. Defaults to "tree".
53             guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
54             expanded (bool, optional): Also display children. Defaults to True.
55             highlight (Optional[bool], optional): Highlight renderable (if str). Defaults to False.
56 
57         Returns:
58             Tree: A new child Tree, which may be further modified.
59         """
60         node = Tree(
61             label,
62             style=self.style if style is None else style,
63             guide_style=self.guide_style if guide_style is None else guide_style,
64             expanded=expanded,
65             highlight=self.highlight if highlight is None else highlight,
66         )
67         self.children.append(node)
68         return node
69 
70     def __rich_console__(
71         self, console: "Console", options: "ConsoleOptions"
72     ) -> "RenderResult":
73 
74         stack: List[Iterator[Tuple[bool, Tree]]] = []
75         pop = stack.pop
76         push = stack.append
77         new_line = Segment.line()
78 
79         get_style = console.get_style
80         null_style = Style.null()
81         guide_style = get_style(self.guide_style, default="") or null_style
82         SPACE, CONTINUE, FORK, END = range(4)
83 
84         ASCII_GUIDES = ("    ", "|   ", "+-- ", "`-- ")
85         TREE_GUIDES = [
86             ("    ", "│   ", "├── ", "└── "),
87             ("    ", "┃   ", "┣━━ ", "┗━━ "),
88             ("    ", "║   ", "╠══ ", "╚══ "),
89         ]
90         _Segment = Segment
91 
92         def make_guide(index: int, style: Style) -> Segment:
93             """Make a Segment for a level of the guide lines."""
94             if options.ascii_only:
95                 line = ASCII_GUIDES[index]
96             else:
97                 guide = 1 if style.bold else (2 if style.underline2 else 0)
98                 line = TREE_GUIDES[0 if options.legacy_windows else guide][index]
99             return _Segment(line, style)
100 
101         levels: List[Segment] = [make_guide(CONTINUE, guide_style)]
102         push(iter(loop_last([self])))
103 
104         guide_style_stack = StyleStack(get_style(self.guide_style))
105         style_stack = StyleStack(get_style(self.style))
106         remove_guide_styles = Style(bold=False, underline2=False)
107 
108         while stack:
109             stack_node = pop()
110             try:
111                 last, node = next(stack_node)
112             except StopIteration:
113                 levels.pop()
114                 if levels:
115                     guide_style = levels[-1].style or null_style
116                     levels[-1] = make_guide(FORK, guide_style)
117                     guide_style_stack.pop()
118                     style_stack.pop()
119                 continue
120             push(stack_node)
121             if last:
122                 levels[-1] = make_guide(END, levels[-1].style or null_style)
123 
124             guide_style = guide_style_stack.current + get_style(node.guide_style)
125             style = style_stack.current + get_style(node.style)
126             prefix = levels[1:]
127             renderable_lines = console.render_lines(
128                 Styled(node.label, style),
129                 options.update(
130                     width=options.max_width
131                     - sum(level.cell_length for level in prefix),
132                     highlight=self.highlight,
133                     height=None,
134                 ),
135             )
136             for first, line in loop_first(renderable_lines):
137                 if prefix:
138                     yield from _Segment.apply_style(
139                         prefix,
140                         style.background_style,
141                         post_style=remove_guide_styles,
142                     )
143                 yield from line
144                 yield new_line
145                 if first and prefix:
146                     prefix[-1] = make_guide(
147                         SPACE if last else CONTINUE, prefix[-1].style or null_style
148                     )
149 
150             if node.expanded and node.children:
151                 levels[-1] = make_guide(
152                     SPACE if last else CONTINUE, levels[-1].style or null_style
153                 )
154                 levels.append(
155                     make_guide(END if len(node.children) == 1 else FORK, guide_style)
156                 )
157                 style_stack.push(get_style(node.style))
158                 guide_style_stack.push(get_style(node.guide_style))
159                 push(iter(loop_last(node.children)))
160 
161     def __rich_measure__(
162         self, console: "Console", options: "ConsoleOptions"
163     ) -> "Measurement":
164         stack: List[Iterator[Tree]] = [iter([self])]
165         pop = stack.pop
166         push = stack.append
167         minimum = 0
168         maximum = 0
169         measure = Measurement.get
170         level = 0
171         while stack:
172             iter_tree = pop()
173             try:
174                 tree = next(iter_tree)
175             except StopIteration:
176                 level -= 1
177                 continue
178             push(iter_tree)
179             min_measure, max_measure = measure(console, options, tree.label)
180             indent = level * 4
181             minimum = max(min_measure + indent, minimum)
182             maximum = max(max_measure + indent, maximum)
183             if tree.expanded and tree.children:
184                 push(iter(tree.children))
185                 level += 1
186         return Measurement(minimum, maximum)
187 
188 
189 if __name__ == "__main__":  # pragma: no cover
190 
191     from rich.console import Group
192     from rich.markdown import Markdown
193     from rich.panel import Panel
194     from rich.syntax import Syntax
195     from rich.table import Table
196 
197     table = Table(row_styles=["", "dim"])
198 
199     table.add_column("Released", style="cyan", no_wrap=True)
200     table.add_column("Title", style="magenta")
201     table.add_column("Box Office", justify="right", style="green")
202 
203     table.add_row("Dec 20, 2019", "Star Wars: The Rise of Skywalker", "$952,110,690")
204     table.add_row("May 25, 2018", "Solo: A Star Wars Story", "$393,151,347")
205     table.add_row("Dec 15, 2017", "Star Wars Ep. V111: The Last Jedi", "$1,332,539,889")
206     table.add_row("Dec 16, 2016", "Rogue One: A Star Wars Story", "$1,332,439,889")
207 
208     code = """\
209 class Segment(NamedTuple):
210     text: str = ""    
211     style: Optional[Style] = None    
212     is_control: bool = False    
213 """
214     syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
215 
216     markdown = Markdown(
217         """\
218 ### example.md
219 > Hello, World!
220 > 
221 > Markdown _all_ the things
222 """
223     )
224 
225     root = Tree("🌲 [b green]Rich Tree", highlight=True)
226 
227     node = root.add(":file_folder: Renderables", guide_style="red")
228     simple_node = node.add(":file_folder: [bold yellow]Atomic", guide_style="uu green")
229     simple_node.add(Group("📄 Syntax", syntax))
230     simple_node.add(Group("📄 Markdown", Panel(markdown, border_style="green")))
231 
232     containers_node = node.add(
233         ":file_folder: [bold magenta]Containers", guide_style="bold magenta"
234     )
235     containers_node.expanded = True
236     panel = Panel.fit("Just a panel", border_style="red")
237     containers_node.add(Group("📄 Panels", panel))
238 
239     containers_node.add(Group("📄 [b magenta]Table", table))
240 
241     console = Console()
242     console.print(root)
243
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/rich/tree.py b/rich/tree.py
--- a/rich/tree.py
+++ b/rich/tree.py
@@ -28,6 +28,7 @@
         guide_style: StyleType = "tree.line",
         expanded: bool = True,
         highlight: bool = False,
+        hide_root: bool = False,
     ) -> None:
         self.label = label
         self.style = style
@@ -35,6 +36,7 @@
         self.children: List[Tree] = []
         self.expanded = expanded
         self.highlight = highlight
+        self.hide_root = hide_root
 
     def add(
         self,
@@ -105,6 +107,8 @@
         style_stack = StyleStack(get_style(self.style))
         remove_guide_styles = Style(bold=False, underline2=False)
 
+        depth = 0
+
         while stack:
             stack_node = pop()
             try:
@@ -123,7 +127,7 @@
 
             guide_style = guide_style_stack.current + get_style(node.guide_style)
             style = style_stack.current + get_style(node.style)
-            prefix = levels[1:]
+            prefix = levels[(2 if self.hide_root else 1) :]
             renderable_lines = console.render_lines(
                 Styled(node.label, style),
                 options.update(
@@ -133,19 +137,21 @@
                     height=None,
                 ),
             )
-            for first, line in loop_first(renderable_lines):
-                if prefix:
-                    yield from _Segment.apply_style(
-                        prefix,
-                        style.background_style,
-                        post_style=remove_guide_styles,
-                    )
-                yield from line
-                yield new_line
-                if first and prefix:
-                    prefix[-1] = make_guide(
-                        SPACE if last else CONTINUE, prefix[-1].style or null_style
-                    )
+
+            if not (depth == 0 and self.hide_root):
+                for first, line in loop_first(renderable_lines):
+                    if prefix:
+                        yield from _Segment.apply_style(
+                            prefix,
+                            style.background_style,
+                            post_style=remove_guide_styles,
+                        )
+                    yield from line
+                    yield new_line
+                    if first and prefix:
+                        prefix[-1] = make_guide(
+                            SPACE if last else CONTINUE, prefix[-1].style or null_style
+                        )
 
             if node.expanded and node.children:
                 levels[-1] = make_guide(
@@ -157,6 +163,7 @@
                 style_stack.push(get_style(node.style))
                 guide_style_stack.push(get_style(node.guide_style))
                 push(iter(loop_last(node.children)))
+                depth += 1
 
     def __rich_measure__(
         self, console: "Console", options: "ConsoleOptions"
@@ -222,7 +229,7 @@
 """
     )
 
-    root = Tree("🌲 [b green]Rich Tree", highlight=True)
+    root = Tree("🌲 [b green]Rich Tree", highlight=True, hide_root=True)
 
     node = root.add(":file_folder: Renderables", guide_style="red")
     simple_node = node.add(":file_folder: [bold yellow]Atomic", guide_style="uu green")

{"golden_diff": "diff --git a/rich/tree.py b/rich/tree.py\n--- a/rich/tree.py\n+++ b/rich/tree.py\n@@ -28,6 +28,7 @@\n         guide_style: StyleType = \"tree.line\",\n         expanded: bool = True,\n         highlight: bool = False,\n+        hide_root: bool = False,\n     ) -> None:\n         self.label = label\n         self.style = style\n@@ -35,6 +36,7 @@\n         self.children: List[Tree] = []\n         self.expanded = expanded\n         self.highlight = highlight\n+        self.hide_root = hide_root\n \n     def add(\n         self,\n@@ -105,6 +107,8 @@\n         style_stack = StyleStack(get_style(self.style))\n         remove_guide_styles = Style(bold=False, underline2=False)\n \n+        depth = 0\n+\n         while stack:\n             stack_node = pop()\n             try:\n@@ -123,7 +127,7 @@\n \n             guide_style = guide_style_stack.current + get_style(node.guide_style)\n             style = style_stack.current + get_style(node.style)\n-            prefix = levels[1:]\n+            prefix = levels[(2 if self.hide_root else 1) :]\n             renderable_lines = console.render_lines(\n                 Styled(node.label, style),\n                 options.update(\n@@ -133,19 +137,21 @@\n                     height=None,\n                 ),\n             )\n-            for first, line in loop_first(renderable_lines):\n-                if prefix:\n-                    yield from _Segment.apply_style(\n-                        prefix,\n-                        style.background_style,\n-                        post_style=remove_guide_styles,\n-                    )\n-                yield from line\n-                yield new_line\n-                if first and prefix:\n-                    prefix[-1] = make_guide(\n-                        SPACE if last else CONTINUE, prefix[-1].style or null_style\n-                    )\n+\n+            if not (depth == 0 and self.hide_root):\n+                for first, line in loop_first(renderable_lines):\n+                    if prefix:\n+                        yield from _Segment.apply_style(\n+                            prefix,\n+                            style.background_style,\n+                            post_style=remove_guide_styles,\n+                        )\n+                    yield from line\n+                    yield new_line\n+                    if first and prefix:\n+                        prefix[-1] = make_guide(\n+                            SPACE if last else CONTINUE, prefix[-1].style or null_style\n+                        )\n \n             if node.expanded and node.children:\n                 levels[-1] = make_guide(\n@@ -157,6 +163,7 @@\n                 style_stack.push(get_style(node.style))\n                 guide_style_stack.push(get_style(node.guide_style))\n                 push(iter(loop_last(node.children)))\n+                depth += 1\n \n     def __rich_measure__(\n         self, console: \"Console\", options: \"ConsoleOptions\"\n@@ -222,7 +229,7 @@\n \"\"\"\n     )\n \n-    root = Tree(\"\ud83c\udf32 [b green]Rich Tree\", highlight=True)\n+    root = Tree(\"\ud83c\udf32 [b green]Rich Tree\", highlight=True, hide_root=True)\n \n     node = root.add(\":file_folder: Renderables\", guide_style=\"red\")\n     simple_node = node.add(\":file_folder: [bold yellow]Atomic\", guide_style=\"uu green\")\n", "issue": "Tree: Flag to hide root from display \nIn certain situations it would be useful to hide the root of a tree, either to save on screen space or for clarity. A `hide_root` flag could allow this behavior.  For example: \r\n```\r\n>>> tree = Tree(\"Dir to print\")\r\n>>> tree.add(\"dir1\").add(\"file1\")\r\n>>> tree.add(\"file2\")\r\n>>> print(tree)\r\n\r\nDir to print\r\n\u251c\u2500\u2500 dir1                                                                                     \r\n\u2502   \u2514\u2500\u2500 file1                                                                                \r\n\u2514\u2500\u2500 file2                                                                                    \r\n```\r\ncould instead be display as\r\n```\r\n>>> tree = Tree(\"Dir to print\", hide_root=True)\r\n>>> tree.add(\"dir1\").add(\"file1\")\r\n>>> tree.add(\"file2\")\r\n>>> print(tree)\r\n\r\ndir1                                                                                     \r\n\u2514\u2500\u2500 file1                                                                                \r\nfile2                                                                                    \r\n```\r\n\r\nThanks!\n", "before_files": [{"content": "from typing import Iterator, List, Optional, Tuple\n\nfrom ._loop import loop_first, loop_last\nfrom .console import Console, ConsoleOptions, RenderableType, RenderResult\nfrom .jupyter import JupyterMixin\nfrom .measure import Measurement\nfrom .segment import Segment\nfrom .style import Style, StyleStack, StyleType\nfrom .styled import Styled\n\n\nclass Tree(JupyterMixin):\n    \"\"\"A renderable for a tree structure.\n\n    Args:\n        label (RenderableType): The renderable or str for the tree label.\n        style (StyleType, optional): Style of this tree. Defaults to \"tree\".\n        guide_style (StyleType, optional): Style of the guide lines. Defaults to \"tree.line\".\n        expanded (bool, optional): Also display children. Defaults to True.\n        highlight (bool, optional): Highlight renderable (if str). Defaults to False.\n    \"\"\"\n\n    def __init__(\n        self,\n        label: RenderableType,\n        *,\n        style: StyleType = \"tree\",\n        guide_style: StyleType = \"tree.line\",\n        expanded: bool = True,\n        highlight: bool = False,\n    ) -> None:\n        self.label = label\n        self.style = style\n        self.guide_style = guide_style\n        self.children: List[Tree] = []\n        self.expanded = expanded\n        self.highlight = highlight\n\n    def add(\n        self,\n        label: RenderableType,\n        *,\n        style: Optional[StyleType] = None,\n        guide_style: Optional[StyleType] = None,\n        expanded: bool = True,\n        highlight: bool = False,\n    ) -> \"Tree\":\n        \"\"\"Add a child tree.\n\n        Args:\n            label (RenderableType): The renderable or str for the tree label.\n            style (StyleType, optional): Style of this tree. Defaults to \"tree\".\n            guide_style (StyleType, optional): Style of the guide lines. Defaults to \"tree.line\".\n            expanded (bool, optional): Also display children. Defaults to True.\n            highlight (Optional[bool], optional): Highlight renderable (if str). Defaults to False.\n\n        Returns:\n            Tree: A new child Tree, which may be further modified.\n        \"\"\"\n        node = Tree(\n            label,\n            style=self.style if style is None else style,\n            guide_style=self.guide_style if guide_style is None else guide_style,\n            expanded=expanded,\n            highlight=self.highlight if highlight is None else highlight,\n        )\n        self.children.append(node)\n        return node\n\n    def __rich_console__(\n        self, console: \"Console\", options: \"ConsoleOptions\"\n    ) -> \"RenderResult\":\n\n        stack: List[Iterator[Tuple[bool, Tree]]] = []\n        pop = stack.pop\n        push = stack.append\n        new_line = Segment.line()\n\n        get_style = console.get_style\n        null_style = Style.null()\n        guide_style = get_style(self.guide_style, default=\"\") or null_style\n        SPACE, CONTINUE, FORK, END = range(4)\n\n        ASCII_GUIDES = (\"    \", \"|   \", \"+-- \", \"`-- \")\n        TREE_GUIDES = [\n            (\"    \", \"\u2502   \", \"\u251c\u2500\u2500 \", \"\u2514\u2500\u2500 \"),\n            (\"    \", \"\u2503   \", \"\u2523\u2501\u2501 \", \"\u2517\u2501\u2501 \"),\n            (\"    \", \"\u2551   \", \"\u2560\u2550\u2550 \", \"\u255a\u2550\u2550 \"),\n        ]\n        _Segment = Segment\n\n        def make_guide(index: int, style: Style) -> Segment:\n            \"\"\"Make a Segment for a level of the guide lines.\"\"\"\n            if options.ascii_only:\n                line = ASCII_GUIDES[index]\n            else:\n                guide = 1 if style.bold else (2 if style.underline2 else 0)\n                line = TREE_GUIDES[0 if options.legacy_windows else guide][index]\n            return _Segment(line, style)\n\n        levels: List[Segment] = [make_guide(CONTINUE, guide_style)]\n        push(iter(loop_last([self])))\n\n        guide_style_stack = StyleStack(get_style(self.guide_style))\n        style_stack = StyleStack(get_style(self.style))\n        remove_guide_styles = Style(bold=False, underline2=False)\n\n        while stack:\n            stack_node = pop()\n            try:\n                last, node = next(stack_node)\n            except StopIteration:\n                levels.pop()\n                if levels:\n                    guide_style = levels[-1].style or null_style\n                    levels[-1] = make_guide(FORK, guide_style)\n                    guide_style_stack.pop()\n                    style_stack.pop()\n                continue\n            push(stack_node)\n            if last:\n                levels[-1] = make_guide(END, levels[-1].style or null_style)\n\n            guide_style = guide_style_stack.current + get_style(node.guide_style)\n            style = style_stack.current + get_style(node.style)\n            prefix = levels[1:]\n            renderable_lines = console.render_lines(\n                Styled(node.label, style),\n                options.update(\n                    width=options.max_width\n                    - sum(level.cell_length for level in prefix),\n                    highlight=self.highlight,\n                    height=None,\n                ),\n            )\n            for first, line in loop_first(renderable_lines):\n                if prefix:\n                    yield from _Segment.apply_style(\n                        prefix,\n                        style.background_style,\n                        post_style=remove_guide_styles,\n                    )\n                yield from line\n                yield new_line\n                if first and prefix:\n                    prefix[-1] = make_guide(\n                        SPACE if last else CONTINUE, prefix[-1].style or null_style\n                    )\n\n            if node.expanded and node.children:\n                levels[-1] = make_guide(\n                    SPACE if last else CONTINUE, levels[-1].style or null_style\n                )\n                levels.append(\n                    make_guide(END if len(node.children) == 1 else FORK, guide_style)\n                )\n                style_stack.push(get_style(node.style))\n                guide_style_stack.push(get_style(node.guide_style))\n                push(iter(loop_last(node.children)))\n\n    def __rich_measure__(\n        self, console: \"Console\", options: \"ConsoleOptions\"\n    ) -> \"Measurement\":\n        stack: List[Iterator[Tree]] = [iter([self])]\n        pop = stack.pop\n        push = stack.append\n        minimum = 0\n        maximum = 0\n        measure = Measurement.get\n        level = 0\n        while stack:\n            iter_tree = pop()\n            try:\n                tree = next(iter_tree)\n            except StopIteration:\n                level -= 1\n                continue\n            push(iter_tree)\n            min_measure, max_measure = measure(console, options, tree.label)\n            indent = level * 4\n            minimum = max(min_measure + indent, minimum)\n            maximum = max(max_measure + indent, maximum)\n            if tree.expanded and tree.children:\n                push(iter(tree.children))\n                level += 1\n        return Measurement(minimum, maximum)\n\n\nif __name__ == \"__main__\":  # pragma: no cover\n\n    from rich.console import Group\n    from rich.markdown import Markdown\n    from rich.panel import Panel\n    from rich.syntax import Syntax\n    from rich.table import Table\n\n    table = Table(row_styles=[\"\", \"dim\"])\n\n    table.add_column(\"Released\", style=\"cyan\", no_wrap=True)\n    table.add_column(\"Title\", style=\"magenta\")\n    table.add_column(\"Box Office\", justify=\"right\", style=\"green\")\n\n    table.add_row(\"Dec 20, 2019\", \"Star Wars: The Rise of Skywalker\", \"$952,110,690\")\n    table.add_row(\"May 25, 2018\", \"Solo: A Star Wars Story\", \"$393,151,347\")\n    table.add_row(\"Dec 15, 2017\", \"Star Wars Ep. V111: The Last Jedi\", \"$1,332,539,889\")\n    table.add_row(\"Dec 16, 2016\", \"Rogue One: A Star Wars Story\", \"$1,332,439,889\")\n\n    code = \"\"\"\\\nclass Segment(NamedTuple):\n    text: str = \"\"    \n    style: Optional[Style] = None    \n    is_control: bool = False    \n\"\"\"\n    syntax = Syntax(code, \"python\", theme=\"monokai\", line_numbers=True)\n\n    markdown = Markdown(\n        \"\"\"\\\n### example.md\n> Hello, World!\n> \n> Markdown _all_ the things\n\"\"\"\n    )\n\n    root = Tree(\"\ud83c\udf32 [b green]Rich Tree\", highlight=True)\n\n    node = root.add(\":file_folder: Renderables\", guide_style=\"red\")\n    simple_node = node.add(\":file_folder: [bold yellow]Atomic\", guide_style=\"uu green\")\n    simple_node.add(Group(\"\ud83d\udcc4 Syntax\", syntax))\n    simple_node.add(Group(\"\ud83d\udcc4 Markdown\", Panel(markdown, border_style=\"green\")))\n\n    containers_node = node.add(\n        \":file_folder: [bold magenta]Containers\", guide_style=\"bold magenta\"\n    )\n    containers_node.expanded = True\n    panel = Panel.fit(\"Just a panel\", border_style=\"red\")\n    containers_node.add(Group(\"\ud83d\udcc4 Panels\", panel))\n\n    containers_node.add(Group(\"\ud83d\udcc4 [b magenta]Table\", table))\n\n    console = Console()\n    console.print(root)\n", "path": "rich/tree.py"}], "after_files": [{"content": "from typing import Iterator, List, Optional, Tuple\n\nfrom ._loop import loop_first, loop_last\nfrom .console import Console, ConsoleOptions, RenderableType, RenderResult\nfrom .jupyter import JupyterMixin\nfrom .measure import Measurement\nfrom .segment import Segment\nfrom .style import Style, StyleStack, StyleType\nfrom .styled import Styled\n\n\nclass Tree(JupyterMixin):\n    \"\"\"A renderable for a tree structure.\n\n    Args:\n        label (RenderableType): The renderable or str for the tree label.\n        style (StyleType, optional): Style of this tree. Defaults to \"tree\".\n        guide_style (StyleType, optional): Style of the guide lines. Defaults to \"tree.line\".\n        expanded (bool, optional): Also display children. Defaults to True.\n        highlight (bool, optional): Highlight renderable (if str). Defaults to False.\n    \"\"\"\n\n    def __init__(\n        self,\n        label: RenderableType,\n        *,\n        style: StyleType = \"tree\",\n        guide_style: StyleType = \"tree.line\",\n        expanded: bool = True,\n        highlight: bool = False,\n        hide_root: bool = False,\n    ) -> None:\n        self.label = label\n        self.style = style\n        self.guide_style = guide_style\n        self.children: List[Tree] = []\n        self.expanded = expanded\n        self.highlight = highlight\n        self.hide_root = hide_root\n\n    def add(\n        self,\n        label: RenderableType,\n        *,\n        style: Optional[StyleType] = None,\n        guide_style: Optional[StyleType] = None,\n        expanded: bool = True,\n        highlight: bool = False,\n    ) -> \"Tree\":\n        \"\"\"Add a child tree.\n\n        Args:\n            label (RenderableType): The renderable or str for the tree label.\n            style (StyleType, optional): Style of this tree. Defaults to \"tree\".\n            guide_style (StyleType, optional): Style of the guide lines. Defaults to \"tree.line\".\n            expanded (bool, optional): Also display children. Defaults to True.\n            highlight (Optional[bool], optional): Highlight renderable (if str). Defaults to False.\n\n        Returns:\n            Tree: A new child Tree, which may be further modified.\n        \"\"\"\n        node = Tree(\n            label,\n            style=self.style if style is None else style,\n            guide_style=self.guide_style if guide_style is None else guide_style,\n            expanded=expanded,\n            highlight=self.highlight if highlight is None else highlight,\n        )\n        self.children.append(node)\n        return node\n\n    def __rich_console__(\n        self, console: \"Console\", options: \"ConsoleOptions\"\n    ) -> \"RenderResult\":\n\n        stack: List[Iterator[Tuple[bool, Tree]]] = []\n        pop = stack.pop\n        push = stack.append\n        new_line = Segment.line()\n\n        get_style = console.get_style\n        null_style = Style.null()\n        guide_style = get_style(self.guide_style, default=\"\") or null_style\n        SPACE, CONTINUE, FORK, END = range(4)\n\n        ASCII_GUIDES = (\"    \", \"|   \", \"+-- \", \"`-- \")\n        TREE_GUIDES = [\n            (\"    \", \"\u2502   \", \"\u251c\u2500\u2500 \", \"\u2514\u2500\u2500 \"),\n            (\"    \", \"\u2503   \", \"\u2523\u2501\u2501 \", \"\u2517\u2501\u2501 \"),\n            (\"    \", \"\u2551   \", \"\u2560\u2550\u2550 \", \"\u255a\u2550\u2550 \"),\n        ]\n        _Segment = Segment\n\n        def make_guide(index: int, style: Style) -> Segment:\n            \"\"\"Make a Segment for a level of the guide lines.\"\"\"\n            if options.ascii_only:\n                line = ASCII_GUIDES[index]\n            else:\n                guide = 1 if style.bold else (2 if style.underline2 else 0)\n                line = TREE_GUIDES[0 if options.legacy_windows else guide][index]\n            return _Segment(line, style)\n\n        levels: List[Segment] = [make_guide(CONTINUE, guide_style)]\n        push(iter(loop_last([self])))\n\n        guide_style_stack = StyleStack(get_style(self.guide_style))\n        style_stack = StyleStack(get_style(self.style))\n        remove_guide_styles = Style(bold=False, underline2=False)\n\n        depth = 0\n\n        while stack:\n            stack_node = pop()\n            try:\n                last, node = next(stack_node)\n            except StopIteration:\n                levels.pop()\n                if levels:\n                    guide_style = levels[-1].style or null_style\n                    levels[-1] = make_guide(FORK, guide_style)\n                    guide_style_stack.pop()\n                    style_stack.pop()\n                continue\n            push(stack_node)\n            if last:\n                levels[-1] = make_guide(END, levels[-1].style or null_style)\n\n            guide_style = guide_style_stack.current + get_style(node.guide_style)\n            style = style_stack.current + get_style(node.style)\n            prefix = levels[(2 if self.hide_root else 1) :]\n            renderable_lines = console.render_lines(\n                Styled(node.label, style),\n                options.update(\n                    width=options.max_width\n                    - sum(level.cell_length for level in prefix),\n                    highlight=self.highlight,\n                    height=None,\n                ),\n            )\n\n            if not (depth == 0 and self.hide_root):\n                for first, line in loop_first(renderable_lines):\n                    if prefix:\n                        yield from _Segment.apply_style(\n                            prefix,\n                            style.background_style,\n                            post_style=remove_guide_styles,\n                        )\n                    yield from line\n                    yield new_line\n                    if first and prefix:\n                        prefix[-1] = make_guide(\n                            SPACE if last else CONTINUE, prefix[-1].style or null_style\n                        )\n\n            if node.expanded and node.children:\n                levels[-1] = make_guide(\n                    SPACE if last else CONTINUE, levels[-1].style or null_style\n                )\n                levels.append(\n                    make_guide(END if len(node.children) == 1 else FORK, guide_style)\n                )\n                style_stack.push(get_style(node.style))\n                guide_style_stack.push(get_style(node.guide_style))\n                push(iter(loop_last(node.children)))\n                depth += 1\n\n    def __rich_measure__(\n        self, console: \"Console\", options: \"ConsoleOptions\"\n    ) -> \"Measurement\":\n        stack: List[Iterator[Tree]] = [iter([self])]\n        pop = stack.pop\n        push = stack.append\n        minimum = 0\n        maximum = 0\n        measure = Measurement.get\n        level = 0\n        while stack:\n            iter_tree = pop()\n            try:\n                tree = next(iter_tree)\n            except StopIteration:\n                level -= 1\n                continue\n            push(iter_tree)\n            min_measure, max_measure = measure(console, options, tree.label)\n            indent = level * 4\n            minimum = max(min_measure + indent, minimum)\n            maximum = max(max_measure + indent, maximum)\n            if tree.expanded and tree.children:\n                push(iter(tree.children))\n                level += 1\n        return Measurement(minimum, maximum)\n\n\nif __name__ == \"__main__\":  # pragma: no cover\n\n    from rich.console import Group\n    from rich.markdown import Markdown\n    from rich.panel import Panel\n    from rich.syntax import Syntax\n    from rich.table import Table\n\n    table = Table(row_styles=[\"\", \"dim\"])\n\n    table.add_column(\"Released\", style=\"cyan\", no_wrap=True)\n    table.add_column(\"Title\", style=\"magenta\")\n    table.add_column(\"Box Office\", justify=\"right\", style=\"green\")\n\n    table.add_row(\"Dec 20, 2019\", \"Star Wars: The Rise of Skywalker\", \"$952,110,690\")\n    table.add_row(\"May 25, 2018\", \"Solo: A Star Wars Story\", \"$393,151,347\")\n    table.add_row(\"Dec 15, 2017\", \"Star Wars Ep. V111: The Last Jedi\", \"$1,332,539,889\")\n    table.add_row(\"Dec 16, 2016\", \"Rogue One: A Star Wars Story\", \"$1,332,439,889\")\n\n    code = \"\"\"\\\nclass Segment(NamedTuple):\n    text: str = \"\"    \n    style: Optional[Style] = None    \n    is_control: bool = False    \n\"\"\"\n    syntax = Syntax(code, \"python\", theme=\"monokai\", line_numbers=True)\n\n    markdown = Markdown(\n        \"\"\"\\\n### example.md\n> Hello, World!\n> \n> Markdown _all_ the things\n\"\"\"\n    )\n\n    root = Tree(\"\ud83c\udf32 [b green]Rich Tree\", highlight=True, hide_root=True)\n\n    node = root.add(\":file_folder: Renderables\", guide_style=\"red\")\n    simple_node = node.add(\":file_folder: [bold yellow]Atomic\", guide_style=\"uu green\")\n    simple_node.add(Group(\"\ud83d\udcc4 Syntax\", syntax))\n    simple_node.add(Group(\"\ud83d\udcc4 Markdown\", Panel(markdown, border_style=\"green\")))\n\n    containers_node = node.add(\n        \":file_folder: [bold magenta]Containers\", guide_style=\"bold magenta\"\n    )\n    containers_node.expanded = True\n    panel = Panel.fit(\"Just a panel\", border_style=\"red\")\n    containers_node.add(Group(\"\ud83d\udcc4 Panels\", panel))\n\n    containers_node.add(Group(\"\ud83d\udcc4 [b magenta]Table\", table))\n\n    console = Console()\n    console.print(root)\n", "path": "rich/tree.py"}]}