This repository has been archived by the owner on Aug 18, 2024. It is now read-only.
forked from daylinmorgan/monolisa-nerdfont-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpatch-monolisa
executable file
·250 lines (205 loc) · 6.94 KB
/
patch-monolisa
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
243
244
245
246
247
248
249
250
#!/usr/bin/env python3
import argparse
import itertools
import os
import subprocess
import sys
import threading
import time
from pathlib import Path
from typing import List, Set
class Color:
def __init__(self):
self.bold = "\033[1m"
self.red = "\033[1;31m"
self.green = "\033[1;32m"
self.yellow = "\033[1;33m"
self.magenta = "\033[1;35m"
self.cyan = "\033[1;36m"
self.end = "\033[0m"
if os.getenv("NO_COLOR"):
for attr in self.__dict__:
setattr(self, attr, "")
class Spinner:
# https://raw.githubusercontent.com/Tagar/stuff/master/spinner.py
def __init__(self, message, delay=0.1):
# self.spinner = itertools.cycle(["-", "/", "|", "\\"])
self.spinner = itertools.cycle([f"{c} " for c in "⣾⣽⣻⢿⡿⣟⣯⣷"])
self.delay = delay
self.busy = False
self.spinner_visible = False
sys.stdout.write(message)
def write_next(self):
with self._screen_lock:
if not self.spinner_visible:
sys.stdout.write(next(self.spinner))
self.spinner_visible = True
sys.stdout.flush()
def remove_spinner(self, cleanup=False):
with self._screen_lock:
if self.spinner_visible:
# sys.stdout.write("\b")
sys.stdout.write("\b\b\b")
self.spinner_visible = False
if cleanup:
sys.stdout.write(" ") # overwrite spinner with blank
# sys.stdout.write("\r") # move to next line
sys.stdout.write("\r\033[K")
sys.stdout.flush()
def spinner_task(self):
while self.busy:
self.write_next()
time.sleep(self.delay)
self.remove_spinner()
def __enter__(self):
if sys.stdout.isatty():
self._screen_lock = threading.Lock()
self.busy = True
self.thread = threading.Thread(target=self.spinner_task)
self.thread.start()
def __exit__(self, exc_type, exc_val, exc_traceback):
if sys.stdout.isatty():
self.busy = False
self.remove_spinner(cleanup=True)
else:
sys.stdout.write("\r")
def run_cmd(
command: List[str], fontfile: Path, verbose: bool, ignore_error: bool = False
) -> None:
"""run a subcommand
Args:
command: Subcommand to be run in subprocess.
fontfile: Path to font file that will be patched
verbose: If true, print subcommand output.
"""
p = subprocess.run(
command,
stdout=None if verbose else subprocess.PIPE,
stderr=None if verbose else subprocess.STDOUT,
universal_newlines=True,
)
if p.returncode != 0 and not ignore_error:
print()
print(p.stdout)
err_msg = (
f"{color.red}ERROR{color.end}: patching font file "
f"{fontfile.name} see above for font-patcher output"
)
echo(err_msg, hue="red")
sys.exit(1)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--font-path",
help="path to font files",
action="append",
type=Path,
required=True,
)
parser.add_argument(
"-o", "--output", help="target output directory", default="patched", type=Path
)
parser.add_argument(
"-d", "--docker", help="use nerdfonts/patcher docker image", action="store_true"
)
parser.add_argument(
"-v", "--verbose", help="show subprocess output", action="store_true"
)
return parser.parse_known_args()
def patch_single_font(
font_path: Path, output_dir: Path, fp_args: str, verbose: bool
) -> None:
# update display name to full resolved path
rel_path = font_path.absolute()
output_path = (output_dir / rel_path.parent.name).absolute()
output_path.mkdir(exist_ok=True, parents=True)
cmd = [
"fontforge",
"-script",
f"{Path(__file__).parent}/font-patcher",
"--glyphdir",
f"{Path(__file__).parent}/src/glyphs/",
"-out",
f"{output_path}",
f"{font_path.absolute()}",
]
if fp_args:
cmd += fp_args.split(" ")
if verbose:
echo(f"cmd: {' '.join(cmd)}")
run_cmd(cmd, font_path, verbose)
else:
with Spinner(
f"{color.yellow}:::{color.end} Patching font "
f"{color.bold}{font_path.name}{color.end}... "
):
run_cmd(cmd, font_path, verbose)
echo(f"{rel_path} patched!", hue="green")
def collect_files_by_dir(fontfiles: List[Path]) -> Set[Path]:
return set([f.parent.absolute() for f in fontfiles])
def patch_font_dir_docker(
font_dir_path: Path, output_dir: Path, fp_args: str, verbose: bool
) -> None:
output_path = (
output_dir / font_dir_path.relative_to(font_dir_path.parent)
).absolute()
output_path.mkdir(exist_ok=True, parents=True)
cmd = [
"docker",
"run",
"--rm",
"-v",
f"{font_dir_path}:/in",
"-v",
f"{output_path}:/out",
"nerdfonts/patcher",
]
if fp_args:
cmd += fp_args.split(" ")
# ignoring the fact that docker exits with code 1
if verbose:
echo(f"cmd: {' '.join(cmd)}")
run_cmd(cmd, font_dir_path, verbose, ignore_error=True)
else:
with Spinner(
f"{color.yellow}:::{color.end} Patching fonts in "
f"{color.bold}{font_dir_path.name}{color.end}... "
):
run_cmd(cmd, font_dir_path, verbose, ignore_error=True)
echo(f"{font_dir_path.name}/ fonts patched!", hue="green")
def get_font_files(p):
exts = ["otf", "ttf", "woff", "woff2"]
if p.is_file():
return (p,)
if p.is_dir():
return (f for ext in exts for f in p.glob(f"**/*.{ext}"))
return ()
def echo(msg: str, header=False, hue="cyan") -> None:
if header:
print(f"==>{color.magenta} {msg} {color.end}<==")
else:
print(f"{color.__dict__[hue]}::{color.end} {msg}")
def main():
echo("MonoLisa NerdFont Patcher", header=True)
args, fp_args = get_args()
fp_args = " ".join(fp_args)
if fp_args:
echo(f"Flags passed to font-patcher: {fp_args}")
if args.verbose:
echo("Patching the following files")
for fontfile in args.font_path:
sys.stdout.write(f" {color.magenta}->{color.end} {fontfile}\n")
fontfiles = [f for p in args.font_path for f in get_font_files(p)]
if args.docker:
echo("==> DOCKER MODE ENABLED")
for font_dir in collect_files_by_dir(fontfiles):
patch_font_dir_docker(font_dir, args.output, fp_args, args.verbose)
else:
for fontfile in fontfiles:
patch_single_font(Path(fontfile), args.output, fp_args, args.verbose)
echo("fonts are patched", hue="green")
echo("Happy typing!", hue="green")
if __name__ == "__main__":
color = Color()
main()