mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-01 08:33:06 +01:00
139 lines
4.3 KiB
Bash
Executable File
139 lines
4.3 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# LICENSE
|
|
# Copyright 2022 Jeffrey Serio <hyperreal@fedoraproject.org>
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
set -eu
|
|
set -o pipefail
|
|
set -o posix
|
|
|
|
# Check for dependencies: curl and gum.
|
|
if ! test -x "$(command -v curl)"; then
|
|
echo "Missing dependencies: curl"
|
|
exit 1
|
|
fi
|
|
|
|
if ! test -x "$(command -v gum)"; then
|
|
echo "Missing dependencies: gum"
|
|
echo "See https://github.com/charmbracelet/gum"
|
|
exit 1
|
|
fi
|
|
|
|
if ! test -x "$(command -v unzip)"; then
|
|
echo "Missing dependencies: unzip"
|
|
exit 1
|
|
fi
|
|
|
|
if ! test -x "$(command -v jq)"; then
|
|
echo "Missing dependencies: jq"
|
|
exit 1
|
|
fi
|
|
|
|
# Directory on the local filesystem where the fonts will be installed.
|
|
LOCAL_FONT_DIR="${HOME}/.local/share/fonts"
|
|
|
|
# Fancy error output message.
|
|
gum_error() {
|
|
gum style \
|
|
--foreground 3 \
|
|
--border-foreground 203 \
|
|
--border rounded \
|
|
--align center \
|
|
--width 50 \
|
|
--margin "1 2" \
|
|
"ERROR" \
|
|
"" \
|
|
"$1"
|
|
exit 1
|
|
}
|
|
|
|
# Print the startup message.
|
|
message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji)
|
|
gum style \
|
|
--foreground 212 \
|
|
--border-foreground 57 \
|
|
--border rounded \
|
|
--align center \
|
|
--width 50 \
|
|
--margin "1 2" \
|
|
"$message"
|
|
|
|
# Read the nerd-font download URLs into an array
|
|
readarray -t nf_url_array < <(curl --silent "https://api.github.com/repos/ryanoasis/nerd-fonts/releases/latest" | jq '.assets.[].browser_download_url' | tr -d '"' | grep ".zip")
|
|
|
|
# Add the nerd-font basenames without file extension suffix into an associative
|
|
# array, using these as the keys and the download URLs as the values.
|
|
declare -A nf_array
|
|
for nf in "${nf_url_array[@]}"; do
|
|
name="$(basename -s .zip "$nf")"
|
|
nf_array[$name]="$nf"
|
|
done
|
|
|
|
# Sort the keys of the array.
|
|
IFS=$'\n'
|
|
readarray -t sorted_keys < <(sort <<<"${!nf_array[*]}")
|
|
unset IFS
|
|
|
|
# Display nerd-font selection menu with the keys of the associative array.
|
|
selection=$(gum choose --height=10 --limit=1 "${sorted_keys[@]}")
|
|
|
|
# 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
|
|
if ! gum spin --spinner dot --title "Downloading $selection..." \
|
|
-- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$selection.zip" "${nf_array["$selection"]}"; then
|
|
gum_error "Failed to download nerdfont archive $selection"
|
|
fi
|
|
if ! gum spin --spinner dot --title "Installing $selection..." \
|
|
-- unzip -uo "${LOCAL_FONT_DIR}/$selection.zip" -d "${LOCAL_FONT_DIR}"; then
|
|
gum_error "Failed to install nerdfont archive $selection"
|
|
fi
|
|
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[@]}")"
|