Exemple #1
0
def walk_directory(directory: pathlib.Path, tree: Tree) -> None:
    """Recursively build a Tree with directory contents."""
    # Sort dirs first then by filename
    paths = sorted(
        pathlib.Path(directory).iterdir(),
        key=lambda path: (path.is_file(), path.name.lower()),
    )
    for path in paths:
        # Remove hidden files
        if path.name.startswith("."):
            continue
        if path.is_dir():
            style = "dim" if path.name.startswith("__") else ""
            branch = tree.add(
                f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}",
                style=style,
                guide_style=style,
            )
            walk_directory(path, branch)
        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")
            icon = "🐍 " if path.suffix == ".py" else "📄 "
            tree.add(Text(icon) + text_filename)
Exemple #2
0
def test_render_single_branch():
    tree = Tree("foo")
    tree.add("bar")
    console = Console(color_system=None, width=20)
    console.begin_capture()
    console.print(tree)
    result = console.end_capture()
    print(repr(result))
    expected = "foo                 \n└── bar             \n"
    assert result == expected
Exemple #3
0
def test_render_ascii():
    tree = Tree("foo")
    tree.add("bar")
    tree.add("baz")

    class AsciiConsole(Console):
        @property
        def encoding(self):
            return "ascii"

    console = AsciiConsole(color_system=None, width=20)
    console.begin_capture()
    console.print(tree)
    result = console.end_capture()
    expected = "foo                 \n+-- bar             \n`-- baz             \n"
    assert result == expected
Exemple #4
0
def test_tree_measure():
    tree = Tree("foo")
    tree.add("bar")
    tree.add("musroom risotto")
    console = Console()
    measurement = Measurement.get(console, console.options, tree)
    assert measurement == Measurement(11, 19)
Exemple #5
0
def test_render():
    tree = Tree("foo")
    tree.add("bar", style="italic")
    baz_tree = tree.add("baz", guide_style="bold red", style="on blue")
    baz_tree.add("1")
    baz_tree.add("2")
    tree.add("egg")

    console = Console(width=20, force_terminal=True, color_system="standard")
    console.begin_capture()
    console.print(tree)
    result = console.end_capture()
    print(repr(result))
    expected = "foo                 \n├── \x1b[3mbar\x1b[0m\x1b[3m             \x1b[0m\n\x1b[44m├── \x1b[0m\x1b[44mbaz\x1b[0m\x1b[44m             \x1b[0m\n\x1b[44m│   \x1b[0m\x1b[31;44m├── \x1b[0m\x1b[44m1\x1b[0m\x1b[44m           \x1b[0m\n\x1b[44m│   \x1b[0m\x1b[31;44m└── \x1b[0m\x1b[44m2\x1b[0m\x1b[44m           \x1b[0m\n└── egg             \n"
    assert result == expected
Exemple #6
0
    def tree(self) -> "Tree":
        """Get a tree renderable to show layout structure."""
        from mudrich.styled import Styled
        from mudrich.table import Table
        from mudrich.tree import Tree

        def summary(layout: "Layout") -> Table:

            icon = layout.splitter.get_tree_icon()

            table = Table.grid(padding=(0, 1, 0, 0))

            text: RenderableType = (Pretty(layout) if layout.visible else
                                    Styled(Pretty(layout), "dim"))
            table.add_row(icon, text)
            _summary = table
            return _summary

        layout = self
        tree = Tree(
            summary(layout),
            guide_style=f"layout.tree.{layout.splitter.name}",
            highlight=True,
        )

        def recurse(tree: "Tree", layout: "Layout") -> None:
            for child in layout._children:
                recurse(
                    tree.add(
                        summary(child),
                        guide_style=f"layout.tree.{child.splitter.name}",
                    ),
                    child,
                )

        recurse(tree, self)
        return tree
Exemple #7
0
def test_render_single_node():
    tree = Tree("foo")
    console = Console(color_system=None, width=20)
    console.begin_capture()
    console.print(tree)
    assert console.end_capture() == "foo                 \n"
Exemple #8
0
            continue
        if path.is_dir():
            style = "dim" if path.name.startswith("__") else ""
            branch = tree.add(
                f"[bold magenta]:open_file_folder: [link file://{path}]{escape(path.name)}",
                style=style,
                guide_style=style,
            )
            walk_directory(path, branch)
        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")
            icon = "🐍 " if path.suffix == ".py" else "📄 "
            tree.add(Text(icon) + text_filename)


try:
    directory = os.path.abspath(sys.argv[1])
except IndexError:
    print("[b]Usage:[/] python tree.py <DIRECTORY>")
else:
    tree = Tree(
        f":open_file_folder: [link file://{directory}]{directory}",
        guide_style="bold bright_blue",
    )
    walk_directory(pathlib.Path(directory), tree)
    print(tree)