remade the code to use dotenvy

This commit is contained in:
TheHolyTachanka 2023-02-19 19:57:37 +02:00
parent 1fb960c1c6
commit 79f6aa4c57
2 changed files with 10 additions and 9 deletions

View File

@ -73,7 +73,7 @@ pub enum PackageManager {
/// Obtain the amount of installed packages on the system by checking all installed supported package /// Obtain the amount of installed packages on the system by checking all installed supported package
/// managers and adding the amounts /// managers and adding the amounts
pub fn total_packages(package_readout: &PackageReadout) -> usize { pub fn total_packages(package_readout: &PackageReadout, skip_slow: bool) -> usize {
match env::consts::OS { match env::consts::OS {
"linux" => { "linux" => {
let macchina_package_count: Vec<(String, usize)> = package_readout let macchina_package_count: Vec<(String, usize)> = package_readout
@ -97,7 +97,7 @@ pub fn total_packages(package_readout: &PackageReadout) -> usize {
PackageManager::Nix, PackageManager::Nix,
] ]
.iter() .iter()
.map(|mngr| packages(mngr, &macchina_package_count)) .map(|mngr| packages(mngr, &macchina_package_count, skip_slow))
.sum() .sum()
} }
"macos" => package_readout "macos" => package_readout
@ -130,8 +130,7 @@ fn get_macchina_package_count(
/// return the amount of packages installed with a given linux package manager /// return the amount of packages installed with a given linux package manager
/// Return `0` if the package manager is not installed /// Return `0` if the package manager is not installed
fn packages(pkg_manager: &PackageManager, macchina_package_count: &[(String, usize)]) -> usize { fn packages(pkg_manager: &PackageManager, macchina_package_count: &[(String, usize)], skip_slow: bool) -> usize {
let args: Vec<String> = env::args().collect();
match pkg_manager { match pkg_manager {
// libmacchina has very fast implementations for most package managers, so we use them // libmacchina has very fast implementations for most package managers, so we use them
// where we can, otherwise we fall back to method used by dylans version of pfetch // where we can, otherwise we fall back to method used by dylans version of pfetch
@ -177,7 +176,7 @@ fn packages(pkg_manager: &PackageManager, macchina_package_count: &[(String, usi
} }
// TODO: nix -q is very slow // TODO: nix -q is very slow
PackageManager::Nix => { PackageManager::Nix => {
if check_if_command_exists("nix-store") || !args.contains(&"--no-nix".to_owned()) || !args.contains(&"-n".to_owned()) { if check_if_command_exists("nix-store") && !skip_slow {
run_and_count_lines( run_and_count_lines(
"nix-store", "nix-store",
&["-q", "--requisites", "/run/current-system/sw"], &["-q", "--requisites", "/run/current-system/sw"],

View File

@ -152,7 +152,7 @@ struct Readouts {
kernel_readout: KernelReadout, kernel_readout: KernelReadout,
} }
fn get_info(info: &PfetchInfo, readouts: &Readouts) -> Option<String> { fn get_info(info: &PfetchInfo, readouts: &Readouts, skip_slow: bool) -> Option<String> {
match info { match info {
PfetchInfo::Ascii => None, PfetchInfo::Ascii => None,
PfetchInfo::Title => { PfetchInfo::Title => {
@ -174,7 +174,7 @@ fn get_info(info: &PfetchInfo, readouts: &Readouts) -> Option<String> {
PfetchInfo::Host => pfetch::host(), PfetchInfo::Host => pfetch::host(),
PfetchInfo::Kernel => pfetch::kernel(&readouts.kernel_readout), PfetchInfo::Kernel => pfetch::kernel(&readouts.kernel_readout),
PfetchInfo::Uptime => pfetch::uptime(&readouts.general_readout), PfetchInfo::Uptime => pfetch::uptime(&readouts.general_readout),
PfetchInfo::Pkgs => Some(pfetch::total_packages(&readouts.package_readout).to_string()), PfetchInfo::Pkgs => Some(pfetch::total_packages(&readouts.package_readout, skip_slow).to_string()),
PfetchInfo::Memory => pfetch::memory(&readouts.memory_readout), PfetchInfo::Memory => pfetch::memory(&readouts.memory_readout),
PfetchInfo::Shell => match dotenvy::var("SHELL") { PfetchInfo::Shell => match dotenvy::var("SHELL") {
Ok(shell) => Some(shell), Ok(shell) => Some(shell),
@ -199,6 +199,8 @@ fn main() {
if let Ok(filepath) = dotenvy::var("PF_SOURCE") { if let Ok(filepath) = dotenvy::var("PF_SOURCE") {
dotenvy::from_path(filepath).unwrap(); dotenvy::from_path(filepath).unwrap();
} }
// Check if SKIP_SLOW is enabled
let skip_slow = dotenvy::var("SKIP_SLOW").map_or(false, |val| val == "true");
let enabled_pf_info_base: Vec<PfetchInfo> = match dotenvy::var("PF_INFO") { let enabled_pf_info_base: Vec<PfetchInfo> = match dotenvy::var("PF_INFO") {
Ok(pfetch_infos) => pfetch_infos Ok(pfetch_infos) => pfetch_infos
@ -242,7 +244,7 @@ fn main() {
kernel_readout: KernelReadout::new(), kernel_readout: KernelReadout::new(),
}; };
let os = get_info(&PfetchInfo::Os, &readouts).unwrap_or_default(); let os = get_info(&PfetchInfo::Os, &readouts, skip_slow).unwrap_or_default();
let logo_override = env::var("PF_ASCII"); let logo_override = env::var("PF_ASCII");
@ -269,7 +271,7 @@ fn main() {
let gathered_pfetch_info: Vec<(pfetch::Color, String, String)> = enabled_pf_info let gathered_pfetch_info: Vec<(pfetch::Color, String, String)> = enabled_pf_info
.iter() .iter()
.filter_map(|info| { .filter_map(|info| {
let info_result = get_info(info, &readouts); let info_result = get_info(info, &readouts, skip_slow);
match info_result { match info_result {
Some(info_str) => match info { Some(info_str) => match info {
PfetchInfo::Title => Some((logo.secondary_color, info_str, "".to_string())), PfetchInfo::Title => Some((logo.secondary_color, info_str, "".to_string())),