#!/usr/bin/env python3

import os
import sys

import gitignore_parser


paths = sys.argv[1:] or ["."]


def process_path(path, prefix="", gitignores=[]):
    children = sorted(os.listdir(path))

    if ".gitignore" in children:
        gitignores = gitignores + [gitignore_parser.parse_gitignore(os.path.join(path, ".gitignore"))]

    for i, name in enumerate(children):
        sub_path = os.path.join(path, name)
        if name != ".git" and all((not gitignore(sub_path) for gitignore in gitignores)):
            sticks = "├──" if i != len(children) - 1 else "└──"
            print(f"{prefix}{sticks}", name)

            if os.path.isdir(sub_path):
                sticks = "│   " if i != len(children) - 1 else "    "
                process_path(os.path.join(path, name), prefix=f"{prefix}{sticks}", gitignores=gitignores)
                statistics["directories"] += 1

            else:
                statistics["files"] += 1


statistics = {"directories": 0, "files": 0}
for path in paths:
    print(path)
    process_path(path)

print()
print(", ".join((f"{v} {k}" for k, v in statistics.items())))
