Skip to content

Instantly share code, notes, and snippets.

@yashodhank
Last active October 9, 2023 06:29
Show Gist options
  • Save yashodhank/1a9d94115f087dd59fba39dfc634dde6 to your computer and use it in GitHub Desktop.
Save yashodhank/1a9d94115f087dd59fba39dfc634dde6 to your computer and use it in GitHub Desktop.
Updated Nerd Fonts Downloader with CLI arguments and error handling

Nerd Fonts Downloader Script

This script allows you to download and install Nerd Fonts with ease, either by specifying the font name(s) as command-line arguments or by selecting from a provided list if no arguments are given.

Prerequisites

Ensure that curl or wget, tar, and fc-cache (from fontconfig) are installed on your system.

For Ubuntu/Debian-based Systems:

sudo apt update
sudo apt install curl wget tar fontconfig

For Red Hat/Fedora-based Systems:

sudo dnf install curl wget tar fontconfig

Usage

1. Direct Font Download

You can directly download one or multiple fonts by specifying their name(s) as command-line arguments.

Example:

bash -c "$(curl -fsSL https://gist.github.com/yashodhank/1a9d94115f087dd59fba39dfc634dde6/raw/nerd-font-installer.sh)" -- FontName1 FontName2 ... FontNameN

Real Example to install NerdFontsSymbolsOnly, Terminus, SourceCodePro, FiraCode and Hack Fonts using CLI ARGs method:

bash -c "$(curl -fsSL https://gist.github.com/yashodhank/1a9d94115f087dd59fba39dfc634dde6/raw/nerd-font-installer.sh)" -- Hack FiraCode SourceCodePro Terminus NerdFontsSymbolsOnly

Ensure that the specified font name(s) match with the actual names used in the download URLs on the Nerd Fonts GitHub releases page.

2. Interactive Font Selection

If you do not provide any command-line arguments, the script will present you with a list of available fonts to choose from.

Example:

bash -c "$(curl -fsSL https://gist.github.com/yashodhank/1a9d94115f087dd59fba39dfc634dde6/raw/nerd-font-installer.sh)"

Follow the interactive prompts to select and download a font.

  1. Run the Script: Use one of the usage methods described above to run the script and download Nerd Fonts.

Troubleshooting

  • Command Not Found Error: If you encounter a "command not found" error, ensure that the script is made executable and that you are in the correct directory.

  • Download Failures: If a font fails to download, double-check the spelling of the font name and ensure it matches the name used in the Nerd Fonts GitHub releases page.

  • Permission Denied: If you encounter a "permission denied" error, ensure you have write access to the installation directory (~/.fonts/ by default). You may need to adjust your directory permissions.

Disclaimer

This script is provided with no warranty. Ensure to review and test in a safe environment before using it in a critical system.

#!/bin/bash
# Function to install prerequisites
install_prerequisites() {
echo "Installing prerequisites..."
if [ -f /etc/os-release ]; then
. /etc/os-release
case $ID in
debian|ubuntu)
sudo apt-get update
sudo apt-get install -y curl wget tar fontconfig
;;
fedora)
sudo dnf install -y curl wget tar fontconfig
;;
centos|rocky)
sudo yum install -y curl wget tar fontconfig
;;
opensuse*)
sudo zypper install -y curl wget tar fontconfig
;;
alpine)
sudo apk --no-cache add curl wget tar fontconfig
;;
*)
echo "Unsupported OS. Please install curl, wget, tar, and fontconfig manually."
exit 1
;;
esac
else
echo "Unable to detect OS. Please install curl, wget, tar, and fontconfig manually."
exit 1
fi
}
# Ensure `curl` or `wget`, `tar`, and `fc-cache` are installed
if ! [ "$(command -v curl)" ] || ! [ "$(command -v wget)" ] || ! [ "$(command -v tar)" ] || ! [ "$(command -v fc-cache)" ]; then
echo "Prerequisites (curl/wget, tar, fc-cache) not found. Attempting to install..."
install_prerequisites
fi
# Creating fonts folder if not exists
mkdir -p "${HOME}/.fonts"
download_and_install_font() {
font_name=$1
download_url="https://github.com/ryanoasis/nerd-fonts/releases/latest/download/${font_name}.tar.xz"
if [ "$(command -v curl)" ]; then
curl -sSOL "$download_url"
elif [ "$(command -v wget)" ]; then
wget -q "$download_url"
fi
if [ "$?" -ne 0 ]; then
echo "Error: Download failed for ${font_name}. Check if the font name is correct."
return 1
fi
mkdir -p "${HOME}/.fonts/${font_name}/"
tar -xf "${font_name}.tar.xz" -C "${HOME}/.fonts/${font_name}/"
fc-cache -fv 2>&1 > /dev/null # Suppressing output but showing errors
if [ "$?" -ne 0 ]; then
echo "Error: Extraction failed for ${font_name}. Skipping to the next font."
return 1
fi
rm "${font_name}.tar.xz"
echo "[-] ${font_name} Nerd Font installed successfully! [-]"
}
fons_list=("3270" "Agave" "AnonymousPro" "Arimo" "AurulentSansMono" "BigBlueTerminal" "BitstreamVeraSansMono" "CascadiaCode" "CodeNewRoman" "ComicShannsMono" "Cousine" "DaddyTimeMono" "DejaVuSansMono" "DroidSansMono" "FantasqueSansMono" "FiraCode" "FiraMono" "Gohu" "Go-Mono" "Hack" "Hasklig" "HeavyData" "Hermit" "iA-Writer" "IBMPlexMono" "InconsolataGo" "InconsolataLGC" "Inconsolata" "IosevkaTerm" "Iosevka" "JetBrainsMono" "Lekton" "LiberationMono" "Lilex" "Meslo" "Monofur" "Monoid" "Mononoki" "MPlus" "NerdFontsSymbolsOnly" "Noto" "OpenDyslexic" "Overpass" "ProFont" "ProggyClean" "RobotoMono" "ShareTechMono" "SourceCodePro" "SpaceMono" "Terminus" "Tinos" "UbuntuMono" "Ubuntu" "VictorMono")
if [ "$#" -gt 0 ]; then
for font_name in "$@"; do
echo "[-] Downloading ${font_name} Nerd Font [-]"
download_and_install_font "$font_name"
done
else
if [ -t 0 ]; then # Check if script has a terminal
# Print available fonts in multi-column format
echo "Available Fonts:"
for i in "${!fons_list[@]}"; do
printf "%3d: %-20s" "$i" "${fons_list[$i]}"
if (( ($i + 1) % 3 == 0 )); then
echo
fi
done
echo
# Ask user to select fonts
echo "Enter the numbers of the fonts you want to install, separated by space (e.g., 1 3 5):"
read -a selected_indexes
# Confirm selection
echo "You've selected:"
for i in "${selected_indexes[@]}"; do
echo "$i: ${fons_list[$i]}"
done
echo "Proceed with installation? [y/N]"
read proceed
if [[ "$proceed" == "Y" || "$proceed" == "y" ]]; then
for i in "${selected_indexes[@]}"; do
echo "[-] Downloading ${fons_list[$i]} Nerd Font [-]"
download_and_install_font "${fons_list[$i]}"
done
else
echo "Installation aborted."
fi
else
echo "No arguments provided and no terminal available for interactive selection. Aborting."
exit 1
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment