From deb2bddbe18112cde5264d2529982a743610e00d Mon Sep 17 00:00:00 2001 From: Azalea Gui <22280294+hykilpikonna@users.noreply.github.com> Date: Tue, 10 Dec 2024 08:54:10 -0500 Subject: [PATCH] [+] Font logo --- crates/hyfetch/src/font_logo.rs | 47 +++++++++++++++++++++++++++++ crates/hyfetch/src/lib.rs | 1 + crates/hyfetch/src/neofetch_util.rs | 2 +- 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 crates/hyfetch/src/font_logo.rs diff --git a/crates/hyfetch/src/font_logo.rs b/crates/hyfetch/src/font_logo.rs new file mode 100644 index 00000000..5794a7c4 --- /dev/null +++ b/crates/hyfetch/src/font_logo.rs @@ -0,0 +1,47 @@ +use crate::neofetch_util::get_distro_name; +use crate::types::Backend; +use crate::utils::get_cache_path; +use anyhow::{Context, Result}; +use std::collections::HashMap; +use std::fs::{self, File}; +use std::io::{Read, Write}; + +const FONT_LOGOS: &str = r#"{"Alma":"","Alpine":"","AOSC OS":"","Apple":"","Archcraft":"","ArchLabs":"","Arch":"","Arco":"","Arduino":"","Artix":"","Awesome WM":"","Big":"","bspwm":"","Budgie":"","CentOS":"","Cinnamon":"","Codeberg":"","CoreOS":"","Crystal":"","Debian":"","Deepin":"","Devuan":"","Docker":"","dwm":"","elementary OS":"","Endeavour OS":"","Enlightenment":"","F-droid":"","Fedora":"","Fedora (inverse)":"","Ferris":"","Flathub":"","Fluxbox":"","Forgejo":"","FOSDEM":"","FreeBSD":"","FreeCAD":"","freedesktop.org":"","Garuda":"","Gentoo":"","GIMP":"","Gitea":"","GNOME":"","GNU Guix":"","GTK":"","Hyperbola -libre":"","Hyprland":"","i3":"","illumos":"","Inkscape":"","JWM":"","Kali":"","KDE":"","KDE Neon":"","KDE Plasma":"","Kdenlive":"","KiCad":"","Krita":"","Kubuntu":"","Kubuntu (inverse)":"","Mint":"","Mint (inverse)":"","Loc-OS":"","LXDE":"","LXLE":"","LXQt":"","Mageia":"","Mandriva":"","Manjaro":"","MATE":"","mpv":"","MX":"","Neovim":"","NixOS":"","Octoprint":"","OpenBSD":"","OpenSCAD":"","OpenSUSE":"","OSH":"","OSHWA":"","OSI":"","Parabola -libre":"","Parrot OS":"","Pop!_OS":"","PostmarketOS":"","Prusa Slicer":"","Puppy":"","Qt":"","Qtile":"","QubesOS":"","Raspberry pi":"","Red Hat":"","RepRap":"","RISC-V":"","Rocky":"","Sabayon":"","Slackware":"","Slackware (inverse)":"","Snappy":"","Solus":"","Sway":"","Tails":"","Thunderbird":"","Tor Browser":"","Trisquel":"","Tux":"","Ubuntu":"","Ubuntu (inverse)":"","Vanilla OS":"","Void":"","VS Codium":"","Wayland":"","Wikimedia":"","Xero":"","XFCE":"","Xmonad":"","Xorg":"","Zorin OS":"","Windows":""}"#; + +pub fn get_font_logo(backend: Backend) -> Result { + // Check if the cache file exists and return its contents if it does + let cache_path = get_cache_path().context("Failed to get cache path")?.join("font_logo"); + if cache_path.exists() { + let mut cached_logo = String::new(); + File::open(cache_path).context("Failed to open cache file")? + .read_to_string(&mut cached_logo).context("Failed to read from cache file")?; + return Ok(cached_logo); + } + + // Deserialize the JSON into a HashMap + let font_logos: HashMap = serde_json::from_str::>(FONT_LOGOS) + .context("Failed to deserialize font logos JSON file")? + .into_iter().map(|(k, v)| (k.to_lowercase(), v)).collect(); + + // Get the distro name + let distro = get_distro_name(backend).context("Failed to get distro name")?.to_lowercase(); + + // Find the most likely matching distro from font_logos + let matched_distro = font_logos.keys().find(|&k| distro.contains(k)) + .or_else(|| font_logos.keys().find(|k| k.contains(&distro))) + .or_else(|| font_logos.keys().find(|k| k.split_whitespace().any(|part| distro.contains(part)))) + .ok_or_else(|| anyhow::anyhow!("No font logo found for distro: {distro}. The supported logos are in https://github.com/Lukas-W/font-logos"))?; + + let logo = font_logos.get(matched_distro).unwrap(); + + // Create parent directories for the cache if they don't exist + if let Some(parent) = cache_path.parent() { + fs::create_dir_all(parent).context("Failed to create cache directory")?; + } + + // Write the logo to the cache file + let mut cache_file = File::create(cache_path).context("Failed to create cache file")?; + cache_file.write_all(logo.as_bytes()).context("Failed to write logo to cache file")?; + + Ok(logo.clone()) +} diff --git a/crates/hyfetch/src/lib.rs b/crates/hyfetch/src/lib.rs index 4cac9f42..f4d54d74 100644 --- a/crates/hyfetch/src/lib.rs +++ b/crates/hyfetch/src/lib.rs @@ -2,6 +2,7 @@ pub mod ascii; pub mod cli_options; pub mod color_util; pub mod distros; +pub mod font_logo; pub mod models; pub mod neofetch_util; pub mod presets; diff --git a/crates/hyfetch/src/neofetch_util.rs b/crates/hyfetch/src/neofetch_util.rs index 22d41280..dacbd34e 100644 --- a/crates/hyfetch/src/neofetch_util.rs +++ b/crates/hyfetch/src/neofetch_util.rs @@ -595,7 +595,7 @@ where } #[tracing::instrument(level = "debug")] -fn get_distro_name(backend: Backend) -> Result { +pub(crate) fn get_distro_name(backend: Backend) -> Result { match backend { Backend::Neofetch => run_neofetch_command_piped(&["ascii_distro_name"]) .context("failed to get distro name from neofetch"),