Refactor nerdfont_installer

This commit is contained in:
Jeffrey Serio 2024-10-08 02:42:38 -05:00
parent 4c8f40e208
commit 3dd94bf2c3
14 changed files with 37 additions and 88 deletions

View File

@ -1,18 +1,10 @@
#!/usr/bin/env bash
LOGFILE="/var/log/btrfs-backup.log"
SNAP_DATE=$(date '+%Y-%m-%d_%H%M%S')
send_matrix_webhook() {
/usr/local/bin/send-matrix-webhook "btrfs-backup failed on $(hostname)"
}
trap send_matrix_webhook SIGINT SIGTERM
SNAP_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
# Check if device is mounted
if ! grep "/srv/backup" /etc/mtab >/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Backup device is not mounted." | tee -a "$LOGFILE"
notify-send -i computer-fail "Backup device is not mounted"
if ! grep "/mnt/storage" /etc/mtab >/dev/null; then
logger -p error -t btrfs_backup "ERROR: /mnt/storage is not mounted."
exit 1
fi

View File

@ -22,20 +22,22 @@ set -o posix
# Check for dependencies: curl and gum.
if ! test -x "$(command -v curl)"; then
echo "Missing dependencies: please install curl."
echo "Missing dependencies: curl"
exit 1
fi
if ! test -x "$(command -v gum)"; then
echo "Missing dependencies: please install the gum command."
echo "Missing dependencies: gum"
echo "See https://github.com/charmbracelet/gum"
exit 1
fi
# Define variables
NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download"
NF_VERSION=$(curl -sL https://github.com/ryanoasis/nerd-fonts/releases/latest | grep "<title>Release" | awk '{ print $2 }')
NF_URL="${NF_BASE_URL}/${NF_VERSION}"
if ! test -x "$(command -v unzip)"; then
echo "Missing dependencies: unzip"
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.
@ -53,63 +55,6 @@ gum_error() {
exit 1
}
# Array of nerd font names.
nf_array=(
3270
Agave
AnonymousPro
Arimo
AurulentSansMono
BigBlueTerminal
BitstreamVeraSansMono
CascadiaCode
CodeNewRoman
Cousine
DaddyTimeMono
DejaVuSansMono
DroidSansMono
EnvyCodeR
FantasqueSansMono
FiraCode
FiraMono
Go-Mono
Gohu
Hack
Hasklig
HeavyData
Hermit
iA-Writer
IBMPlexMono
Inconsolata
InconsolataGo
InconsolataLGC
IntelOneMono
Iosevka
JetBrainsMono
Lekton
LiberationMono
Lilex
Meslo
Monofur
Monoid
Mononoki
MPlus
Noto
OpenDyslexic
Overpass
ProFont
ProggyClean
RobotoMono
ShareTechMono
SourceCodePro
SpaceMono
Terminus
Tinos
Ubuntu
UbuntuMono
VictorMono
)
# Print the startup message.
message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji)
gum style \
@ -121,10 +66,24 @@ gum style \
--margin "1 2" \
"$message"
# 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.
mapfile -t selection < <(printf "%s\n" "${nf_array[@]}" | gum choose --no-limit)
# 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.
#
@ -133,16 +92,14 @@ mapfile -t selection < <(printf "%s\n" "${nf_array[@]}" | gum choose --no-limit)
#
# 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"
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 $item..." \
-- unzip -uo "${LOCAL_FONT_DIR}/$item.zip" -d "${LOCAL_FONT_DIR}"; then
gum_error "Failed to install nerd font archive $item"
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
done
else
gum style \
--foreground 212 \