python/dev/fonts.py

76 lines
3 KiB
Python
Executable file

#!/usr/bin/env python3
import os
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
from colorama import init, Fore, Style
import requests
# Initialize colorama
init(autoreset=True)
# List of font repositories
FONT_REPOS = {
"JetBrainsMono": "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.3.0/JetBrainsMono.zip",
"CaskaydiaCove": "https://github.com/ryanoasis/nerd-fonts/releases/download/v3.3.0/CascadiaCode.zip",
# Add more repositories here
}
def display_menu(options, existing_fonts):
print(Fore.CYAN + "Select Font Repositories to Download:")
for i, option in enumerate(options, 1):
icon = Fore.GREEN + "" if option in existing_fonts else Fore.RED + " "
print(f"{i}. {option} {icon}")
print(Fore.YELLOW + "0. Exit")
def get_user_selection(options):
selected_indices = []
while True:
try:
choices = input(Fore.YELLOW + "Enter the numbers of your choices separated by spaces (q to quit): ")
if choices.strip().lower() == "q":
break
indices = [int(choice.strip()) - 1 for choice in choices.split() if choice.strip().isdigit()]
for index in indices:
if 0 <= index < len(options):
selected_indices.append(index)
else:
print(Fore.RED + f"Invalid choice: {index + 1}. Please try again.")
except ValueError:
print(Fore.RED + "Invalid input. Please enter numbers separated by spaces.")
return selected_indices
def download_font(url, download_path):
response = requests.get(url)
with open(download_path, 'wb') as file:
file.write(response.content)
def clone_repos(selected_repos, base_clone_directory):
with ThreadPoolExecutor(max_workers=4) as executor:
future_to_repo = {
executor.submit(download_font, FONT_REPOS[repo], os.path.join(base_clone_directory, f"{repo}.zip")): repo
for repo in selected_repos
}
for future in as_completed(future_to_repo):
repo = future_to_repo[future]
try:
future.result()
print(Fore.GREEN + f"Successfully downloaded {repo}")
except Exception as e:
print(Fore.RED + f"Failed to download {repo}: {e}")
def check_existing_fonts(clone_directory):
return {repo for repo in FONT_REPOS if os.path.exists(os.path.join(clone_directory, f"{repo}.zip"))}
def main():
base_clone_directory = os.path.expanduser("~/.local/share/fonts/")
os.makedirs(base_clone_directory, exist_ok=True)
existing_fonts = check_existing_fonts(base_clone_directory)
options = list(FONT_REPOS.keys())
display_menu(options, existing_fonts)
selected_indices = get_user_selection(options)
selected_repos = [options[i] for i in selected_indices if options[i] not in existing_fonts]
clone_repos(selected_repos, base_clone_directory)
if __name__ == "__main__":
main()