forked from getnf/getnf
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgetnf
executable file
·718 lines (657 loc) · 19.5 KB
/
getnf
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
#!/bin/bash
# To check if Termux is being used
if [ $(ps -ef| grep -c com.termux) -eq 0 ]; then
echo "please make sure that you are using termux, this script is only made for termux"
exit 0
fi
PREFIX='/data/data/com.termux/files/usr'
readonly NERDFONTSAPI='https://api.github.com/repos/ryanoasis/nerd-fonts'
readonly NERDFONTSREPO='https://github.com/ryanoasis/nerd-fonts'
readonly DOWN_DIR="$HOME/share/termux-nf/Downloads"
readonly GETNF_DIR="$PREFIX/share/termux-nf"
readonly FONT_RELEASES_DIR="$GETNF_DIR/releases"
readonly RELEASE_FILE="$GETNF_DIR/release.txt"
readonly ALL_FONTS_FILE="$GETNF_DIR/all_fonts.txt"
readonly FONT_DIR="$PREFIX/share/termux-nf/fonts"
NEW_RELEASE_AVAILABLE='false'
OPERATE_GLOBALLY='false'
KEEP_FONT_ARCHIVES='false'
INCLUDE_INSTALLED_FONTS='false'
UPDATE_INSTALLED_FONTS='false'
DISPLAY_INSTALLED_FONTS='false'
DISPLAY_ALL_FONTS='false'
if command -v tput > /dev/null; then
# tput is part of the ncurses package
# Colors: https://www.ditig.com/publications/256-colors-cheat-sheet
readonly OLIVE=$(tput setaf 3)
readonly RED=$(tput setaf 9)
readonly GREEN=$(tput setaf 2)
readonly RESET=$(tput sgr0)
fi
display_help() {
cat << EOF
Termux-NF: A Better way to install NerdFonts on Termux
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
-i <font> directly install the specified Nerd Fonts
-u <font> uninstall the specified Nerd Fonts
-U update all installed Nerd Fonts
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${OLIVE}%b${RESET}\n" "$1"
}
confirm() {
printf "${GREEN}%b${RESET}\n" "$1"
}
alert() {
printf "${RED}%b${RESET}\n" "$1"
}
error() {
alert "$1" >&2
exit 1
}
ensure_installed() {
if ! command -v "$1" > /dev/null; then
error "Dependency $1 is not installed on your system."
fi
}
create_dirs() {
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo mkdir -p "$FONT_DIR"
else
mkdir -p "$FONT_DIR"
fi
mkdir -p "$DOWN_DIR"
mkdir -p "$FONT_RELEASES_DIR"
}
verify_internet_access() {
if ! curl --silent --head --fail "$NERDFONTSAPI" > /dev/null; then
alert "Couldn't connect to the Nerd Fonts API." >&2
error "Most likely the internet connection is not working."
fi
}
check_dependencies() {
ensure_installed curl
verify_internet_access
create_dirs
}
erase_line() {
# Move up a line and erase
printf "\033[1A\033[2K"
}
erase_char() {
# Move left one char and erase
printf "\033[1D\033[K"
}
###############################################################################
# Animation Functions
# Credit: https://github.com/Silejonu/bash_loading_animations
#
# Usage:
# Run `stop_loading_animation` if the script is interrupted:
#trap stop_loading_animation SIGINT
# Show a loading animation for the command "foo"
#start_loading_animation
#foo
#stop_loading_animation
# Don't forget to redirect the output from "foo"
readonly ANIMATION=('-' "\\" '|' '/')
readonly FRAME_INTERVAL=0.25
play_loading_animation_loop() {
while true; do
for frame in "${ANIMATION[@]}"; do
printf "\033[1D\033[K"
printf "%s" "${frame}"
sleep "$FRAME_INTERVAL"
done
done
}
start_loading_animation() {
if command -v tput > /dev/null; then
tput civis # Hide the terminal cursor
fi
play_loading_animation_loop &
LOADING_ANIMATION_PID="${!}"
}
stop_loading_animation() {
erase_char
kill "${LOADING_ANIMATION_PID}" &> /dev/null
if command -v tput > /dev/null; then
tput cnorm # Restore the terminal cursor
fi
}
###############################################################################
#######################################
# Create the global RELEASE with the latest release version and
# save it in the RELEASE_FILE. If there's a new release available,
# set NEW_RELEASE_AVAILABLE to true.
# Globals:
# NERDFONTSAPI
# RELEASE_FILE
# NEW_RELEASE_AVAILABLE
# Arguments:
# None
#######################################
get_latest_release_version() {
readonly RELEASE=$(curl --silent "$NERDFONTSAPI/releases/latest" |
awk -v RS=',' -F'"' '/tag_name/ {print $4}')
if [[ -f "$RELEASE_FILE" ]]; then
local current_release=$(cat "$RELEASE_FILE")
if [[ "$current_release" != "$RELEASE" ]]; then
NEW_RELEASE_AVAILABLE='true'
fi
fi
echo "$RELEASE" > "$RELEASE_FILE"
}
#######################################
# Get the name of the file which saves the release version of the given font
# Globals:
# OPERATE_GLOBALLY
# FONT_RELEASES_DIR
# Arguments:
# Font name
# Outputs:
# Writes the name of the file 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 if the font version is saved and is the latest
# Globals:
# RELEASE
# Arguments:
# Font name
# Returns:
# 0 if font is of the latest version, 1 otherwise
#######################################
is_latest_version() {
local font_release_file="$(get_font_release_file "$1")"
if [[ -f "$font_release_file" ]]; then
local font_release_version=$(cat "$font_release_file")
if [[ "$font_release_version" == "$RELEASE" ]]; then
return 0
fi
fi
return 1
}
#######################################
# Create a local file with all fonts from the Nerd Fonts repo.
# We can do this since the list only changes with a new release.
# Globals:
# NERDFONTSAPI
# Arguments:
# None
#######################################
create_all_fonts_file() {
local font_list
font_list=$(curl -s "$NERDFONTSAPI/contents/patched-fonts?ref=master" |
awk -v RS=',' -F'"' '/name/ {print $4}')
echo "$font_list" > "$ALL_FONTS_FILE"
}
#######################################
# Create the global array ALL_FONTS with all fonts from the Nerd Fonts repo
# Globals:
# ALL_FONTS_FILE
# NEW_RELEASE_AVAILABLE
# Arguments:
# None
#######################################
get_all_fonts() {
if [[ ! -f "$ALL_FONTS_FILE" || "$NEW_RELEASE_AVAILABLE" == "true" ]]; then
create_all_fonts_file
fi
ALL_FONTS=()
#mapfile -t ALL_FONTS < "$ALL_FONTS_FILE" # not available on mac
while IFS= read -r line; do
ALL_FONTS+=("$line")
done < "$ALL_FONTS_FILE"
}
#######################################
# Create the global array INSTALLED_FONTS
# Globals:
# FONT_DIR
# ALL_FONTS
# Arguments:
# None
#######################################
get_installed_fonts() {
# list all fonts in FONT_DIR
local installed_user_fonts=("$FONT_DIR"/*)
# leave only the basenames
installed_user_fonts=("${installed_user_fonts[@]##*/}")
# filter Nerd Fonts
INSTALLED_FONTS=()
for font in "${ALL_FONTS[@]}"; do
if [[ " ${installed_user_fonts[*]} " == *" $font "* ]]; then
INSTALLED_FONTS+=("$font")
fi
done
}
#######################################
# Create the global array FONT_OPTIONS by removing installed fonts
# from the list of all fonts if they are from current release
# or if the user does not want to force the update
# Globals:
# INCLUDE_INSTALLED_FONTS
# ALL_FONTS
# INSTALLED_FONTS
# Arguments:
# None
#######################################
filter_installed_fonts() {
if [[ "$INCLUDE_INSTALLED_FONTS" == "false" ]]; then
FONT_OPTIONS=()
for font in "${ALL_FONTS[@]}"; do
# add the font even if it's installed but the font_release_file
# doesn't exists or the version is not the latest
if [[ " ${INSTALLED_FONTS[*]} " != *" $font "* ]] ||
! is_latest_version "$font"; then
FONT_OPTIONS+=("$font")
fi
done
else
FONT_OPTIONS=("${ALL_FONTS[@]}")
fi
}
#######################################
# Download the font archive
# Globals:
# NERDFONTSREPO
# RELEASE
# Arguments:
# Font name
#######################################
download_font() {
local opts=("--location" "--remote-header-name" "--remote-name")
if [[ $ON_MAC == "false" ]]; then
opts+=("--silent")
echo -n "Downloading $1... "
trap stop_loading_animation SIGINT
start_loading_animation
curl "${opts[@]}" "$NERDFONTSREPO/releases/download/$RELEASE/$1.tar.xz"
stop_loading_animation
confirm "Done"
else
# TODO: figure out how to disable messages about bg process termination
opts+=("--progress-bar")
info "Downloading $1... "
curl "${opts[@]}" "$NERDFONTSREPO/releases/download/$RELEASE/$1.tar.xz"
erase_line
erase_line
echo "Downloading $1... ${GREEN}Done${RESET}"
fi
}
#######################################
# Extract the font archive to the user fonts directory
# Globals:
# OPERATE_GLOBALLY
# FONT_DIR
# Arguments:
# Font name
#######################################
extract_font() {
echo -n "Installing $1... "
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo mkdir -p "$FONT_DIR/$1"
sudo tar -xf "$1.tar.xz" -C "$FONT_DIR/$1"
else
mkdir -p "$FONT_DIR/$1"
tar -xf "$1.tar.xz" -C "$FONT_DIR/$1"
fi
confirm "Done"
}
#######################################
# Save the release version of the given font
# Globals:
# RELEASE
# Arguments:
# Font name
#######################################
save_font_release() {
echo "$RELEASE" > "$(get_font_release_file "$1")"
}
#######################################
# Remove the downloaded font archives
# Globals:
# DOWN_DIR
# SELECTED_FONTS
# Arguments:
# None
#######################################
remove_font_archives() {
echo -n "Removing downloaded font archives... "
for font in "${SELECTED_FONTS[@]}"; do
rm -f "$DOWN_DIR/$font.tar.xz"
done
confirm "Done"
}
#######################################
# Install the selected font
# Globals:
# DOWN_DIR
# INSTALLED_FONTS_FILE
# Arguments:
# Font name
#######################################
install_font() {
pushd "$DOWN_DIR" > /dev/null
# Remove the archive if it exists due to curl not having overwrite option
rm -f "$DOWN_DIR/$1.tar.xz"
download_font "$1" &&
extract_font "$1" &&
save_font_release "$1"
popd > /dev/null
}
#######################################
# Parse the string of font names and add them
# to SELECTED_FONTS if all of them are valid
# Globals:
# ALL_FONTS
# SELECTED_FONTS
# Arguments:
# String of font names
#######################################
direct_install() {
for font_name in $(echo "$1" | tr ',' ' '); do
if [[ " ${ALL_FONTS[*]} " == *" $font_name "* ]]; then
SELECTED_FONTS+=("$font_name")
else
error "Invalid font name: $font_name. Try again."
fi
done
}
#######################################
# Parse the string of font names and uninstall them if they are installed
# Globals:
# FONT_DIR
# OPERATE_GLOBALLY
# Arguments:
# String of font names
#######################################
uninstall_fonts() {
get_font_dir
local -a fonts_to_uninstall
for font_name in $(echo "$1" | tr ',' ' '); do
if [[ -d "$FONT_DIR/$font_name" ]]; then
fonts_to_uninstall+=("$font_name")
else
error "Font $font_name is not installed."
fi
done
if [[ ${#fonts_to_uninstall[@]} -ne 0 ]]; then
for font in "${fonts_to_uninstall[@]}"; do
echo -n "Uninstalling $font... "
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
sudo rm -rf "${FONT_DIR:?}/$font"
else
rm -rf "${FONT_DIR:?}/$font"
fi
rm -f "$(get_font_release_file "$font")"
confirm "Done"
done
fi
}
#######################################
# remove font archives unless specified otherwise
# Globals:
# KEEP_FONT_ARCHIVES
# Arguments:
# None
#######################################
post_install() {
if [[ "$KEEP_FONT_ARCHIVES" == "false" ]]; then
remove_font_archives
else
info "The downloaded font archives can be found in $DOWN_DIR"
fi
applynf
}
#######################################
# Add all installed Nerd Fonts to the SELECTED_FONTS array
# Globals:
# INSTALLED_FONTS
# SELECTED_FONTS
# OPERATE_GLOBALLY
# Arguments:
# None
#######################################
update_installed_fonts() {
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
error "No globally installed Nerd Fonts found."
else
error "No installed Nerd Fonts found."
fi
fi
}
#######################################
# Display installed fonts
# Globals:
# INSTALLED_FONTS
# OPERATE_GLOBALLY
# Arguments:
# None
# Returns:
# 1 if no installed Nerd Fonts found, 0 otherwise
#######################################
display_installed_fonts() {
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="$(get_font_release_file "$font")"
if [[ -f "$font_release_file" ]]; then
local 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 list of all fonts
# Globals:
# RELEASE
# ALL_FONTS_FILE
# Arguments:
# None
#######################################
display_all_fonts() {
title "Nerd Fonts ${RELEASE}:"
cat "$ALL_FONTS_FILE"
}
#######################################
# Display the font menu
# Globals:
# RELEASE
# FONT_OPTIONS
# Arguments:
# None
#######################################
display_font_menu() {
local term_width=$(stty size | cut -d ' ' -f 2)
title "Nerd Fonts ${RELEASE}:"
(
for i in "${!FONT_OPTIONS[@]}"; do
printf "%d) %s\n" "$((i + 1))" "${FONT_OPTIONS[$i]}"
done
printf "q) Quit\n"
) | pr -3 -t -w "$term_width"
}
#######################################
# Parse input range of format *-* (e.g. 1-3) and
# add the selected fonts to SELECTED_FONTS array
# Globals:
# FONT_OPTIONS
# SELECTED_FONTS
# Arguments:
# None
# Returns:
# 1 if input format is invalid or if the option is out of range
#######################################
parse_range() {
if ! [[ $1 =~ ^[0-9]+-[0-9]+$ ]]; then
alert "Invalid input format: $1. Expected format: X-Y."
return 1
fi
local -a range
IFS='-' read -ra range <<< "$1"
local range_start=${range[0]}
local range_end=${range[1]}
for ((i = range_start; i <= range_end; i++)); do
local index=$((i - 1))
if ((index >= 0 && index < ${#FONT_OPTIONS[@]})); then
SELECTED_FONTS[index]=${FONT_OPTIONS[index]}
else
alert "Invalid option: $i. Try again."
return 1
fi
done
}
#######################################
# Get and parse user input and add the selected fonts to SELECTED_FONTS array
# Globals:
# FONT_OPTIONS
# SELECTED_FONTS
# Arguments:
# None
#######################################
handle_user_input() {
while true; do
local choices
read -rp "Enter font number(s) (e.g. 1,2,3 or 1-3 or 1,3-5): " choices
for choice in $(echo "$choices" | tr ',' ' '); do
if [[ $choice == "q" ]]; then
confirm "Goodbye!"
exit 0
# Choice is a range (e.g. 1-3)
elif [[ $choice == *-* ]]; then
parse_range "$choice" || continue 2
elif ((choice >= 1 && choice <= ${#FONT_OPTIONS[@]})); then
local index=$((choice - 1))
SELECTED_FONTS[index]=${FONT_OPTIONS[index]}
else
alert "Invalid option: $choice. Try again."
continue 2
fi
done
title "Selected Nerd Fonts: ${SELECTED_FONTS[*]}"
break
done
}
#######################################
# Run the nerd font installation menu
# Arguments:
# None
#######################################
menu_install() {
get_installed_fonts
filter_installed_fonts
display_installed_fonts
display_font_menu
handle_user_input
}
main() {
while getopts ":hkaglLi:u:UV" option; do
case "${option}" in
h) display_help && exit 0 ;;
k) readonly KEEP_FONT_ARCHIVES='true' ;;
a) readonly INCLUDE_INSTALLED_FONTS='true' ;;
g) readonly OPERATE_GLOBALLY='true' ;;
l) readonly DISPLAY_INSTALLED_FONTS='true' ;;
L) readonly DISPLAY_ALL_FONTS='true' ;;
i) readonly FONTNAMES="$OPTARG" ;;
u) uninstall_fonts "$OPTARG" && exit 0 ;;
U) readonly UPDATE_INSTALLED_FONTS='true' ;;
:) error "Option '-$OPTARG' requires at least one font name." ;;
*) display_help && exit 0 ;;
esac
done
shift $((OPTIND - 1))
check_dependencies
get_latest_release_version
get_all_fonts
if [[ "$DISPLAY_INSTALLED_FONTS" == "true" ]]; then
get_installed_fonts
if ! display_installed_fonts; then
if [[ "$OPERATE_GLOBALLY" == "true" ]]; then
info "No globally installed Nerd Fonts found."
else
info "No installed Nerd Fonts found."
fi
fi
exit 0
fi
if [[ "$DISPLAY_ALL_FONTS" == "true" ]]; then
display_all_fonts
exit 0
fi
SELECTED_FONTS=()
if [[ "$UPDATE_INSTALLED_FONTS" == "true" ]]; then
update_installed_fonts
elif [[ -n "$FONTNAMES" ]]; then
direct_install "$FONTNAMES"
else
menu_install
fi
if ((${#SELECTED_FONTS[@]} > 0)); then
for i in "${SELECTED_FONTS[@]}"; do
install_font "$i"
done
post_install
else
error "No fonts were selected, exiting."
fi
}
main "$@"