-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathascii2header.py
242 lines (204 loc) · 12.2 KB
/
ascii2header.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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# ⠀⠀⠀⠀⠀⠀⠀⢻⣿⡿⣌⢿⣿⣿⣿⣿⣿⣷⣦⡀⠀⠀⢀⣠⠄⣠⣶⣿⣿⣿⣿⣿⣿⣟⣠⣾⣿⣿⣿⠟⢡⣿⣿⢿⣟⡣⠞⣿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⢸⣽⠏⠘⣯⢻⣿⣿⣿⣿⣿⠯⠑⠛⣛⣿⣗⣋⠉⢉⢉⣛⠿⣿⣿⣿⣿⣿⣿⣮⣍⠀⠴⠒⠤⣾⣟⣭⡊⠰⢿⣿⣿⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠆⠻⡧⠘⣿⡅⠹⣿⠟⣋⣤⣶⢎⣾⣿⣿⣿⣿⣿⣿⣿⣮⣝⢆⠙⢿⣿⣿⣿⣿⣿⣷⣄⢨⣿⣿⠿⠿⠭⢬⣘⢿⣿⡟⣼⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠀⢷⠦⢀⣽⡷⢀⣴⣿⣿⣿⣷⣿⣿⣿⣿⣿⢿⣿⣿⣿⣿⣿⣿⣦⡢⠹⣮⢻⣿⣿⣿⣿⣧⡉⠿⠷⣶⠄⡀⢘⣿⣿⠇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠀⠿⣡⣿⣿⢿⣿⣟⣽⣿⣿⣿⣿⣿⣎⣿⣿⣟⣿⣿⣿⣿⣿⣄⢎⠃⢻⣿⣿⣿⣿⣷⡨⣛⠺⠋⠠⠸⠟⣿⢰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⢀⣾⣿⣿⢏⣿⣿⢹⡏⣿⣿⣿⣿⣿⣿⡸⣿⣿⣮⢿⣿⣿⣿⣟⢧⡣⡀⢻⣿⡿⣿⣿⣷⡸⡗⠀⠀⣼⡿⠋⢺⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# File: ascii2header.py ⠀⠀⠀⠀⠀⠀⠀⠀⠀⣰⣿⣿⣿⡟⡼⣿⠏⣿⣧⣿⣿⣿⣿⠸⣿⡇⠘⢿⣿⣧⡙⣿⣟⣿⣶⡳⣅⠈⣿⣷⢹⣿⣿⣧⠀⡠⢀⡉⠀⢠⣧⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⢀⣼⣿⣿⣿⣿⠛⣸⡟⢠⢹⣿⢻⣿⣿⣿⣧⢻⣿⡰⣬⡻⣿⣜⣌⠻⣮⡻⣿⣜⢆⠸⣿⡏⣿⣿⣿⡌⣷⠏⠀⠀⡺⣧⠎⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# Project: ascii2header ⠀⠀⠀⠀⠀⠀⣰⡟⣼⣿⣿⣿⡟⢠⣿⢁⣿⢸⣿⣽⣿⣿⣿⡌⣇⢻⣧⠻⠘⢙⢫⣭⣧⡐⢦⡲⣭⣥⡁⢿⣯⢹⣿⡞⣇⢑⣀⡔⢸⣧⢻⣿⡜⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# Github: marsdevx ⠀⠀⠀⠀⣠⣾⡟⣼⣿⣿⣿⣿⡇⣸⠇⣸⡟⡞⡿⡇⢿⣿⣿⣇⠘⡌⢟⡄⣿⣮⡣⠹⣧⣻⣮⡳⢌⢿⣿⣸⣿⠘⣿⡇⣿⠸⢏⣴⣿⣿⠈⣿⢿⣞⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⣠⣾⡿⠋⢰⣿⢻⣿⣿⣿⡇⡿⠀⢿⣛⡃⢡⢻⠈⢧⠹⣿⣆⠲⠈⢿⡜⣿⣿⠦⠄⠙⠛⠛⠒⠤⠍⠛⣿⡆⣿⣿⣿⠲⠟⠛⣾⢏⡀⣿⣏⢿⣎⢻⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# Created: 08:00 04/01/2025 ⣴⡿⠟⠋⠀⠀⣾⣿⣾⣿⠇⣿⠁⣣⢰⣿⣿⣷⡀⢏⢧⠈⣧⠘⢿⣄⠀⠘⠿⠉⢀⡀⠀⠀⠀⠀⠀⢢⣄⠀⢉⣰⣿⣿⣿⡆⠀⠾⣣⢿⢀⠘⣿⢦⢻⣷⢝⢿⣿⣿⣿⣿⣿⣿⣿ #
# Updated: 01:47 05/01/2025 ⠀⠀⠀⠀⠀⢠⣿⣿⣿⣿⡀⣿⠀⣿⣼⣿⡿⠿⠗⣈⢮⡃⠘⢂⠈⠻⢿⡄⢟⢰⣿⣇⣰⡆⠀⠀⣦⢸⣿⠃⢸⣿⣿⣿⢹⠱⢦⡸⢏⡾⢸⠀⠘⠀⡑⣝⢿⣿⣮⣝⢿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⢸⣿⣿⢸⣿⡇⢸⡄⣿⡯⠁⣠⠀⠀⠀⠨⣿⣷⣄⠹⣢⣜⡻⣷⣿⣿⣿⣏⠠⣆⡄⠀⣾⣿⣼⣾⣿⣿⡇⢸⠼⢾⡇⣾⠃⡘⡇⣦⢠⡈⠪⢳⣭⣛⠿⣿⣿⣿⣿⣿ #
# Path: ./ascii2header.py ⠀⠀⠀⠀⠀⢸⡇⣿⠘⣿⣇⠈⣧⠻⠁⣸⣿⣀⣤⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠷⠾⣶⠾⠿⠿⡿⢾⣿⣿⡇⢻⢰⠞⡠⠀⠀⣇⣿⣻⣇⣿⣿⣿⣶⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⡄⠀⠀⢸⡇⢹⡆⢻⣿⡆⠘⠄⢄⠸⣿⣿⡏⠀⠀⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠏⣠⡾⣟⣿⡇⣸⣿⣿⣇⣼⢨⠞⠁⢰⡀⣿⢸⢸⣿⣮⡻⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠇⠀⠀⢸⡇⠈⢧⠈⢿⣷⠀⠀⠘⣷⣽⣿⣿⣶⣿⣿⡿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣾⣿⣿⣿⣿⠁⣿⣿⣿⠿⣿⠀⠀⡀⢸⡇⢻⣿⡖⣿⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⡇⠀⠀⠀⣧⠀⠈⠆⠈⠻⣧⠱⡄⠸⣿⣫⡔⢹⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⢫⡇⣿⣿⣿⠀⡟⠀⢸⡇⢸⣿⡈⣿⣇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠸⡀⠀⠀⠀⠀⢀⠰⣦⡀⣧⣭⣴⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣵⡿⠃⣿⢻⡟⢰⡇⠀⠘⣧⠀⣿⣷⠸⣿⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠁⠀⠀⠀⢀⡾⠀⣿⣇⠸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠋⣠⠀⣿⣾⡇⢾⠃⠀⠀⣿⢠⢸⣿⣃⢿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠊⠀⡠⢻⣿⡤⣝⠿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠛⠁⣠⣾⡿⢰⣇⣿⠸⢹⠀⠀⠀⠸⠘⠀⢛⠉⠈⠇⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿ #
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡇⠸⣿⣇⢿⡇⢰⣬⠙⠛⠿⠿⣿⣿⣿⣿⣿⠿⠛⠁⠀⣠⣾⠟⣯⣾⣼⢹⡟⠁⡎⠀⢰⡶⠿⠟⠋⡛⠛⠓⠾⢭⣝⣛⠻⠿⢿⣿⣿⣿⣿⣿ #
import os
import sys
import math
import random
import argparse
import subprocess
from datetime import datetime
def header_info(path):
info = {
"file_name": "",
"empty1": "",
"git_repo": "",
"git_user": "",
"empty2": "",
"creation_time": "",
"update_time": "",
"empty3": "",
"rel_path": ""
}
file_name = os.path.basename(path)
info['file_name'] = f"File: {file_name}"
creation_timestamp = subprocess.check_output(["stat", "-f", "%B", path]).decode().strip()
creation_time = datetime.fromtimestamp(int(creation_timestamp)).strftime("%H:%M %d/%m/%Y")
info['creation_time'] = f"Created: {creation_time}"
update_time = datetime.fromtimestamp(os.stat(path).st_mtime).strftime("%H:%M %d/%m/%Y")
info['update_time'] = f"Updated: {update_time}"
git_url = subprocess.check_output(["git", "remote", "get-url", "origin"], cwd=os.path.dirname(path)).decode().strip()
if git_url.startswith("https://"):
git_user = git_url.split("/")[-2]
git_repo = git_url.split("/")[-1].replace(".git", "")
else:
git_user = "unknown"
git_repo = "unknown"
info['git_user'] = f"Github: {git_user}"
info['git_repo'] = f"Project: {git_repo}"
rel_path = os.path.relpath(path, git_repo)[1:]
info['rel_path'] = f"Path: {rel_path}"
return info
def generate_header_core(info, ascii):
width = 115
header_lines = []
ascii_index = 0
with open(ascii, 'r', encoding='utf-8') as file:
ascii = file.readlines()
ascii_lines = len(ascii)
if ascii_lines < 9:
print("Usage: ASCII art must be at least 9 lines")
sys.exit(1)
start_point = math.floor((ascii_lines - 9) / 2)
for i in range(ascii_lines):
if i >= start_point and i < start_point + len(info):
info_key = list(info.keys())[ascii_index]
info_value = info[info_key]
info_line = info_value.ljust(width // 2)
art_line = ascii[i].strip() if i < len(ascii) else ''
combined_line = f"{info_line}{art_line.rjust(width - len(info_line))}"
header_lines.append(combined_line)
ascii_index += 1
else:
art_line = ascii[i].strip()
empty_info = ' ' * (width // 2)
combined_line = f"{empty_info}{art_line.rjust(width - len(empty_info))}"
header_lines.append(combined_line)
return '\n'.join(header_lines)
def gen_header(path, header_core, info):
_, file_ext = os.path.splitext(info['file_name'])
if file_ext in [".c", ".css", ".js", ".ino", ".h", ".lua"]:
start_marker = "/* "
end_marker = " */"
elif file_ext == ".html":
start_marker = "<!-- "
end_marker = " -->"
else:
start_marker = "# "
end_marker = " #"
header_lines = header_core.splitlines()
header = []
for line in header_lines:
decorated_line = f"{start_marker}{line.ljust(116)}{end_marker}"
header.append(decorated_line)
header = "\n".join(header)
return header
def prepare_header_and_content(ascii, path):
info = header_info(path)
header_core = generate_header_core(info, ascii)
header = gen_header(path, header_core, info)
with open(path, "r") as file:
existing_content = file.readlines()
lines_to_skip = 0
for line in existing_content:
if line.strip().startswith(("/*", "#", "<!--")):
lines_to_skip += 1
else:
break
if lines_to_skip >= 9:
existing_content = existing_content[lines_to_skip:]
if len(existing_content) < 2 or existing_content[0].strip() or existing_content[1].strip():
existing_content = ["\n", "\n"] + existing_content
return header, existing_content
def write_header_to_file(ascii, file_path):
if ascii == "random":
ascii_arts_dir = os.path.expanduser("~/ascii2header/ascii-arts")
ascii_files = [f for f in os.listdir(ascii_arts_dir) if os.path.isfile(os.path.join(ascii_arts_dir, f))]
ascii = random.choice(ascii_files)
ascii = os.path.expanduser(f"~/ascii2header/ascii-arts/{ascii}")
if not os.path.exists(ascii):
print(f"Error: The file '{ascii}' does not exist.")
sys.exit(1)
header, existing_content = prepare_header_and_content(ascii, file_path)
with open(file_path, "w") as file:
file.write(header)
file.writelines(existing_content)
def write_header():
parser = argparse.ArgumentParser(
prog="ascii2header",
description=(
"ascii2header is a tool for adding customizable headers to files or directories with optional ASCII art. "
"Headers include details like file name, creation and update timestamps, and project information."
),
epilog=(
"Examples of usage:\n"
" 1. Add a header to a single file:\n"
" ascii2header <file>\n\n"
" 2. Add headers to all files in a directory:\n"
" ascii2header <dir>\n\n"
" 3. Add headers to multiple files and directories:\n"
" ascii2header <dir> <file> <dir> <file> <file>\n\n"
" 4. Specify ASCII art:\n"
" ascii2header <file> -a <ascii>\n\n"
"Available ASCII Art Options:\n"
" - anime-girl\n"
" - asteroid\n"
" - cat\n"
" - eye\n"
" - eyes\n"
" - franxx\n"
" - gary\n"
" - gojo\n"
" - pikachu\n"
" - rabbit\n\n"
"Advanced Options:\n"
" To add your own ASCII art:\n"
" 1. Navigate to the ASCII arts directory:\n"
" cd ~/ascii2header/ascii-arts\n"
" 2. Add your custom ASCII art by creating a new file and saving your art in it.\n\n"
"For more details, visit the Github repository: marsdevx/ascii2header"
),
formatter_class=argparse.RawTextHelpFormatter
)
parser.add_argument(
"paths",
nargs="+",
help="Path(s) to the file(s) or directory(ies) where the header will be added.",
)
parser.add_argument(
"-a", "--ascii",
default="random",
help=(
"Specify an ASCII art file to include in the header. Provide the name of the file from "
"the ~/ascii2header/ascii-arts directory. Defaults to 'random'."
)
)
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
args = parser.parse_args()
for path in args.paths:
path = os.path.abspath(os.path.expanduser(path))
if not os.path.exists(path):
print(f"Error: The path '{path}' does not exist.")
continue
if os.path.isfile(path):
write_header_to_file(args.ascii, path)
elif os.path.isdir(path):
for file_name in os.listdir(path):
file_path = os.path.join(path, file_name)
if os.path.isfile(file_path):
write_header_to_file(args.ascii, file_path)
else:
print(f"Error: The path '{path}' is neither a file nor a directory.")
if __name__ == "__main__":
write_header()