-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnerd-font-manager.sh
More file actions
executable file
·382 lines (324 loc) · 9.44 KB
/
Copy pathnerd-font-manager.sh
File metadata and controls
executable file
·382 lines (324 loc) · 9.44 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#!/usr/bin/env bash
# =============================
# Config
# =============================
VERSION="1.0.3"
LOG_FILE="$HOME/.nerd-font-manager.log"
# Updated favorites (8 fonts)
FAVORITES=(
"fira-code"
"jetbrains-mono"
"hack"
"iosevka"
"meslo-lg"
"caskaydia-cove"
"roboto-mono"
"sauce-code-pro"
)
# =============================
# Helpers
# =============================
log_event() {
local action="$1"
local font="$2"
echo "$(date '+%Y-%m-%d %H:%M:%S') | $action | $font" >> "$LOG_FILE"
}
get_available_fonts() {
brew search --casks nerd-font | grep "font-.*-nerd-font"
}
get_installed_fonts() {
brew list --cask | grep "font-.*-nerd-font"
}
get_installed_nerd_casks() {
brew list --cask | grep "font-.*-nerd-font"
}
# =============================
# Core actions
# =============================
install_fonts_by_middle_names() {
local names=("$@")
for font in "${names[@]}"; do
local cask="font-${font}-nerd-font"
echo "Installing: $cask"
if brew install --cask "$cask"; then
log_event "INSTALL" "$cask"
else
log_event "INSTALL_FAILED" "$cask"
fi
done
}
uninstall_fonts_by_middle_names() {
local names=("$@")
for font in "${names[@]}"; do
local cask="font-${font}-nerd-font"
echo "Uninstalling: $cask"
if brew uninstall --cask "$cask"; then
log_event "UNINSTALL" "$cask"
else
log_event "UNINSTALL_FAILED" "$cask"
fi
done
}
bulk_update_fonts() {
local casks
casks=$(get_installed_nerd_casks)
if [ -z "$casks" ]; then
echo "No Nerd Fonts installed to update."
return
fi
echo "Updating all installed Nerd Fonts..."
echo "$casks" | while read -r cask; do
echo "Upgrading: $cask"
if brew upgrade --cask "$cask"; then
log_event "UPGRADE" "$cask"
else
log_event "UPGRADE_FAILED" "$cask"
fi
done
echo "Bulk update complete."
}
# =============================
# CLI mode (fzf)
# =============================
cli_mode() {
if ! command -v fzf &> /dev/null; then
echo "fzf is required for CLI mode. Install it with: brew install fzf"
exit 1
fi
local ACTION
ACTION=$(printf "Install\nUninstall\nInstall favorites\nBulk update" | fzf --prompt="Choose action > ")
if [ -z "$ACTION" ]; then
echo "No action selected."
exit 0
fi
case "$ACTION" in
"Install")
local FONTS
FONTS=$(get_available_fonts | sed 's/font-//; s/-nerd-font//')
if [ -z "$FONTS" ]; then
echo "No Nerd Fonts found in Homebrew."
exit 0
fi
# Add [ALL] option and enable search with header hint
local SELECTED
SELECTED=$(echo -e "[ALL] Install all fonts\n$FONTS" | fzf --multi --prompt="Install (type to filter) > " --header="Select fonts (TAB to multi-select, ENTER to confirm)")
if [ -z "$SELECTED" ]; then
echo "Nothing selected."
exit 0
fi
# Check if ALL was selected
if echo "$SELECTED" | grep -q "^\[ALL\]"; then
echo "Installing ALL available Nerd Fonts..."
mapfile -t names <<< "$FONTS"
else
mapfile -t names <<< "$SELECTED"
fi
install_fonts_by_middle_names "${names[@]}"
;;
"Uninstall")
local FONTS
FONTS=$(get_installed_fonts | sed 's/font-//; s/-nerd-font//')
if [ -z "$FONTS" ]; then
echo "No Nerd Fonts installed."
exit 0
fi
# Add [ALL] option and enable search with header hint
local SELECTED
SELECTED=$(echo -e "[ALL] Uninstall all fonts\n$FONTS" | fzf --multi --prompt="Uninstall (type to filter) > " --header="Select fonts (TAB to multi-select, ENTER to confirm)")
if [ -z "$SELECTED" ]; then
echo "Nothing selected."
exit 0
fi
# Check if ALL was selected
if echo "$SELECTED" | grep -q "^\[ALL\]"; then
echo "Uninstalling ALL installed Nerd Fonts..."
mapfile -t names <<< "$FONTS"
else
mapfile -t names <<< "$SELECTED"
fi
uninstall_fonts_by_middle_names "${names[@]}"
;;
"Install favorites")
echo "Installing favorite fonts: ${FAVORITES[*]}"
install_fonts_by_middle_names "${FAVORITES[@]}"
;;
"Bulk update")
bulk_update_fonts
;;
*)
echo "Unknown action."
;;
esac
echo "Done!"
}
# =============================
# GUI mode (osascript)
# =============================
gui_choose_action() {
osascript <<EOF
choose from list {"Install", "Uninstall", "Install favorites", "Bulk update"} with title "Nerd Font Manager" with prompt "Choose an action:"
EOF
}
gui_mode() {
local ACTION
ACTION=$(gui_choose_action)
if [[ "$ACTION" == "false" ]]; then
exit 0
fi
case "$ACTION" in
"Install")
local FONTS
FONTS=$(get_available_fonts | sed 's/font-//; s/-nerd-font//')
if [ -z "$FONTS" ]; then
osascript -e 'display alert "No Nerd Fonts found in Homebrew." as warning'
exit 0
fi
# Optional search filter
local SEARCH
if ! SEARCH=$(osascript <<EOF
display dialog "Enter search term (leave empty for all fonts):" default answer "" with title "Filter Fonts" buttons {"Cancel", "Search"} default button "Search"
text returned of result
EOF
); then
exit 0
fi
# Filter fonts if search term provided
local FILTERED_FONTS
if [ -n "$SEARCH" ]; then
FILTERED_FONTS=$(echo "$FONTS" | grep -i "$SEARCH")
if [ -z "$FILTERED_FONTS" ]; then
osascript -e "display alert \"No fonts matching '$SEARCH' found.\" as warning"
exit 0
fi
else
FILTERED_FONTS="$FONTS"
fi
# Add [ALL] option at the beginning
local FONT_LIST
FONT_LIST=$(echo "$FILTERED_FONTS" | tr '\n' ',' | sed 's/,$//' | sed 's/,/", "/g')
local SELECTED
SELECTED=$(osascript <<EOF
choose from list {"[ALL]", "$FONT_LIST"} with title "Install Nerd Fonts" with prompt "Select fonts to install:" with multiple selections allowed
EOF
)
if [[ "$SELECTED" == "false" ]]; then
exit 0
fi
# Check if ALL was selected
if [[ "$SELECTED" == *"[ALL]"* ]]; then
echo "Installing ALL filtered Nerd Fonts..."
mapfile -t names <<< "$FILTERED_FONTS"
else
IFS=', ' read -ra names <<< "$SELECTED"
fi
install_fonts_by_middle_names "${names[@]}"
;;
"Uninstall")
local FONTS
FONTS=$(get_installed_fonts | sed 's/font-//; s/-nerd-font//')
if [ -z "$FONTS" ]; then
osascript -e 'display alert "No Nerd Fonts installed." as warning'
exit 0
fi
# Optional search filter
local SEARCH
if ! SEARCH=$(osascript <<EOF
display dialog "Enter search term (leave empty for all fonts):" default answer "" with title "Filter Fonts" buttons {"Cancel", "Search"} default button "Search"
text returned of result
EOF
); then
exit 0
fi
# Filter fonts if search term provided
local FILTERED_FONTS
if [ -n "$SEARCH" ]; then
FILTERED_FONTS=$(echo "$FONTS" | grep -i "$SEARCH")
if [ -z "$FILTERED_FONTS" ]; then
osascript -e "display alert \"No fonts matching '$SEARCH' found.\" as warning"
exit 0
fi
else
FILTERED_FONTS="$FONTS"
fi
# Add [ALL] option at the beginning
local FONT_LIST
FONT_LIST=$(echo "$FILTERED_FONTS" | tr '\n' ',' | sed 's/,$//' | sed 's/,/", "/g')
local SELECTED
SELECTED=$(osascript <<EOF
choose from list {"[ALL]", "$FONT_LIST"} with title "Uninstall Nerd Fonts" with prompt "Select fonts to uninstall:" with multiple selections allowed
EOF
)
if [[ "$SELECTED" == "false" ]]; then
exit 0
fi
# Check if ALL was selected
if [[ "$SELECTED" == *"[ALL]"* ]]; then
echo "Uninstalling ALL filtered Nerd Fonts..."
mapfile -t names <<< "$FILTERED_FONTS"
else
IFS=', ' read -ra names <<< "$SELECTED"
fi
uninstall_fonts_by_middle_names "${names[@]}"
;;
"Install favorites")
echo "Installing favorite fonts: ${FAVORITES[*]}"
install_fonts_by_middle_names "${FAVORITES[@]}"
;;
"Bulk update")
bulk_update_fonts
;;
*)
echo "Unknown action."
;;
esac
osascript -e 'display alert "Done!" as informational'
}
# =============================
# Main entry point
# =============================
main() {
case "${1:-}" in
--gui|-g)
gui_mode
;;
--cli|-c|"")
cli_mode
;;
--version|-v)
echo "nerd-font-manager $VERSION"
;;
--list-installed|-l)
local installed
installed=$(get_installed_fonts)
if [ -z "$installed" ]; then
echo "No Nerd Fonts installed."
else
echo "$installed"
fi
;;
--list-available|-a)
local available
available=$(get_available_fonts)
if [ -z "$available" ]; then
echo "No Nerd Fonts found."
else
echo "$available"
fi
;;
--help|-h)
echo "Usage: $0 [--cli|-c] [--gui|-g] [--help|-h] [--version|-v]"
echo " --cli, -c Run in CLI mode with fzf (default)"
echo " --gui, -g Run in GUI mode with macOS dialogs"
echo " --version, -v Show version"
echo " --list-installed, -l List installed Nerd Fonts"
echo " --list-available, -a List available Nerd Fonts"
echo " --help, -h Show this help message"
;;
*)
echo "Unknown option: $1"
echo "Use --help for usage information."
exit 1
;;
esac
}
main "$@"