From 62be00b3b93e8ea7a3f17e3e420028f8900c1350 Mon Sep 17 00:00:00 2001 From: Adrian Groh Date: Fri, 7 Jul 2023 16:23:29 +0200 Subject: [PATCH] feat: add support for custom logos at runtime closes #33 This allows to use the `PF_CUSTOM_LOGOS` option to load a file containing pfetch logos --- README.md | 59 ++++++++++++++++++++++++++++++++++++++++---- custom_logos_example | 34 +++++++++++++++++++++++++ src/lib.rs | 21 +++++++++++++--- 3 files changed, 106 insertions(+), 8 deletions(-) create mode 100644 custom_logos_example diff --git a/README.md b/README.md index 2e60dd7..300b8d2 100644 --- a/README.md +++ b/README.md @@ -85,12 +85,11 @@ value. ## Configuration Like the original `pfetch`, `pfetch-rs` is configured through environment -variables. Your existing config will probably still work, the only difference is +variables. Your existing config will probably still work, the main difference is how padding is configured. -If you want to display a custom logo, you will have to download the source code, -make your changes to `./pfetch-extractor/logos.sh` and build the binary with -`cargo b --release`. +If you want to display a custom logo, use the `PF_CUSTOM_LOGOS` option, an +example for a custom logos file can be found below. ```sh # Which information to display. @@ -106,11 +105,16 @@ PF_INFO="ascii" # Example: Only Information. PF_INFO="title os host kernel uptime pkgs memory" -# A file containing environment variables to source before running pfetch. +# A file containing environment variables to source before running pfetch # Default: unset # Valid: A shell script PF_SOURCE="" +# A file containing pfetch logos to overwrite default logos or add new logos +# Default: unset +# Valid: Path to a file containing pfetch logos (example below) +PF_CUSTOM_LOGOS="~/.config/pfetch_logos" + # Separator between info name and info data. # Default: unset # Valid: string @@ -162,3 +166,48 @@ HOSTNAME="" # Skip package managers that take "long" to query package count (like nix) PF_FAST_PKG_COUNT=1 ``` + +A file containing custom pfetch logos could look like this (also found under +`custom_logos_example`). This will turn the Arch Linux logo red, the Debian Logo +blue and the Fedora logo yellow: + +``` +[Aa]rch*) + read_ascii 1 <<- EOF + ${c1} /\\ + ${c1} / \\ + ${c1} /\\ \\ + ${c1} / \\ + ${c1} / ,, \\ + ${c1} / | | -\\ + ${c1} /_-'' ''-_\\ + EOF + ;; +[Dd]ebian*) + read_ascii 4 <<- EOF + ${c4} _____ + ${c4} / __ \\ + ${c4}| / | + ${c4}| \\___- + ${c4}-_ + ${c4} --_ + EOF + ;; +[Ff]edora*) + read_ascii 3 <<- EOF + ${c3},'''''. + ${c3}| ,. | + ${c3}| | '_' + ${c3} ,....| |.. + ${c3}.' ,_;| ..' + ${c3}| | | | + ${c3}| ',_,' | + ${c3} '. ,' + ${c3}''''' + EOF +``` + +_Note: Make sure to use tabs for indentation and separate logos with `;;`, as +seen above. You only need to add the logos you want to overwrite/add, the +default logos will stay available. The included logos can be found at +`./pfetch-extractor/logos.sh`._ diff --git a/custom_logos_example b/custom_logos_example new file mode 100644 index 0000000..a9054c7 --- /dev/null +++ b/custom_logos_example @@ -0,0 +1,34 @@ +[Aa]rch*) + read_ascii 1 <<- EOF + ${c1} /\\ + ${c1} / \\ + ${c1} /\\ \\ + ${c1} / \\ + ${c1} / ,, \\ + ${c1} / | | -\\ + ${c1} /_-'' ''-_\\ + EOF + ;; +[Dd]ebian*) + read_ascii 4 <<- EOF + ${c4} _____ + ${c4} / __ \\ + ${c4}| / | + ${c4}| \\___- + ${c4}-_ + ${c4} --_ + EOF + ;; + +[Ff]edora*) + read_ascii 3 <<- EOF + ${c3},'''''. + ${c3}| ,. | + ${c3}| | '_' + ${c3} ,....| |.. + ${c3}.' ,_;| ..' + ${c3}| | | | + ${c3}| ',_,' | + ${c3} '. ,' + ${c3}''''' + EOF diff --git a/src/lib.rs b/src/lib.rs index 8abcc3e..b12322c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,4 @@ -use std::{env, fs, io::Result, process::Command}; +use std::{collections::VecDeque, env, fs, io::Result, process::Command}; use glob::glob; use globset::Glob; @@ -6,7 +6,7 @@ use libmacchina::{ traits::GeneralReadout as _, traits::KernelReadout as _, traits::MemoryReadout as _, traits::PackageReadout as _, GeneralReadout, KernelReadout, MemoryReadout, PackageReadout, }; -use pfetch_logo_parser::Logo; +use pfetch_logo_parser::{parse_logo, Logo}; #[derive(Debug)] pub enum PackageManager { @@ -311,8 +311,23 @@ pub fn host(general_readout: &GeneralReadout) -> Option { } } +fn parse_custom_logos(filename: &str) -> Vec> { + let file_contents = fs::read_to_string(filename).expect("Could not open custom logo file"); + file_contents + .split(";;") + .map(|raw_logo| parse_logo(raw_logo).map(|(_, logo)| logo)) + .collect::>() +} + pub fn logo(logo_name: &str) -> Logo { - let (tux, logos) = pfetch_extractor::parse_logos!(); + let (tux, included_logos) = pfetch_extractor::parse_logos!(); + let mut logos: VecDeque<_> = included_logos.into(); + if let Ok(filename) = dotenvy::var("PF_CUSTOM_LOGOS") { + // insert custom logos in front of incuded logos + for custom_logo in parse_custom_logos(&filename).into_iter().flatten() { + logos.insert(0, custom_logo.clone()); + } + }; logos .into_iter() .find(|logo| {