mirror of
https://github.com/Textualize/rich.git
synced 2025-08-04 18:18:22 +00:00
tree renderable
This commit is contained in:
parent
e02cc29477
commit
7ba81f3a6a
2 changed files with 31 additions and 14 deletions
|
@ -1,11 +1,11 @@
|
|||
import os
|
||||
from operator import attrgetter
|
||||
import pathlib
|
||||
import sys
|
||||
|
||||
from rich import print
|
||||
from rich.filesize import decimal
|
||||
from rich.markup import escape
|
||||
from rich.style import Style
|
||||
from rich.text import Text
|
||||
from rich.tree import Tree
|
||||
|
||||
|
@ -19,9 +19,9 @@ def walk_directory(directory, tree):
|
|||
if path.name.startswith("."):
|
||||
continue
|
||||
if path.is_dir():
|
||||
style = "dim not bold" if path.name.startswith("__") else ""
|
||||
style = "dim" if path.name.startswith("__") else ""
|
||||
branch = tree.add(
|
||||
f"[bold magenta]:open_file_folder: {escape(path.name)}",
|
||||
f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}",
|
||||
style=style,
|
||||
guide_style=style,
|
||||
)
|
||||
|
@ -29,6 +29,7 @@ def walk_directory(directory, tree):
|
|||
else:
|
||||
text_filename = Text(path.name, "green")
|
||||
text_filename.highlight_regex(r"\..*$", "bold red")
|
||||
text_filename.stylize(f"link file://{ path }")
|
||||
file_size = path.stat().st_size
|
||||
text_filename.append(f" ({decimal(file_size)})", "blue")
|
||||
tree.add(Text("🐍 " if path.suffix == ".py" else "📄 ") + text_filename)
|
||||
|
@ -39,6 +40,9 @@ try:
|
|||
except IndexError:
|
||||
print("[b]Usage:[/] python tree.py <DIRECTORY>")
|
||||
else:
|
||||
tree = Tree(directory, guide_style="bold cyan")
|
||||
tree = Tree(
|
||||
f":open_file_folder: [link file://{directory}]{directory}",
|
||||
guide_style="bold bright_blue",
|
||||
)
|
||||
walk_directory(directory, tree)
|
||||
print(tree)
|
||||
print(tree)
|
||||
|
|
31
rich/tree.py
31
rich/tree.py
|
@ -1,13 +1,14 @@
|
|||
from typing import Iterable, List, NamedTuple, Tuple
|
||||
|
||||
from .console import Console, ConsoleOptions, RenderResult, RenderableType
|
||||
from .jupyter import JupyterMixin
|
||||
from ._loop import loop_first, loop_last
|
||||
from .segment import Segment
|
||||
from .style import Style, StyleStack, StyleType
|
||||
from .styled import Styled
|
||||
|
||||
|
||||
class Tree:
|
||||
class Tree(JupyterMixin):
|
||||
def __init__(
|
||||
self,
|
||||
renderable: RenderableType,
|
||||
|
@ -15,6 +16,15 @@ class Tree:
|
|||
guide_style: StyleType = "tree.line",
|
||||
expanded=True,
|
||||
) -> None:
|
||||
"""A renderable for a tree structure.
|
||||
|
||||
Args:
|
||||
renderable (RenderableType): The renderable or text for the root node.
|
||||
style (StyleType, optional): Style of this tree. Defaults to "tree".
|
||||
guide_style (StyleType, optional): Style of the guide lines. Defaults to "tree.line".
|
||||
expanded (bool, optional): Also display children. Defaults to True.
|
||||
"""
|
||||
|
||||
self.renderable = renderable
|
||||
self.style = style
|
||||
self.guide_style = guide_style
|
||||
|
@ -66,7 +76,7 @@ class Tree:
|
|||
line = TREE_GUIDES[guide][index]
|
||||
return _Segment(line, _style)
|
||||
|
||||
levels: List[Segment] = [make_guide(SPACE)]
|
||||
levels: List[Segment] = [make_guide(CONTINUE)]
|
||||
push(loop_last([self]))
|
||||
|
||||
guide_style_stack = StyleStack(get_style(self.guide_style))
|
||||
|
@ -101,11 +111,14 @@ class Tree:
|
|||
|
||||
prefix = levels[1:]
|
||||
for first, line in loop_first(renderable_lines):
|
||||
yield from _Segment.apply_style(prefix, style.background_style)
|
||||
if prefix:
|
||||
yield from _Segment.apply_style(prefix, style.background_style)
|
||||
yield from line
|
||||
yield new_line
|
||||
if first:
|
||||
prefix = levels[1:-1] + [make_guide(SPACE if last else CONTINUE)]
|
||||
if first and prefix:
|
||||
prefix[-1] = make_guide(
|
||||
SPACE if last else CONTINUE, prefix[-1].style
|
||||
)
|
||||
|
||||
if node.expanded and node.children:
|
||||
levels[-1] = make_guide(SPACE if last else CONTINUE, levels[-1].style)
|
||||
|
@ -170,10 +183,10 @@ class Segment(NamedTuple):
|
|||
"""
|
||||
)
|
||||
|
||||
root = Tree(":open_file_folder: The Root node")
|
||||
root = Tree(":open_file_folder: The Root node\n", guide_style="red")
|
||||
|
||||
node = root.add(":file_folder: Renderables")
|
||||
simple_node = node.add(":file_folder: [bold red]Atomic", guide_style="uu green")
|
||||
node = root.add(":file_folder: Renderables\n")
|
||||
simple_node = node.add(":file_folder: [bold red]Atomic\n", guide_style="uu green")
|
||||
simple_node.add(RenderGroup("📄 Syntax", syntax))
|
||||
simple_node.add(RenderGroup("📄 Markdown", markdown))
|
||||
|
||||
|
@ -182,7 +195,7 @@ class Segment(NamedTuple):
|
|||
)
|
||||
containers_node.expanded = True
|
||||
panel = Panel.fit("Just a panel", border_style="red")
|
||||
containers_node.add(RenderGroup("📄 Panels", panel))
|
||||
containers_node.add(RenderGroup("📄 Panels\n", panel))
|
||||
|
||||
containers_node.add(RenderGroup("📄 [b magenta]Table", table))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue