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

add support for $HOME and variables with paths #17

Merged
merged 3 commits into from
Nov 2, 2024
Merged
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
52 changes: 45 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,57 @@ impl HyprlandConfig {
0
};

let mut env_vars = HashMap::new();
let home = env::var("HOME").unwrap_or_default();
env_vars.insert("HOME".to_string(), home.clone());

println!("Parsing env vars from config:");
for line in config_str.lines() {
let trimmed = line.trim();
if let Some((var, val)) = trimmed.split_once('=').map(|(v, p)| (v.trim(), p.trim())) {
if let Some(stripped) = var.strip_prefix('$') {
println!("Found env var: {} = {}", var, val);
let mut expanded_val = val.to_string();
for (existing_var, existing_val) in &env_vars {
expanded_val =
expanded_val.replace(&format!("${}", existing_var), existing_val);
}
env_vars.insert(stripped.to_string(), expanded_val);
continue;
}
}
}
println!("Collected env vars: {:?}", env_vars);

for (i, line) in config_str.lines().enumerate() {
let trimmed = line.trim();

if trimmed.starts_with("source") && !sourced {
if let Some(path) = trimmed.split_once('=').map(|(_, p)| p.trim()) {
let sourced_path = if !path.starts_with('/') && !path.starts_with('~') {
format!("{}/.config/hypr/{}", env::var("HOME").unwrap(), path)
println!("Processing source path: {}", path);
let mut expanded_path = path.to_string();

for (var, val) in &env_vars {
let var_pattern = format!("${}", var);
println!("Replacing {} with {}", var_pattern, val);
expanded_path = expanded_path.replace(&var_pattern, val);
}
println!("After env var expansion: {}", expanded_path);

if !expanded_path.starts_with('/') && !expanded_path.starts_with('~') {
expanded_path = format!("{}/.config/hypr/{}", home, expanded_path);
} else {
path.replacen("~", &env::var("HOME").unwrap(), 1)
};
if let Ok(content) = fs::read_to_string(&sourced_path) {
self.parse(&content, true);
self.sourced_paths.push(sourced_path);
expanded_path = expanded_path.replacen("~", &home, 1);
}
println!("Final expanded path: {}", expanded_path);

match fs::read_to_string(&expanded_path) {
Ok(content) => {
println!("Successfully read sourced file");
self.parse(&content, true);
self.sourced_paths.push(expanded_path);
}
Err(e) => println!("Failed to read file: {}", e),
}
}
} else if trimmed.ends_with('{') {
Expand Down