-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspit.py
80 lines (64 loc) · 2.53 KB
/
spit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python3
import os
import argparse
# Default ignored directories and files
default_ignored = {
"node_modules",
".git",
".yarn",
"Pods",
"Build",
".vscode",
".gradle",
"DerivedData"
".Trash",
".DS_Store",
}
def print_directory_structure(path, depth=None, ignored=None, verbose=False, show_hidden=False, indent="", last=True):
"""Recursively prints the directory structure."""
# Adds a prefix to the current file or directory
prefix = "└─ " if last else "├─ "
# Adds padding to the current file or directory
padding = " " if last else "│ "
# If depth is zero, we won't proceed
if depth == 0:
return
# Check if the directory or file is in the ignored list
if not verbose and os.path.basename(path) in ignored:
return
# Print current file or directory
print(indent + prefix + os.path.basename(path))
# If path is a directory, print its contents
if os.path.isdir(path):
items = sorted([item for item in os.listdir(path) if (
show_hidden or not item.startswith('.')) and item not in ignored])
for index, item in enumerate(items):
next_depth = depth - 1 if depth is not None else None
print_directory_structure(
os.path.join(path, item),
next_depth,
ignored,
verbose,
show_hidden,
indent + padding,
index == len(items) - 1
)
def main():
# Set up argument parsing
parser = argparse.ArgumentParser(description="Print directory structure.")
parser.add_argument('-depth', '-d', type=int, default=None,
help="Max depth to traverse. Default is infinite depth.")
parser.add_argument('-verbose', '-v', action='store_true',
help="Verbose mode. Ignores all default ignored directories and files.")
parser.add_argument('-ignore', '-i', nargs='*', default=[],
help="Names of additional directories or files to ignore.")
parser.add_argument('-a', action='store_true', help="Show hidden files.")
# Parse arguments
args = parser.parse_args()
# Add user-specified ignored items to the default set
ignored = default_ignored.union(args.ignore)
# Call the function with the specified depth, hidden and ignored set
print_directory_structure(os.getcwd(), args.depth,
ignored, args.verbose, args.a)
if __name__ == "__main__":
main()