Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[rust] implement list_distros match_condition method in rust #5

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions crates/hyfetch/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,66 @@ impl Distro {
",
);

buf.push_str(
"
impl Distro {
pub fn detect(name: &str) -> Option<Self> {
",
);
for (variant, distro) in &variants {
let distro_pattern = &distro.pattern;
let matches: Vec<&str> = distro_pattern.split('|').map(|s| s.trim()).collect();
let mut condition = Vec::new();

for m in matches {
let stripped = m.trim_matches(|c| c == '*' || c == '\'' || c == '"').to_lowercase();

if stripped.contains('*') || stripped.contains('"') {
println!("TODO: Cannot properly parse: {}", m);
}

// Exact matches
if m.trim_matches('*') == m {
condition.push(format!("name == r#\"{}\"#", stripped));
continue;
}

// Both sides are *
if m.starts_with('*') && m.ends_with('*') {
condition.push(format!("(name.starts_with(r#\"{}\"#) || name.ends_with(r#\"{}\"#))", stripped, stripped));
continue;
}

// Ends with *
if m.ends_with('*') {
condition.push(format!("name.starts_with(r#\"{}\"#)", stripped));
continue;
}

// Starts with *
if m.starts_with('*') {
condition.push(format!("name.ends_with(r#\"{}\"#)", stripped));
continue;
}
}

let condition = condition.join(" || ");

buf.push_str(&format!("
if {condition} {{
return Some(Self::{variant});
}}"
));
};
buf.push_str(&format!("
None
"
));

buf.push_str("
}
}");

fs::write(out_path.join("distros.rs"), buf).expect("couldn't write distros.rs");
}

Expand Down
6 changes: 6 additions & 0 deletions crates/hyfetch/src/neofetch_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use std::{env, fmt};
use anyhow::{anyhow, Context, Result};
use tracing::debug;

use crate::distros::Distro;

/// Gets the absolute path of the neofetch command.
pub fn get_command_path() -> Result<PathBuf> {
if let Ok(workspace_dir) = env::var("CARGO_WORKSPACE_DIR") {
Expand Down Expand Up @@ -61,6 +63,10 @@ where
};
debug!(%distro, "distro name");

if let Some(distro) = Distro::detect(&distro) {
return Ok(distro.ascii_art().to_owned());
}

todo!()
}

Expand Down