-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdexit.py
More file actions
205 lines (181 loc) · 5.85 KB
/
Copy pathdexit.py
File metadata and controls
205 lines (181 loc) · 5.85 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# genindex.py
# @spiralbend 2025-07-23
import os
import stat
import argparse
import datetime
from pathlib import Path
TEMPLATE = """<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Index of /{path}</title>
<style>
body {{
background-color: black;
color: lime;
font-family: 'Inconsolata', 'Courier New', monospace;
white-space: pre;
}}
.content {{
margin-left: 1cm;
}}
a {{
color: lime;
text-decoration: none;
}}
a:hover {{
text-decoration: underline;
}}
.cursor {{
display: inline-block;
width: 10px;
animation: blink 1s steps(1) infinite;
}}
@keyframes blink {{
50% {{ background-color: lime; }}
100% {{ background-color: transparent; }}
}}
.cursor {{
animation: blink 1s steps(2, start) infinite;
opacity: 1;
}}
@keyframes blink {{
to {{ opacity: 0; }}
}}
.prompt {{
color:orange
}}
</style>
</head>
<body>
<!-- ::spiralbend:: autogenerated index -->
Index of /{path}
total {total_size}
{entries}
<span class="prompt">𝜟·</span> <span class="cursor"><b>_</b></span>
</body>
</html>
"""
TOKEN = "::spiralbend:: autogenerated index"
def human_readable_size(size):
for unit in ["B", "K", "M", "G", "T"]:
if size < 1024.0:
return f"{size:>4.1f}{unit}" if unit != "B" else f"{int(size):>4d}{unit}"
size /= 1024.0
return f"{size:.1f}P"
def get_mode_str(mode):
is_dir = "d" if stat.S_ISDIR(mode) else "-"
perms = "".join(
(
["r", "w", "x"][(mode >> i) & 1]
if i % 3 != 2
else (["x", "x", "x"][((mode >> i) & 1)] if (mode >> (i - 1)) & 1 else "-")
)
for i in reversed(range(9))
)
return f"{is_dir}{perms}."
def classify_suffix(st_mode, is_dir, full_path):
if is_dir:
return "/"
elif stat.S_ISLNK(st_mode):
return "@"
elif stat.S_ISFIFO(st_mode):
return "|"
elif stat.S_ISSOCK(st_mode):
return "="
elif os.access(full_path, os.X_OK):
return "*"
else:
return ""
def format_entry(mode, nlink, size, mtime, name, href, is_dir):
size_str = human_readable_size(size) if not is_dir else ""
date_str = mtime.strftime("%b %e %H:%M")
return f'{mode:11} {nlink:>2} {size_str:>6} {date_str} <a href="{href}">{name}</a>'
def build_index(dirpath, rel_dir, dirnames, filenames, include_dotfiles):
entries = []
total_size = 0
# Parent directory link
rel_parent = os.path.relpath(os.path.join(dirpath, ".."), dirpath)
entries.append(
format_entry(
"drwxr-xr-x.", 1, 0, datetime.datetime.now(), "..", rel_parent, True
)
)
all_entries = sorted(dirnames + filenames, key=str.lower)
for name in all_entries:
if not include_dotfiles and name.startswith("."):
continue
full_path = os.path.join(dirpath, name)
try:
st = os.lstat(full_path)
except FileNotFoundError:
continue
mode_str = get_mode_str(st.st_mode)
mtime = datetime.datetime.fromtimestamp(st.st_mtime)
size = st.st_size
is_dir = stat.S_ISDIR(st.st_mode)
suffix = classify_suffix(st.st_mode, is_dir, full_path)
display_name = name + suffix
href = f"{name}/" if is_dir else name
entries.append(
format_entry(mode_str, st.st_nlink, size, mtime, display_name, href, is_dir)
)
total_size += size
return TEMPLATE.format(
path=rel_dir if rel_dir != "." else "",
entries="\n".join(entries),
total_size=human_readable_size(total_size),
)
def process_dir(root, include_dotfiles=False, verbose=False):
for dirpath, dirnames, filenames in os.walk(root):
rel_dir = os.path.relpath(dirpath, root)
if (
rel_dir != "."
and not include_dotfiles
and any(part.startswith(".") for part in rel_dir.split(os.sep))
):
continue
index_path = os.path.join(dirpath, "index.html")
index_exists = os.path.exists(index_path)
index_owned = False
if index_exists:
try:
with open(index_path, "r") as f:
if TOKEN in f.read():
index_owned = True
else:
if verbose:
print(f"Skipping existing index (no token): {index_path}")
continue
except Exception as e:
if verbose:
print(f"Error reading {index_path}: {e}")
continue
html = build_index(dirpath, rel_dir, dirnames, filenames, include_dotfiles)
try:
with open(index_path, "w") as f:
f.write(html)
if verbose:
action = "Updated" if index_owned else "Created"
print(f"{action} index: {index_path}")
except PermissionError:
if verbose:
print(f"Permission denied: {index_path}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Generate terminal-style index.html files for a directory tree."
)
parser.add_argument(
"root", nargs="?", default=".", help="Root directory to start from (default: .)"
)
parser.add_argument(
"--include-dotfiles",
action="store_true",
help="Include dotfiles and dot directories",
)
parser.add_argument(
"--verbose", action="store_true", help="Print progress while generating indexes"
)
args = parser.parse_args()
process_dir(args.root, include_dotfiles=args.include_dotfiles, verbose=args.verbose)