forked from MunifTanjim/scripts.sh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-nerd-font
executable file
·101 lines (77 loc) · 2.17 KB
/
setup-nerd-font
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
#!/usr/bin/env bash
set -euo pipefail
declare -r NERD_FONTS_DIR="${HOME}/.fonts/nerd-fonts"
mkdir -p "${NERD_FONTS_DIR}"
declare -A NERD_FONTS=()
declare font=""
_get_font_list() {
local api_url="https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest"
local urls
IFS=$'\n' read -r -d '' -a urls \
< <(set -o pipefail; curl -sf "${api_url}" | jq -r --arg pattern ".zip" '.assets | map(.browser_download_url) | map(select(. | test($pattern))) | .[]' && printf '\0')
local _font
for url in "${urls[@]}"; do
_font=${url##*/}
_font=${_font/.zip/}
NERD_FONTS["${_font}"]="${url}"
done
}
_select_font() {
PS3='Select Font: '
select _font in "${!NERD_FONTS[@]}"; do
if [[ -n "${_font}" ]]; then
if [[ -n "${NERD_FONTS[${_font}]}" ]]; then
font="${_font}"
break
fi
fi
done
}
_download_font() {
cd "${NERD_FONTS_DIR}"
echo "Downloading font: ${font}"
local -r url="${NERD_FONTS[${font}]}"
wget --quiet --show-progress "${url}" -O "./${font}.zip"
echo "Extracting archive..."
unzip -o -q "./${font}.zip" -d "${font}"
}
_checkout_font() {
local -r temp_dir="$(mktemp --directory /tmp/nerd-fonts--XXXXXXXXX)"
echo "Checking-out font: ${font}"
git clone --filter=blob:none --sparse https://github.com/ryanoasis/nerd-fonts.git "${temp_dir}"
pushd "${temp_dir}" >/dev/null
git sparse-checkout add "patched-fonts/${font}"
popd >/dev/null
echo "Copying files..."
mkdir -p "${NERD_FONTS_DIR}/${font}"
find "${temp_dir}/patched-fonts/${font}" -type f -iname "*.ttf" -exec cp {} "${NERD_FONTS_DIR}/${font}/" \;
rm -rf "${temp_dir}"
}
_update_font_cache() {
echo "Updating font cache..."
sudo fc-cache -f
}
_git_supports_sparse_checkout() {
local version="$(git --version)"
version="${version##git version }"
local major="${version%%.*}"
local minor="${version/${major}./}"
minor="${minor%%.*}"
local patch="${version/${major}.${minor}./}"
if [[ $major -lt 2 ]]; then
return 1
fi
if [[ $minor -lt 26 ]]; then
return 1
fi
return 0
}
_get_font_list
_select_font
if _git_supports_sparse_checkout; then
_checkout_font
else
_download_font
fi
_update_font_cache
echo "Done!"