-
Notifications
You must be signed in to change notification settings - Fork 151
/
treemacs-dirs-to-collapse.py
85 lines (78 loc) · 2.55 KB
/
treemacs-dirs-to-collapse.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
81
82
83
84
85
from os import listdir
from os.path import isdir
from posixpath import join
import sys
import os
ROOT = sys.argv[1]
LIMIT = int(sys.argv[2])
SHOW_ALL = sys.argv[3] == 't'
if ROOT.startswith("/ssh:") or ROOT.startswith("/scp:"):
print("()")
sys.exit(0)
# special workaround for windows platforms
# the default `join' implementation cannot quite deal with windows
# paths in the form of "C:/A/B" & "C:/A/B/C", joining them as
# "C:/A/B/C:/A/B/C"
# it can, however, be "tricked" into doing the right thing by adding
# a slash to the start of the paths
# go figure
if sys.platform == 'win32':
def join_dirs(d1, d2, full_path=False):
missing_slash = False
if not d1.startswith("/"):
missing_slash = True
d1 = "/" + d1
# full_path is only True when the second argument is
# another absolute path
if full_path and not d2.startswith("/"):
missing_slash = True
d2 = "/" + d2
joined = join(d1, d2)
if missing_slash:
# still need to return the joined path without the
# leading slash, the way it looked originally
return joined[1:]
else:
return joined
else:
def join_dirs(d1, d2, _):
return join(d1, d2)
if LIMIT <= 0:
exit(0)
def dir_content(path):
"""
returns the content of given path, excluding unreadable files
and dotfiles (unless SHOW_ALL is True)
"""
ret = []
for item in listdir(path):
full_path = join_dirs(path, item)
if os.access(full_path, os.R_OK) and (SHOW_ALL or item[0] != '.'):
ret.append(full_path)
return ret
def main():
out = sys.stdout
dirs = [d for d in dir_content(ROOT) if isdir(d)]
out.write("(")
for current_dir in dirs:
content = dir_content(current_dir)
collapsed = current_dir
steps = []
depth = 0
while True:
if len(content) == 1 and isdir(content[0]):
single_path = content[0]
collapsed = join_dirs(collapsed, single_path, True)
content = dir_content(collapsed)
depth += 1
steps.append(single_path)
if depth >= LIMIT:
break
else:
break
if depth > 0:
final_dir = steps[-1]
display_suffix = final_dir[len(current_dir):]
out.write("(" + '"' + current_dir + '" ' + '"' + display_suffix + '" ' + '"' + '" "'.join(steps) + '")')
out.write(")")
main()