Repository navigation
Expand file tree
/
Copy pathgetnf
More file actions
executable file
·1243 lines (1164 loc) · 36.1 KB
/
Copy pathgetnf
File metadata and controls
executable file
·1243 lines (1164 loc) · 36.1 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# getnf: A better way to install Nerd Fonts
#
# Author: @ronniedroid
# Maintainer: @trimclain
# License: GPL-3.0
readonly VERSION="0.5.0-dev"
readonly NERDFONTSAPI='https://api.github.com/repos/ryanoasis/nerd-fonts'
readonly NERDFONTSREPO='https://github.com/ryanoasis/nerd-fonts'
ON_MAC='false'
if [[ $(uname) == "Darwin" ]]; then
ON_MAC='true'
fi
readonly ON_MAC
readonly GETNF_DIR="${XDG_DATA_HOME:-$HOME/.local/share}/getnf"
readonly FONT_RELEASES_DIR="$GETNF_DIR/releases"
readonly RELEASE_FILE="$GETNF_DIR/release.txt"
readonly ALL_FONTS_FILE="$GETNF_DIR/all_fonts.txt"
NEW_RELEASE_AVAILABLE='false'
OPERATE_GLOBALLY='false'
KEEP_FONT_ARCHIVES='false'
INCLUDE_INSTALLED_FONTS='false'
DOWNLOAD_DIR=''
DOWNLOAD_STAGING_DIR=''
FONT_DIR=''
LOADING_ANIMATION_PID=''
LOADING_ANIMATION_ACTIVE='false'
OLIVE=''
CYAN=''
RED=''
GREEN=''
RESET=''
ITALIC=''
if command -v tput > /dev/null && [[ -t 1 ]]; then
# Use terminal capabilities for colors rather than hard-coded escapes.
# Colors: https://www.ditig.com/publications/256-colors-cheat-sheet
OLIVE=$(tput setaf 3)
CYAN=$(tput setaf 6)
RED=$(tput setaf 9)
GREEN=$(tput setaf 2)
RESET=$(tput sgr0)
ITALIC=$'\e[3m'
fi
readonly OLIVE CYAN RED GREEN RESET ITALIC
readonly ANIMATION=('-' "\\" '|' '/')
readonly FRAME_INTERVAL=0.25
#######################################
# Display command-line options and usage examples.
# Arguments:
# None
# Outputs:
# Writes help to stdout.
#######################################
display_help() {
cat << EOF
Usage:
getnf [options]
OPTIONS:
-h display this help message
-k keep the downloaded font archives
-a show already installed Nerd Fonts in the menu
-g install/uninstall/list/update Nerd Fonts for all users
-l show the list of installed Nerd Fonts
-L show the list of all Nerd Fonts
-f select and install Nerd Fonts using fzf
-i <font> directly install the specified Nerd Fonts
-u <font> uninstall the specified Nerd Fonts
-U update all installed Nerd Fonts
-V display current getnf version
To install fonts using the menu:
- Choose one or more fonts (by index/number) to install
- Hit Return/Enter to install the selected fonts
- Type 'q' to quit
To install fonts directly:
- Get the exact name of a font from the menu
- Use 'getnf -i "<font-name>"' to install a font
- Use 'getnf -i "<name1>,<name2>"' to install multiple fonts
To uninstall fonts use the same syntax as for direct install.
To install, uninstall, list, update fonts for all users use -gi, -gu, -gl, -gU.
EOF
}
info() {
printf '%s\n' "$1"
}
title() {
printf '\n%s%b%s\n' "$OLIVE" "$1" "$RESET"
}
confirm() {
printf '%s%b%s\n' "$GREEN" "$1" "$RESET"
}
alert() {
printf '%s%b%s\n' "$RED" "$1" "$RESET"
}
#######################################
# Print an error and exit the script with status 1.
# Arguments:
# Error message.
# Outputs:
# Writes the message to stderr.
#######################################
error() {
alert "$1" >&2
exit 1
}
#######################################
# Print an argument error and help, then exit with status 1.
# Arguments:
# Error message.
# Outputs:
# Writes the error and help to stderr.
#######################################
usage_error() {
alert "$1" >&2
printf '\n' >&2
display_help >&2
exit 1
}
#######################################
# Exit with status 1 if a required command is unavailable.
# Arguments:
# Command name.
# Outputs:
# Writes an error to stderr if the command is missing.
#######################################
ensure_installed() {
if ! command -v "$1" > /dev/null; then
error "Dependency $1 is not installed on your system."
fi
}
# Loading animation adapted from:
# https://github.com/Silejonu/bash_loading_animations
erase_line() {
# Move up one line and erase it.
printf '\033[1A\033[2K'
}
erase_char() {
# Move left one column and erase to the end of the line.
printf '\033[1D\033[K'
}
#######################################
# Draw animation frames until the background process is stopped.
# Globals:
# ANIMATION
# FRAME_INTERVAL
# Arguments:
# None
# Outputs:
# Writes animation frames and erase sequences to stdout.
#######################################
play_loading_animation_loop() {
while true; do
local frame
for frame in "${ANIMATION[@]}"; do
printf '\033[1D\033[K%s' "$frame"
sleep "$FRAME_INTERVAL"
done
done
}
#######################################
# Start the background animation and record its process ID.
# Globals:
# LOADING_ANIMATION_PID
# LOADING_ANIMATION_ACTIVE
# Arguments:
# None
# Outputs:
# Writes animation frames and erase sequences to stdout in the background.
# Hides the cursor when stdout is a terminal.
#######################################
start_loading_animation() {
if command -v tput > /dev/null && [[ -t 1 ]]; then
tput civis # Hide the terminal cursor.
fi
play_loading_animation_loop &
LOADING_ANIMATION_PID="${!}"
LOADING_ANIMATION_ACTIVE='true'
}
#######################################
# Stop an active animation and restore the terminal cursor.
# Globals:
# LOADING_ANIMATION_PID
# LOADING_ANIMATION_ACTIVE
# Arguments:
# None
# Outputs:
# Writes erase and cursor-restoration sequences to stdout.
# Returns:
# 0 if no animation is active; otherwise the last command status.
#######################################
stop_loading_animation() {
[[ "$LOADING_ANIMATION_ACTIVE" == "true" ]] || return 0
erase_char
kill "$LOADING_ANIMATION_PID" &> /dev/null
wait "$LOADING_ANIMATION_PID" &> /dev/null
LOADING_ANIMATION_PID=''
LOADING_ANIMATION_ACTIVE='false'
if command -v tput > /dev/null && [[ -t 1 ]]; then
tput cnorm # Restore the terminal cursor.
fi
}
handle_interrupt() {
alert '\nInterrupted. Exiting.' >&2
exit 130
}
handle_termination() {
alert '\nTerminated. Exiting.' >&2
exit 143
}
handle_hangup() {
alert '\nTerminal disconnected. Exiting.' >&2
exit 129
}
#######################################
# Stop animation and remove partial staging; retain complete archives with -k.
# Globals:
# DOWNLOAD_DIR
# DOWNLOAD_STAGING_DIR
# KEEP_FONT_ARCHIVES
# Arguments:
# None
#######################################
cleanup() {
stop_loading_animation
if [[ -n "$DOWNLOAD_STAGING_DIR" ]]; then
rm -rf -- "$DOWNLOAD_STAGING_DIR"
fi
if [[ -n "$DOWNLOAD_DIR" && "$KEEP_FONT_ARCHIVES" == "false" ]]; then
rm -rf -- "$DOWNLOAD_DIR"
fi
}
#######################################
# Set FONT_DIR for the selected platform and installation scope.
# Globals:
# ON_MAC
# OPERATE_GLOBALLY
# HOME
# FONT_DIR
# Arguments:
# None
#######################################
get_font_dir() {
if [[ "$ON_MAC" == "true" ]] && [[ "$OPERATE_GLOBALLY" == "true" ]]; then
FONT_DIR="/Library/Fonts"
elif [[ "$ON_MAC" == "true" ]]; then
FONT_DIR="$HOME/Library/Fonts"
elif [[ "$OPERATE_GLOBALLY" == "true" ]]; then
FONT_DIR="/usr/local/share/fonts"
else
FONT_DIR="$HOME/.local/share/fonts"
fi
readonly FONT_DIR
}
#######################################
# Create the download directory and set DOWNLOAD_DIR.
# Globals:
# KEEP_FONT_ARCHIVES
# HOME
# TMPDIR
# DOWNLOAD_DIR
# Arguments:
# None
# Outputs:
# Writes errors to stderr.
# Returns:
# 0 on success, nonzero if the directory cannot be prepared.
#######################################
init_download_dir() {
local download_dir
if [[ "$KEEP_FONT_ARCHIVES" == "true" ]]; then
if command -v xdg-user-dir > /dev/null; then
download_dir=$(xdg-user-dir DOWNLOAD) || {
alert "Could not determine the download directory." >&2
return 1
}
[[ -n "$download_dir" ]] || {
alert "The download directory is empty." >&2
return 1
}
download_dir="$download_dir/getnf"
else
download_dir="$HOME/Downloads/getnf"
fi
mkdir -p "$download_dir" || {
alert "Could not create download directory: $download_dir" >&2
return 1
}
else
download_dir=$(mktemp -d "${TMPDIR:-/tmp}/getnf.XXXXXX") || {
alert "Could not create temporary download directory in ${TMPDIR:-/tmp}." >&2
return 1
}
fi
readonly DOWNLOAD_DIR="$download_dir"
}
#######################################
# Prepare download, font, and installation-record directories.
# Globals:
# OPERATE_GLOBALLY
# FONT_DIR
# FONT_RELEASES_DIR
# Arguments:
# None
# Outputs:
# Writes errors to stderr; sudo may prompt for authentication.
# Returns:
# 0 on success, nonzero if directory preparation fails.
#######################################
create_dirs() {
init_download_dir || return 1
get_font_dir
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo mkdir -p "$FONT_DIR" || {
alert "Could not create global font directory: $FONT_DIR" >&2
return 1
}
else
mkdir -p "$FONT_DIR" || {
alert "Could not create font directory: $FONT_DIR" >&2
return 1
}
fi
mkdir -p "$FONT_RELEASES_DIR" || {
alert "Could not create font state directory: $FONT_RELEASES_DIR" >&2
return 1
}
}
#######################################
# Check whether a connectivity endpoint responds with HTTP 204.
# Arguments:
# Endpoint URL.
# Connection timeout in seconds.
# Overall timeout in seconds.
# Returns:
# 0 for HTTP 204, 1 otherwise.
#######################################
_probe_204() {
local url="$1"
local connect_timeout="$2"
local max_time="$3"
[[ "$(curl -o /dev/null -s \
--connect-timeout "$connect_timeout" \
--max-time "$max_time" \
-w '%{http_code}' \
"$url" 2> /dev/null || true)" == "204" ]]
}
#######################################
# Try connectivity probes; exit with status 1 if neither succeeds.
# Arguments:
# None
# Outputs:
# Writes an error to stderr if both probes fail.
#######################################
verify_internet_access() {
_probe_204 "http://connectivitycheck.gstatic.com/generate_204" 1 2 ||
_probe_204 "https://cp.cloudflare.com/generate_204" 2 3 ||
error "Internet connection is required for getnf to work."
}
#######################################
# Check curl and connectivity, then prepare runtime directories.
# Arguments:
# None
# Outputs:
# Writes errors to stderr; sudo may prompt for authentication.
# Returns:
# 0 on success, nonzero on directory failure; exits on failed checks.
#######################################
check_dependencies() {
ensure_installed curl
verify_internet_access
create_dirs || return 1
}
#######################################
# Fetch RELEASE and set NEW_RELEASE_AVAILABLE by comparing the cache.
# Leave release-marker persistence to get_all_fonts after catalog refresh.
# Globals:
# NERDFONTSAPI
# RELEASE_FILE
# RELEASE
# NEW_RELEASE_AVAILABLE
# Arguments:
# None
# Outputs:
# Writes request and validation errors to stderr.
# Returns:
# 0 on success, nonzero on request failure, unusable data, or read failure.
#######################################
get_latest_release_version() {
local response current_release
response=$(curl --fail --silent --show-error "$NERDFONTSAPI/releases/latest") || {
alert "Could not fetch latest release metadata." >&2
return 1
}
RELEASE=$(printf '%s\n' "$response" |
awk -v RS=',' -F'"' '/"tag_name"[[:space:]]*:/ {print $4}')
# These are parsed-value checks, not structural JSON validation.
if [[ ! "$RELEASE" =~ ^v[0-9]+\.[0-9]+\.[0-9]+([.-][A-Za-z0-9.-]+)?$ ]]; then
alert "Unusable release metadata." >&2
return 1
fi
current_release=''
if [[ -f "$RELEASE_FILE" ]]; then
current_release=$(cat "$RELEASE_FILE") || return 1
fi
NEW_RELEASE_AVAILABLE='false'
if [[ "$current_release" != "$RELEASE" ]]; then
NEW_RELEASE_AVAILABLE='true'
fi
# Persist only after the catalog refresh succeeds (see get_all_fonts).
}
#######################################
# Get the installation-record path for a font in the selected scope.
# Globals:
# OPERATE_GLOBALLY
# FONT_RELEASES_DIR
# Arguments:
# Font name.
# Outputs:
# Writes the record path to stdout.
#######################################
get_font_release_file() {
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
echo "$FONT_RELEASES_DIR/${1}_release.global.txt"
else
echo "$FONT_RELEASES_DIR/${1}_release.txt"
fi
}
#######################################
# Check whether a font has a saved version matching RELEASE.
# Globals:
# RELEASE
# Arguments:
# Font name.
# Returns:
# 0 if the saved version matches, 1 otherwise.
#######################################
is_latest_version() {
local font_release_file
font_release_file="$(get_font_release_file "$1")"
if [[ -f "$font_release_file" ]]; then
local font_release_version
font_release_version=$(cat "$font_release_file")
if [[ "$font_release_version" == "$RELEASE" ]]; then
return 0
fi
fi
return 1
}
#######################################
# Fetch the master-branch catalog and replace its cache after value checks.
# Globals:
# NERDFONTSAPI
# ALL_FONTS_FILE
# Arguments:
# None
# Outputs:
# Writes request, validation, and persistence errors to stderr.
# Returns:
# 0 on success, nonzero on request, validation, or persistence failure.
#######################################
create_all_fonts_file() {
local response font_list
response=$(curl --fail --silent --show-error "$NERDFONTSAPI/contents/patched-fonts?ref=master") || {
alert "Could not fetch font catalog." >&2
return 1
}
font_list=$(printf '%s\n' "$response" |
awk -v RS=',' -F'"' '/"name"[[:space:]]*:/ {print $4}')
if ! valid_font_catalog "$font_list"; then
alert "Unusable font catalog." >&2
return 1
fi
replace_metadata_file "$ALL_FONTS_FILE" "$font_list"
}
#######################################
# Check that extracted catalog names are nonempty and use safe characters.
# This validates parsed values, not JSON structure.
# Arguments:
# Newline-separated font names.
# Returns:
# 0 if every name is usable, 1 otherwise.
#######################################
valid_font_catalog() {
local name
[[ -n "$1" ]] || return 1
while IFS= read -r name; do
[[ "$name" =~ ^[A-Za-z0-9][A-Za-z0-9_-]*$ ]] || return 1
done <<< "$1"
}
#######################################
# Write metadata through a unique sibling file and checked rename.
# Staging beside the destination keeps replacement on the same filesystem.
# Arguments:
# Destination file path.
# Text to save; a final newline is appended.
# Outputs:
# Writes staging and persistence errors to stderr.
# Returns:
# 0 on success, nonzero if staging, writing, or replacement fails.
#######################################
replace_metadata_file() {
local temporary
temporary=$(mktemp "$1.tmp.XXXXXX") && [[ -n "$temporary" ]] || {
alert "Could not stage metadata: $1" >&2
return 1
}
if [[ -d "$1" ]] || ! printf '%s\n' "$2" > "$temporary" ||
! mv -f -- "$temporary" "$1"; then
rm -f -- "$temporary"
alert "Could not save metadata: $1" >&2
return 1
fi
}
#######################################
# Load ALL_FONTS, refreshing a missing or unusable catalog when needed.
# Save a changed release marker only after the catalog is ready.
# Globals:
# ALL_FONTS_FILE
# NEW_RELEASE_AVAILABLE
# RELEASE_FILE
# RELEASE
# ALL_FONTS
# Arguments:
# None
# Outputs:
# Writes refresh and persistence errors to stderr.
# Returns:
# 0 on success, nonzero if reading, refresh, or persistence fails.
#######################################
get_all_fonts() {
local line catalog
catalog=''
if [[ -f "$ALL_FONTS_FILE" ]]; then
catalog=$(cat "$ALL_FONTS_FILE") || return 1
fi
if [[ "$NEW_RELEASE_AVAILABLE" == "true" ]] || ! valid_font_catalog "$catalog"; then
create_all_fonts_file || return 1
catalog=$(cat "$ALL_FONTS_FILE") || return 1
valid_font_catalog "$catalog" || return 1
fi
if [[ "$NEW_RELEASE_AVAILABLE" == "true" ]]; then
replace_metadata_file "$RELEASE_FILE" "$RELEASE" || return 1
fi
ALL_FONTS=()
# Use read instead of mapfile for macOS Bash 3.2 compatibility.
# mapfile -t ALL_FONTS < "$ALL_FONTS_FILE" # not available on mac
while IFS= read -r line; do
ALL_FONTS+=("$line")
done <<< "$catalog"
}
#######################################
# Set INSTALLED_FONTS to catalog names found in FONT_DIR.
# Globals:
# FONT_DIR
# ALL_FONTS
# INSTALLED_FONTS
# Arguments:
# None
#######################################
get_installed_fonts() {
local font
local installed_user_fonts=("$FONT_DIR"/*)
installed_user_fonts=("${installed_user_fonts[@]##*/}")
INSTALLED_FONTS=()
for font in "${ALL_FONTS[@]}"; do
if [[ " ${installed_user_fonts[*]} " == *" $font "* ]]; then
INSTALLED_FONTS+=("$font")
fi
done
}
#######################################
# Build FONT_OPTIONS, hiding current installations unless -a is enabled.
# Globals:
# INCLUDE_INSTALLED_FONTS
# ALL_FONTS
# INSTALLED_FONTS
# FONT_OPTIONS
# Arguments:
# None
#######################################
filter_installed_fonts() {
local font
if [[ "$INCLUDE_INSTALLED_FONTS" == "false" ]]; then
FONT_OPTIONS=()
for font in "${ALL_FONTS[@]}"; do
# Keep installed fonts selectable when their version is not current.
if [[ " ${INSTALLED_FONTS[*]} " != *" $font "* ]] ||
! is_latest_version "$font"; then
FONT_OPTIONS+=("$font")
fi
done
else
FONT_OPTIONS=("${ALL_FONTS[@]}")
fi
}
#######################################
# Download a font archive through private staging into DOWNLOAD_DIR.
# Globals:
# DOWNLOAD_DIR
# DOWNLOAD_STAGING_DIR
# NERDFONTSREPO
# RELEASE
# ON_MAC
# GREEN
# RESET
# Arguments:
# Font name.
# Outputs:
# Writes download status to stdout and curl progress/errors to stderr.
# Returns:
# 0 on success, nonzero if staging, curl, promotion, or cleanup fails.
#######################################
download_font() {
local status=0 staging_dir
# Bound connection and stalled-transfer waits without capping large downloads.
# Do not restart failed archives, including when curl config enables retries.
local opts=("--fail" "--show-error" "--location"
"--connect-timeout" "15" "--speed-limit" "1" "--speed-time" "120" "--retry" "0")
staging_dir=$(mktemp -d "$DOWNLOAD_DIR/.getnf-download.XXXXXX") || {
alert "Could not create private archive staging directory." >&2
return 1
}
DOWNLOAD_STAGING_DIR=$staging_dir
opts+=("--output" "$DOWNLOAD_STAGING_DIR/archive.partial")
if [[ $ON_MAC == "false" ]]; then
opts+=("--silent")
echo -n "Downloading $1... "
start_loading_animation
else
# TODO: figure out how to disable messages about bg process termination
opts+=("--progress-bar")
info "Downloading $1... "
fi
curl "${opts[@]}" "$NERDFONTSREPO/releases/download/$RELEASE/$1.tar.xz" || status=$?
if [[ $ON_MAC == "false" ]]; then
stop_loading_animation
fi
if ((status == 0)); then
if [[ -d "$DOWNLOAD_DIR/$1.tar.xz" ]]; then
alert "Archive destination is a directory: $DOWNLOAD_DIR/$1.tar.xz" >&2
status=1
else
mv -f -- "$DOWNLOAD_STAGING_DIR/archive.partial" "$DOWNLOAD_DIR/$1.tar.xz" || status=$?
fi
fi
rm -rf -- "$DOWNLOAD_STAGING_DIR" || return 1
DOWNLOAD_STAGING_DIR=''
((status == 0)) || return "$status"
if [[ $ON_MAC == "false" ]]; then
confirm "Done"
else
erase_line
erase_line
echo "Downloading $1... ${GREEN}Done${RESET}"
fi
}
#######################################
# Check archive readability, then extract into its family directory.
# Globals:
# OPERATE_GLOBALLY
# FONT_DIR
# Arguments:
# Font name.
# Outputs:
# Writes installation progress to stdout and command errors to stderr.
# Returns:
# 0 on success, nonzero if validation, directory creation, or extraction fails.
#######################################
extract_font() {
tar -tf "$1.tar.xz" > /dev/null || {
local status=$?
alert "Cannot read archive for $1." >&2
return "$status"
}
echo -n "Installing $1... "
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo mkdir -p "$FONT_DIR/$1" || return $?
sudo tar -xf "$1.tar.xz" -C "$FONT_DIR/$1" || return $?
else
mkdir -p "$FONT_DIR/$1" || return $?
tar -xf "$1.tar.xz" -C "$FONT_DIR/$1" || return $?
fi
}
#######################################
# Save RELEASE in the font installation record.
# Globals:
# RELEASE
# Arguments:
# Font name.
# Outputs:
# Writes staging and persistence errors to stderr.
# Returns:
# 0 on success, nonzero if staging, writing, or replacement fails.
#######################################
save_font_release() {
replace_metadata_file "$(get_font_release_file "$1")" "$RELEASE"
}
#######################################
# Download, extract, and record a font from DOWNLOAD_DIR, then restore cwd.
# Marker persistence failure does not roll back extracted font files.
# Globals:
# DOWNLOAD_DIR
# Arguments:
# Font name.
# Outputs:
# Writes installation progress to stdout and errors to stderr.
# Returns:
# 0 on success, nonzero if a directory change or installation step fails.
#######################################
install_font() {
local status=0
pushd "$DOWNLOAD_DIR" > /dev/null || return 1
download_font "$1" &&
extract_font "$1" &&
save_font_release "$1" &&
confirm "Done" || status=$?
popd > /dev/null || return 1
return "$status"
}
#######################################
# Append requested catalog names to SELECTED_FONTS in input order.
# Exit with status 1 at the first invalid name.
# Globals:
# ALL_FONTS
# SELECTED_FONTS
# Arguments:
# Comma- or space-separated font names.
# Outputs:
# Writes invalid-name errors to stderr.
#######################################
direct_install() {
local font_name candidate found
local IFS=', '
for font_name in $1; do
found=0
for candidate in "${ALL_FONTS[@]}"; do
if [[ "$candidate" == "$font_name" ]]; then
found=1
break
fi
done
if ((found)); then
SELECTED_FONTS+=("$font_name")
else
error "Invalid font name: $font_name. Try again."
fi
done
}
#######################################
# Validate every requested font before removing managed installations.
# Exit with status 1 on invalid input or a removal failure.
# Globals:
# FONT_DIR
# OPERATE_GLOBALLY
# Arguments:
# Comma- or whitespace-separated font names.
# Outputs:
# Writes removal progress to stdout and errors to stderr.
#######################################
uninstall_fonts() {
get_font_dir
local -a fonts_to_uninstall=() names=()
local remaining="$1" segment font marker
while true; do
segment=${remaining%%,*}
# Reject multiline input.
[[ "$segment" != *$'\n'* && "$segment" != *$'\r'* ]] ||
error "Invalid font selection."
IFS=$' \t' read -r -a names <<< "$segment"
# Reject empty input and empty comma fields, such as "Hack,,Other".
((${#names[@]} > 0)) || error "Empty font name."
for font in "${names[@]}"; do
[[ "$font" =~ ^[a-zA-Z0-9][a-zA-Z0-9_.+-]*$ ]] ||
error "Invalid font name: $font."
marker=$(get_font_release_file "$font")
[[ -d "$FONT_DIR/$font" ]] || error "Font $font not found."
[[ -f "$marker" ]] || error "Font $font is not managed by getnf."
fonts_to_uninstall+=("$font")
done
[[ "$remaining" == *,* ]] || break
remaining=${remaining#*,}
done
for font in "${fonts_to_uninstall[@]}"; do
printf 'Uninstalling %s... ' "$font"
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo rm -rf -- "${FONT_DIR:?}/$font" ||
error "Failed to uninstall font $font."
else
rm -rf -- "${FONT_DIR:?}/$font" ||
error "Failed to uninstall font $font."
fi
rm -f -- "$(get_font_release_file "$font")" ||
error "Font $font removed; failed to clear its saved information."
confirm "Done"
done
}
#######################################
# Show the archive location if kept and announce success when requested.
# Globals:
# KEEP_FONT_ARCHIVES
# DOWNLOAD_DIR
# Arguments:
# Whether all selected fonts installed successfully (default: true).
# Outputs:
# Writes archive location and success messages to stdout.
#######################################
post_install() {
if [[ "$KEEP_FONT_ARCHIVES" == "true" ]]; then
info "The downloaded font archives can be found in $DOWNLOAD_DIR"
fi
if [[ "${1:-true}" == "true" ]]; then
confirm "Enjoy your new fonts!"
fi
}
#######################################
# Select installed catalog fonts whose saved version is not current.
# Exit with status 0 if all are current or no installed fonts are found.
# Globals:
# INSTALLED_FONTS
# SELECTED_FONTS
# OPERATE_GLOBALLY
# Arguments:
# None
# Outputs:
# Writes update status and empty-installation messages to stdout.
#######################################
update_installed_fonts() {
local font
get_installed_fonts
if [[ ${#INSTALLED_FONTS[@]} -ne 0 ]]; then
for font in "${INSTALLED_FONTS[@]}"; do
if ! is_latest_version "$font"; then
SELECTED_FONTS+=("$font")
fi
done
if [[ ${#SELECTED_FONTS[@]} -eq 0 ]]; then
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
confirm "All globally installed Nerd Fonts are up to date."
else
confirm "All installed Nerd Fonts are up to date."
fi
exit 0
fi
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
info "Updating globally installed Nerd Fonts: ${SELECTED_FONTS[*]}"
else
info "Updating installed Nerd Fonts: ${SELECTED_FONTS[*]}"
fi
else
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
info "No globally installed Nerd Fonts found."
else
info "No installed Nerd Fonts found."
fi
exit 0
fi
}
#######################################
# Display installed fonts and their saved versions, or unknown version.
# Globals:
# INSTALLED_FONTS
# OPERATE_GLOBALLY
# Arguments:
# None
# Outputs:
# Writes the installed-font list to stdout.
# Returns:
# 1 if no installed fonts were found, otherwise the last command status.
#######################################
display_installed_fonts() {
local font
if [[ ${#INSTALLED_FONTS[@]} -eq 0 ]]; then
return 1
fi
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
title "Installed Nerd Fonts (globally):"
else
title "Installed Nerd Fonts:"
fi
for font in "${INSTALLED_FONTS[@]}"; do
local font_release_file
font_release_file="$(get_font_release_file "$font")"
if [[ -f "$font_release_file" ]]; then
local font_release_version
font_release_version=$(cat "$font_release_file")
printf '%s - %s\n' "$font" "$font_release_version"
else
printf '%s - unknown version\n' "$font"
fi
done
}
#######################################
# Display the getnf version.
# Globals:
# VERSION
# Arguments:
# None
# Outputs:
# Writes the getnf version to stdout.
#######################################
display_getnf_version() {
echo "getnf v$VERSION"
}
#######################################
# Display the cached font catalog with the selected release as its heading.
# Globals:
# RELEASE
# ALL_FONTS_FILE
# Arguments:
# None
# Outputs:
# Writes the heading and catalog to stdout; read errors go to stderr.
#######################################
display_all_fonts() {
title "Nerd Fonts ${RELEASE}:"
cat "$ALL_FONTS_FILE"
}
#######################################
# Display numbered font choices in three columns sized for the terminal.
# Globals:
# RELEASE
# FONT_OPTIONS
# Arguments:
# None
# Outputs:
# Writes the menu to stdout.
#######################################
display_font_menu() {
local font_index
local term_width
term_width=$(stty size | cut -d ' ' -f 2)
title "Nerd Fonts ${RELEASE}:"
(
for font_index in "${!FONT_OPTIONS[@]}"; do
printf '%d) %s\n' "$((font_index + 1))" "${FONT_OPTIONS[$font_index]}"
done
printf 'q) Quit\n'