2021-01-03 17:14:59 +00:00
|
|
|
import os
|
2021-01-05 16:31:23 +00:00
|
|
|
from operator import attrgetter
|
|
|
|
import pathlib
|
2021-01-03 17:14:59 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from rich import print
|
2021-01-05 16:31:23 +00:00
|
|
|
from rich.filesize import decimal
|
|
|
|
from rich.markup import escape
|
2021-01-03 17:14:59 +00:00
|
|
|
from rich.text import Text
|
|
|
|
from rich.tree import Tree
|
|
|
|
|
|
|
|
|
|
|
|
def walk_directory(directory, tree):
|
2021-01-05 16:31:23 +00:00
|
|
|
paths = sorted(
|
|
|
|
pathlib.Path(directory).iterdir(),
|
|
|
|
key=lambda path: (path.name.lower(), path.is_file()),
|
|
|
|
)
|
|
|
|
for path in paths:
|
|
|
|
if path.name.startswith("."):
|
|
|
|
continue
|
|
|
|
if path.is_dir():
|
|
|
|
style = "dim not bold" if path.name.startswith("__") else ""
|
|
|
|
branch = tree.add(
|
|
|
|
f"[bold magenta]:open_file_folder: {escape(path.name)}",
|
|
|
|
style=style,
|
|
|
|
guide_style=style,
|
|
|
|
)
|
2021-01-03 17:14:59 +00:00
|
|
|
walk_directory(path, branch)
|
|
|
|
else:
|
2021-01-05 16:31:23 +00:00
|
|
|
text_filename = Text(path.name, "green")
|
|
|
|
text_filename.highlight_regex(r"\..*$", "bold red")
|
|
|
|
file_size = path.stat().st_size
|
|
|
|
text_filename.append(f" ({decimal(file_size)})", "blue")
|
|
|
|
tree.add(Text("🐍 " if path.suffix == ".py" else "📄 ") + text_filename)
|
2021-01-03 17:14:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
directory = os.path.abspath(sys.argv[1])
|
|
|
|
except IndexError:
|
|
|
|
print("[b]Usage:[/] python tree.py <DIRECTORY>")
|
|
|
|
else:
|
2021-01-05 16:31:23 +00:00
|
|
|
tree = Tree(directory, guide_style="bold cyan")
|
2021-01-03 17:14:59 +00:00
|
|
|
walk_directory(directory, tree)
|
|
|
|
print(tree)
|