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 #!/usr/bin/env bash
LOGFILE="/var/log/btrfs-backup.log" SNAP_DATE=$(date '+%Y-%m-%d_%H-%M-%S')
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
# Check if device is mounted # Check if device is mounted
if ! grep "/srv/backup" /etc/mtab >/dev/null; then if ! grep "/mnt/storage" /etc/mtab >/dev/null; then
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Backup device is not mounted." | tee -a "$LOGFILE" logger -p error -t btrfs_backup "ERROR: /mnt/storage is not mounted."
notify-send -i computer-fail "Backup device is not mounted"
exit 1 exit 1
fi fi

View File

@ -22,20 +22,22 @@ set -o posix
# Check for dependencies: curl and gum. # Check for dependencies: curl and gum.
if ! test -x "$(command -v curl)"; then if ! test -x "$(command -v curl)"; then
echo "Missing dependencies: please install curl." echo "Missing dependencies: curl"
exit 1 exit 1
fi fi
if ! test -x "$(command -v gum)"; then 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" echo "See https://github.com/charmbracelet/gum"
exit 1 exit 1
fi fi
# Define variables if ! test -x "$(command -v unzip)"; then
NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download" echo "Missing dependencies: unzip"
NF_VERSION=$(curl -sL https://github.com/ryanoasis/nerd-fonts/releases/latest | grep "<title>Release" | awk '{ print $2 }') exit 1
NF_URL="${NF_BASE_URL}/${NF_VERSION}" fi
# Directory on the local filesystem where the fonts will be installed.
LOCAL_FONT_DIR="${HOME}/.local/share/fonts" LOCAL_FONT_DIR="${HOME}/.local/share/fonts"
# Fancy error output message. # Fancy error output message.
@ -53,63 +55,6 @@ gum_error() {
exit 1 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. # Print the startup message.
message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji) message=$(echo "Nerd font installer :nerd_face:" | gum format -t emoji)
gum style \ gum style \
@ -121,10 +66,24 @@ gum style \
--margin "1 2" \ --margin "1 2" \
"$message" "$message"
# Print the `nf_array` line by line and pipe to `gum choose --no-limit`. # Read the nerd-font download URLs into an array
# Create `selection` array from the output of `gum choose --no-limit`. 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")
# It will contain only the items that were selected by the user.
mapfile -t selection < <(printf "%s\n" "${nf_array[@]}" | gum choose --no-limit) # 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. # 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 user declines to proceed with installation, print a cancel message.
if gum confirm "Proceed with installation?"; then if gum confirm "Proceed with installation?"; then
for item in "${selection[@]}"; do if ! gum spin --spinner dot --title "Downloading $selection..." \
if ! gum spin --spinner dot --title "Downloading $item..." \ -- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$selection.zip" "${nf_array["$selection"]}"; then
-- curl --create-dirs -f -sL -o "${LOCAL_FONT_DIR}/$item.zip" "${NF_URL}/$item.zip"; then gum_error "Failed to download nerdfont archive $selection"
gum_error "Failed to download nerd font archive $item"
fi fi
if ! gum spin --spinner dot --title "Installing $item..." \ if ! gum spin --spinner dot --title "Installing $selection..." \
-- unzip -uo "${LOCAL_FONT_DIR}/$item.zip" -d "${LOCAL_FONT_DIR}"; then -- unzip -uo "${LOCAL_FONT_DIR}/$selection.zip" -d "${LOCAL_FONT_DIR}"; then
gum_error "Failed to install nerd font archive $item" gum_error "Failed to install nerdfont archive $selection"
fi fi
done
else else
gum style \ gum style \
--foreground 212 \ --foreground 212 \