mirror of
https://codeberg.org/hyperreal/bin
synced 2024-11-25 10:23:42 +01:00
Add new nerdfont_installer
This commit is contained in:
parent
a18642d82c
commit
83f24d5ff3
@ -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
|
||||||
|
|
||||||
NF_BASE_URL = "https://github.com/ryanoasis/nerd-fonts/releases/download"
|
if ! test -x "$(command -v gum)"; then
|
||||||
NF_VERSION = "v2.1.0"
|
echo "Missing dependencies: please install the gum command."
|
||||||
LOCAL_FONT_DIR = os.getenv("HOME") + "/.local/share/fonts"
|
echo "See https://github.com/charmbracelet/gum"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
nf_dict = {
|
# Define variables
|
||||||
1: "3270",
|
NF_BASE_URL="https://github.com/ryanoasis/nerd-fonts/releases/download"
|
||||||
2: "Agave",
|
NF_VERSION=$(curl -sL https://github.com/ryanoasis/nerd-fonts/releases/latest | grep "<title>Release" | awk '{ print $2 }')
|
||||||
3: "AnonymousPro",
|
NF_URL="${NF_BASE_URL}/${NF_VERSION}"
|
||||||
4: "Arimo",
|
LOCAL_FONT_DIR="${HOME}/.local/share/fonts"
|
||||||
5: "AurulentSansMono",
|
|
||||||
6: "BigBlueTerminal",
|
# Fancy error output message.
|
||||||
7: "BitstreamVeraSansMono",
|
gum_error() {
|
||||||
8: "CascadiaCode",
|
gum style \
|
||||||
9: "CodeNewRoman",
|
--foreground 3 \
|
||||||
10: "Cousine",
|
--border-foreground 203 \
|
||||||
11: "DaddyTimeMono",
|
--border rounded \
|
||||||
12: "DejaVuSansMono",
|
--align center \
|
||||||
13: "DroidSansMono",
|
--width 50 \
|
||||||
14: "FantasqueSansMono",
|
--margin "1 2" \
|
||||||
15: "FiraCode",
|
"ERROR" \
|
||||||
16: "FiraMono",
|
"" \
|
||||||
17: "Go-Mono",
|
"$1"
|
||||||
18: "Gohu",
|
exit 1
|
||||||
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
|
||||||
|
AnonymousPro
|
||||||
|
Arimo
|
||||||
|
AurulentSansMono
|
||||||
|
BigBlueTerminal
|
||||||
|
BitstreamVeraSansMono
|
||||||
|
CascadiaCode
|
||||||
|
CodeNewRoman
|
||||||
|
Cousine
|
||||||
|
DaddyTimeMono
|
||||||
|
DejaVuSansMono
|
||||||
|
DroidSansMono
|
||||||
|
FantasqueSansMono
|
||||||
|
FiraCode
|
||||||
|
FiraMono
|
||||||
|
Go-Mono
|
||||||
|
Gohu
|
||||||
|
Hack
|
||||||
|
Hasklig
|
||||||
|
HeavyData
|
||||||
|
Hermit
|
||||||
|
iA-Writer
|
||||||
|
IBMPlexMono
|
||||||
|
Inconsolata
|
||||||
|
InconsolataGo
|
||||||
|
InconsolataLGC
|
||||||
|
Iosevka
|
||||||
|
JetBrainsMono
|
||||||
|
Lekton
|
||||||
|
LiberationMono
|
||||||
|
Meslo
|
||||||
|
Monofur
|
||||||
|
Monoid
|
||||||
|
Mononoki
|
||||||
|
MPlus
|
||||||
|
Noto
|
||||||
|
OpenDyslexic
|
||||||
|
Overpass
|
||||||
|
ProFont
|
||||||
|
ProggyClean
|
||||||
|
RobotoMono
|
||||||
|
ShareTechMono
|
||||||
|
SourceCodePro
|
||||||
|
SpaceMono
|
||||||
|
Terminus
|
||||||
|
Tinos
|
||||||
|
Ubuntu
|
||||||
|
UbuntuMono
|
||||||
|
VictorMono
|
||||||
|
)
|
||||||
|
|
||||||
selection = int(input("\nSelection: "))
|
# 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"
|
||||||
|
|
||||||
if not os.path.exists(LOCAL_FONT_DIR):
|
# Print the `nf_array` line by line and pipe to `gum choose --no-limit`.
|
||||||
os.mkdir(LOCAL_FONT_DIR)
|
# 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))
|
||||||
|
|
||||||
if selection >= 1 and selection <= 50:
|
# Prompt for user confirmation and proceed with installation of nerd fonts.
|
||||||
print("⚡️ Downloading %s..." % nf_dict[selection])
|
#
|
||||||
|
# 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
|
||||||
|
|
||||||
url = "%s/%s/%s.zip" % (NF_BASE_URL, NF_VERSION, nf_dict[selection])
|
# Clean up local font directory. Removes everything besides fonts.
|
||||||
save_path = "%s/%s.zip" % (LOCAL_FONT_DIR, nf_dict[selection])
|
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
|
||||||
|
|
||||||
with urllib.request.urlopen(url) as in_file:
|
# Update font cache
|
||||||
with open(save_path, "wb") as out_file:
|
if ! gum spin --spinner dot --title "Updating font cache..." \
|
||||||
out_file.write(in_file.read())
|
-- fc-cache -f; then
|
||||||
|
gum_error "Failed to update font cache."
|
||||||
|
fi
|
||||||
|
|
||||||
print("🗃️ Extracting the archive...")
|
# Print a message stating which nerd fonts were installed.
|
||||||
with zipfile.ZipFile(save_path, "r") as z:
|
gum format -t markdown -- \
|
||||||
z.extractall(LOCAL_FONT_DIR)
|
"# Successfully installed" \
|
||||||
|
"$(printf "* %s\n" "${selection[@]}")"
|
||||||
print("🗑️ Cleaning up...")
|
|
||||||
for item in os.listdir(LOCAL_FONT_DIR):
|
|
||||||
if ".zip" in item:
|
|
||||||
os.remove(LOCAL_FONT_DIR + "/%s" % item)
|
|
||||||
|
|
||||||
if ".otf" in item:
|
|
||||||
os.remove(LOCAL_FONT_DIR + "/%s" % item)
|
|
||||||
|
|
||||||
if "Windows Compatible.ttf" in item:
|
|
||||||
os.remove(LOCAL_FONT_DIR + "/%s" % item)
|
|
||||||
|
|
||||||
if "Complete.ttf" in item:
|
|
||||||
os.remove(LOCAL_FONT_DIR + "/%s" % item)
|
|
||||||
else:
|
|
||||||
print(
|
|
||||||
"💥 Your selection must be between and including 1 to 50. Like, obviously."
|
|
||||||
)
|
|
||||||
os.exit(1)
|
|
||||||
except ValueError:
|
|
||||||
print("💥 Whatever input you entered was not an integer.")
|
|
||||||
except urllib.error.URLError as url_err:
|
|
||||||
print("💥 Something weird happened while trying to download the archive.")
|
|
||||||
print(url_err.strerror)
|
|
||||||
except zipfile.BadZipFile as badzip_err:
|
|
||||||
print("💥 Something weird happened while trying to unzip the archive.")
|
|
||||||
print(badzip_err.with_traceback)
|
|
||||||
except OSError as os_err:
|
|
||||||
print("💥 Something weird happened while running this program.")
|
|
||||||
print("%s: %s" % (os_err.filename, os_err.strerror))
|
|
||||||
|
Loading…
Reference in New Issue
Block a user