Add new nerdfont_installer

This commit is contained in:
Jeffrey Serio 2022-11-26 22:29:27 -06:00
parent a18642d82c
commit 83f24d5ff3

View File

@ -1,5 +1,4 @@
# Nerd font installler #!/usr/bin/env bash
# Usage: python3 nerdfont_installer.py
# LICENSE # LICENSE
# Copyright 2022 Jeffrey Serio <hyperreal@fedoraproject.org> # Copyright 2022 Jeffrey Serio <hyperreal@fedoraproject.org>
@ -17,122 +16,158 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
import os.path set -eu
import sys set -o pipefail
import urllib.error set -o posix
import urllib.request
import zipfile
if sys.platform.startswith("win32") or sys.platform.startswith("cygwin"): # Check for dependencies: curl and gum.
print("🔥 Sadly, this script only works on Linux/Unix systems for now. Sorries 🙁.") if ! test -x "$(command -v curl)"; then
os.exit(1) echo "Missing dependencies: please install curl."
exit 1
fi
if ! test -x "$(command -v gum)"; then
echo "Missing dependencies: please install the gum command."
echo "See https://github.com/charmbracelet/gum"
exit 1
fi
# Define variables
NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download" NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download"
NF_VERSION = "v2.1.0" NF_VERSION=$(curl -sL https://github.com/ryanoasis/nerd-fonts/releases/latest | grep "<title>Release" | awk '{ print $2 }')
LOCAL_FONT_DIR = os.getenv("HOME") + "/.local/share/fonts" NF_URL="${NF_BASE_URL}/${NF_VERSION}"
LOCAL_FONT_DIR="${HOME}/.local/share/fonts"
nf_dict = { # Fancy error output message.
1: "3270", gum_error() {
2: "Agave", gum style \
3: "AnonymousPro", --foreground 3 \
4: "Arimo", --border-foreground 203 \
5: "AurulentSansMono", --border rounded \
6: "BigBlueTerminal", --align center \
7: "BitstreamVeraSansMono", --width 50 \
8: "CascadiaCode", --margin "1 2" \
9: "CodeNewRoman", "ERROR" \
10: "Cousine", "" \
11: "DaddyTimeMono", "$1"
12: "DejaVuSansMono", exit 1
13: "DroidSansMono",
14: "FantasqueSansMono",
15: "FiraCode",
16: "FiraMono",
17: "Go-Mono",
18: "Gohu",
19: "Hack",
20: "Hasklig",
21: "HeavyData",
22: "Hermit",
23: "iA-Writer",
24: "IBMPlexMono",
25: "Inconsolata",
26: "InconsolataGo",
27: "InconsolataLGC",
28: "Iosevka",
29: "JetBrainsMono",
30: "Lekton",
31: "LiberationMono",
32: "Meslo",
33: "Monofur",
34: "Monoid",
35: "Mononoki",
36: "MPlus",
37: "Noto",
38: "OpenDyslexic",
39: "Overpass",
40: "ProFont",
41: "ProggyClean",
42: "RobotoMono",
43: "ShareTechMono",
44: "SourceCodePro",
45: "SpaceMono",
46: "Terminus",
47: "Tinos",
48: "Ubuntu",
49: "UbuntuMono",
50: "VictorMono",
} }
try: # Array of nerd font names.
for key, value in nf_dict.items(): nf_array=(
print("%d) %s" % (key, value)) 3270
Agave
selection = int(input("\nSelection: ")) AnonymousPro
Arimo
if not os.path.exists(LOCAL_FONT_DIR): AurulentSansMono
os.mkdir(LOCAL_FONT_DIR) BigBlueTerminal
BitstreamVeraSansMono
if selection >= 1 and selection <= 50: CascadiaCode
print("⚡️ Downloading %s..." % nf_dict[selection]) CodeNewRoman
Cousine
url = "%s/%s/%s.zip" % (NF_BASE_URL, NF_VERSION, nf_dict[selection]) DaddyTimeMono
save_path = "%s/%s.zip" % (LOCAL_FONT_DIR, nf_dict[selection]) DejaVuSansMono
DroidSansMono
with urllib.request.urlopen(url) as in_file: FantasqueSansMono
with open(save_path, "wb") as out_file: FiraCode
out_file.write(in_file.read()) FiraMono
Go-Mono
print("🗃️ Extracting the archive...") Gohu
with zipfile.ZipFile(save_path, "r") as z: Hack
z.extractall(LOCAL_FONT_DIR) Hasklig
HeavyData
print("🗑️ Cleaning up...") Hermit
for item in os.listdir(LOCAL_FONT_DIR): iA-Writer
if ".zip" in item: IBMPlexMono
os.remove(LOCAL_FONT_DIR + "/%s" % item) Inconsolata
InconsolataGo
if ".otf" in item: InconsolataLGC
os.remove(LOCAL_FONT_DIR + "/%s" % item) Iosevka
JetBrainsMono
if "Windows Compatible.ttf" in item: Lekton
os.remove(LOCAL_FONT_DIR + "/%s" % item) LiberationMono
Meslo
if "Complete.ttf" in item: Monofur
os.remove(LOCAL_FONT_DIR + "/%s" % item) Monoid
else: Mononoki
print( MPlus
"💥 Your selection must be between and including 1 to 50. Like, obviously." Noto
OpenDyslexic
Overpass
ProFont
ProggyClean
RobotoMono
ShareTechMono
SourceCodePro
SpaceMono
Terminus
Tinos
Ubuntu
UbuntuMono
VictorMono
) )
os.exit(1)
except ValueError: # Print the startup message.
print("💥 Whatever input you entered was not an integer.") message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji)
except urllib.error.URLError as url_err: gum style \
print("💥 Something weird happened while trying to download the archive.") --foreground 212 \
print(url_err.strerror) --border-foreground 57 \
except zipfile.BadZipFile as badzip_err: --border rounded \
print("💥 Something weird happened while trying to unzip the archive.") --align center \
print(badzip_err.with_traceback) --width 50 \
except OSError as os_err: --margin "1 2" \
print("💥 Something weird happened while running this program.") "$message"
print("%s: %s" % (os_err.filename, os_err.strerror))
# Print the `nf_array` line by line and pipe to `gum choose --no-limit`.
# Create `selection` array from the output of `gum choose --no-limit`.
# It will contain only the items that were selected by the user.
selection=($(printf "%s\n" "${nf_array[@]}" | gum choose --no-limit))
# Prompt for user confirmation and proceed with installation of nerd fonts.
#
# For each nerd font selected, print a status message while downloading and
# installing the nerd font. Else print an error message if any of it fails.
#
# If user declines to proceed with installation, print a cancel message.
if gum confirm "Proceed with installation?"; then
for item in "${selection[@]}"; do
if ! gum spin --spinner dot --title "Downloading $item..." \
-- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$item.zip" "${NF_URL}/$item.zip"; then
gum_error "Failed to download nerd font archive $item"
fi
if ! gum spin --spinner dot --title "Installing $item..." \
-- unzip -uo "${LOCAL_FONT_DIR}/$item.zip" -d "${LOCAL_FONT_DIR}"; then
gum_error "Failed to install nerd font archive $item"
fi
done
else
gum style \
--foreground 212 \
--border-foreground 57 \
--border rounded \
--align center \
--width 50 \
--margin "1 2" \
"Nerd font installation cancelled"
fi
# Clean up local font directory. Removes everything besides fonts.
if ! gum spin --spinner dot --title "Cleaning up local font directory..." \
-- find "${LOCAL_FONT_DIR}" -mindepth 1 \
-not -name "*.otf" \
-not -name "*.ttf" \
-not -name "static" \
-exec rm -rf {} \; ; then
gum_error "Failed to clean up local font directory. Try doing it manually."
fi
# Update font cache
if ! gum spin --spinner dot --title "Updating font cache..." \
-- fc-cache -f; then
gum_error "Failed to update font cache."
fi
# Print a message stating which nerd fonts were installed.
gum format -t markdown -- \
"# Successfully installed" \
"$(printf "* %s\n" "${selection[@]}")"