Compare commits

..

No commits in common. "refactor" and "main" have entirely different histories.

3 changed files with 96 additions and 114 deletions

View File

@ -37,9 +37,6 @@ impl FromStr for Color {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
if s.is_empty() {
return Err("No string given".to_string());
}
Ok(Color(s.parse::<u8>().ok()))
}
}

View File

@ -90,17 +90,19 @@ fn packages(
&format!("{pkg_manager:?}").to_lowercase(),
)
.unwrap_or(0),
PackageManager::Rpm => get_macchina_package_count(
PackageManager::Rpm => match get_macchina_package_count(
macchina_package_count,
&format!("{pkg_manager:?}").to_lowercase(),
)
.unwrap_or_else(|| {
if !skip_slow {
run_and_count_lines("rpm", &["-qa"])
} else {
0
) {
Some(count) => count,
None => {
if !skip_slow {
run_and_count_lines("rpm", &["-qa"])
} else {
0
}
}
}),
},
PackageManager::Guix => run_and_count_lines("guix", &["package", "--list-installed"]),
PackageManager::Crux => {
if check_if_command_exists("crux") {
@ -137,31 +139,37 @@ pub fn user_at_hostname(
username_override: &Option<String>,
hostname_override: &Option<String>,
) -> Option<String> {
let username = username_override
.to_owned()
.or_else(|| general_readout.username().ok());
let hostname = hostname_override
.to_owned()
.or_else(|| general_readout.hostname().ok());
if let (Some(username), Some(hostname)) = (username, hostname) {
return Some(format!("{username}@{hostname}"));
let username = match username_override {
Some(username) => Ok(username.to_string()),
None => general_readout.username(),
};
let hostname = match hostname_override {
Some(hostname) => Ok(hostname.to_string()),
None => general_readout.hostname(),
};
if username.is_err() || hostname.is_err() {
None
} else {
Some(format!(
"{}@{}",
username.unwrap_or_default(),
hostname.unwrap_or_default()
))
}
None
}
pub fn memory(memory_readout: &MemoryReadout) -> Option<String> {
let total_memory = memory_readout.total();
let used_memory = memory_readout.used();
if let (Ok(total_memory), Ok(used_memory)) = (total_memory, used_memory) {
return Some(format!(
if total_memory.is_err() || used_memory.is_err() {
None
} else {
Some(format!(
"{}M / {}M",
used_memory / 1024,
total_memory / 1024
));
used_memory.unwrap() / 1024,
total_memory.unwrap() / 1024
))
}
None
}
pub fn cpu(general_readout: &GeneralReadout) -> Option<String> {
@ -228,7 +236,7 @@ pub fn seconds_to_string(seconds: usize) -> String {
}
pub fn uptime(general_readout: &GeneralReadout) -> Option<String> {
general_readout.uptime().ok().map(seconds_to_string)
Some(seconds_to_string(general_readout.uptime().ok()?))
}
pub fn host(general_readout: &GeneralReadout) -> Option<String> {
@ -431,3 +439,4 @@ mod tests {
assert_eq!(seconds_to_string(90060), "1d 1h 1m".to_string());
}
}

View File

@ -6,70 +6,6 @@ use pfetch_logo_parser::{Color, Logo, LogoPart};
use std::{env, fmt::Display, str::FromStr};
use unicode_width::UnicodeWidthStr;
struct Config {
pf_ascii: Option<String>,
pf_col1: String,
pf_col2: String,
pf_col3: String,
pf_color: String,
pf_sep: String,
pf_pad1: String,
pf_pad2: String,
pf_pad3: String,
user: String,
hostname: String,
pf_source: String,
pf_fast_pkg_count: bool,
pf_info: Vec<PfetchInfo>,
pf_custom_logos: String,
print_version: bool,
}
impl Config {
fn load() -> Self {
// source file specified by env: PF_SOURCE
if let Ok(filepath) = dotenvy::var("PF_SOURCE") {
let _ = dotenvy::from_path(filepath);
}
Self {
pf_ascii: dotenvy::var("PF_ASCII").ok(),
pf_col1: dotenvy::var("PF_COL1").unwrap_or_default(),
pf_col2: dotenvy::var("PF_COL2").unwrap_or_default(),
pf_col3: dotenvy::var("PF_COL3").unwrap_or_default(),
pf_color: dotenvy::var("PF_COLOR").unwrap_or_default(),
pf_sep: dotenvy::var("PF_SEP").unwrap_or_default(),
pf_pad1: dotenvy::var("PF_PAD1").unwrap_or_default(),
pf_pad2: dotenvy::var("PF_PAD2").unwrap_or_default(),
pf_pad3: dotenvy::var("PF_PAD3").unwrap_or_default(),
user: dotenvy::var("USER").unwrap_or_default(),
hostname: dotenvy::var("HOSTNAME").unwrap_or_default(),
pf_source: dotenvy::var("PF_SOURCE").unwrap_or_default(),
pf_fast_pkg_count: dotenvy::var("PF_FAST_PKG_COUNT").is_ok(),
pf_info: match dotenvy::var("PF_INFO") {
Ok(pfetch_infos) => pfetch_infos
.trim()
.split(' ')
.map(PfetchInfo::from_str)
.filter_map(|i| i.ok())
.collect(),
Err(_) => vec![
PfetchInfo::Ascii,
PfetchInfo::Title,
PfetchInfo::Os,
PfetchInfo::Host,
PfetchInfo::Kernel,
PfetchInfo::Uptime,
PfetchInfo::Pkgs,
PfetchInfo::Memory,
],
},
pf_custom_logos: dotenvy::var("PF_CUSTOM_LOGOS").unwrap_or_default(),
print_version: std::env::args()
.any(|arg| arg.starts_with("-v") || arg.starts_with("--v")),
}
}
}
#[derive(Debug, PartialEq)]
enum PfetchInfo {
Ascii,
@ -268,17 +204,59 @@ fn get_info(
}
fn main() {
let config = Config::load();
if config.print_version {
// parse arguements
if std::env::args().any(|arg| arg.starts_with("-v") || arg.starts_with("--v")) {
println!("pfetch-rs {}", env!("CARGO_PKG_VERSION"));
std::process::exit(0);
} else if std::env::args().len() > 1 {
// TODO: change this for raw logos
println!("pfetch show system information");
println!("pfetch -v show version");
std::process::exit(0);
}
// source file specified by env: PF_SOURCE
if let Ok(filepath) = dotenvy::var("PF_SOURCE") {
let _ = dotenvy::from_path(filepath);
}
// Check if SKIP_SLOW is enabled
let skip_slow_package_managers = dotenvy::var("PF_FAST_PKG_COUNT").is_ok();
let enabled_pf_info_base: Vec<PfetchInfo> = match dotenvy::var("PF_INFO") {
Ok(pfetch_infos) => pfetch_infos
.trim()
.split(' ')
.map(PfetchInfo::from_str)
.filter_map(|i| i.ok())
.collect(),
Err(_) => vec![
PfetchInfo::Ascii,
PfetchInfo::Title,
PfetchInfo::Os,
PfetchInfo::Host,
PfetchInfo::Kernel,
PfetchInfo::Uptime,
PfetchInfo::Pkgs,
PfetchInfo::Memory,
],
};
// insert blank lines before and after palettes
let mut enabled_pf_info: Vec<PfetchInfo> = vec![];
let mut ascii_enabled: bool = false;
for info in enabled_pf_info_base {
match info {
PfetchInfo::Palette => {
enabled_pf_info.push(PfetchInfo::BlankLine);
enabled_pf_info.push(PfetchInfo::Palette);
enabled_pf_info.push(PfetchInfo::BlankLine);
}
PfetchInfo::Ascii => {
ascii_enabled = true;
}
i => enabled_pf_info.push(i),
}
}
let readouts = Readouts {
general_readout: GeneralReadout::new(),
package_readout: PackageReadout::new(),
@ -288,7 +266,9 @@ fn main() {
let os = pfetch::os(&GeneralReadout::new()).unwrap_or_default();
let logo_name = config.pf_ascii.unwrap_or(match env::consts::OS {
let logo_override = env::var("PF_ASCII");
let logo_name = logo_override.unwrap_or(match env::consts::OS {
"linux" => os.clone(),
other => other.to_owned(),
});
@ -296,22 +276,25 @@ fn main() {
let mut logo = pfetch::logo(&logo_name);
// color overrides
if let Ok(newcolor) = Color::from_str(&config.pf_col1) {
logo.primary_color = newcolor;
if let Ok(newcolor) = dotenvy::var("PF_COL1") {
if let Ok(newcolor) = Color::from_str(&newcolor) {
logo.primary_color = newcolor;
}
}
if config.pf_col3 == "COL1" {
logo.secondary_color = logo.primary_color;
} else if let Ok(newcolor) = Color::from_str(&config.pf_col3) {
logo.secondary_color = newcolor;
if let Ok(newcolor) = dotenvy::var("PF_COL3") {
if newcolor == "COL1" {
logo.secondary_color = logo.primary_color;
} else if let Ok(newcolor) = Color::from_str(&newcolor) {
logo.secondary_color = newcolor;
}
}
let gathered_pfetch_info: Vec<(Color, String, String)> = config
.pf_info
let gathered_pfetch_info: Vec<(Color, String, String)> = enabled_pf_info
.iter()
.filter_map(|info| match info {
PfetchInfo::Os => Some((logo.primary_color, info.to_string(), os.clone())),
_ => get_info(info, &readouts, config.pf_fast_pkg_count).map(|info_str| match info {
_ => get_info(info, &readouts, skip_slow_package_managers).map(|info_str| match info {
PfetchInfo::Title => (logo.secondary_color, info_str, "".to_string()),
PfetchInfo::BlankLine => (logo.primary_color, "".to_string(), "".to_string()),
PfetchInfo::Palette => (logo.primary_color, info_str, "".to_string()),
@ -320,12 +303,5 @@ fn main() {
})
.collect();
pfetch(
gathered_pfetch_info,
logo,
config
.pf_info
.iter()
.any(|x| matches!(&x, PfetchInfo::Ascii)),
);
pfetch(gathered_pfetch_info, logo, ascii_enabled);
}