From c912a6024b5a2b42f8a00e4331d8c0a5ccc657d2 Mon Sep 17 00:00:00 2001 From: nyx Date: Sat, 2 Nov 2024 07:49:40 -0400 Subject: [PATCH 1/3] add support for /home/nyx and variables with paths --- src/lib.rs | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 21503d2..2be64d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -29,11 +29,17 @@ impl HyprlandConfig { 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) - } else { - path.replacen("~", &env::var("HOME").unwrap(), 1) - }; + let home = env::var("HOME").unwrap(); + let expanded_path = path + .replace("$HOME", &home) + .replace("$Config", &format!("{}/.config/hypr", home)); + + let sourced_path = + if !expanded_path.starts_with('/') && !expanded_path.starts_with('~') { + format!("{}/.config/hypr/{}", home, expanded_path) + } else { + expanded_path.replacen("~", &home, 1) + }; if let Ok(content) = fs::read_to_string(&sourced_path) { self.parse(&content, true); self.sourced_paths.push(sourced_path); From 600bccb5b8db22a412582351759707630b77489e Mon Sep 17 00:00:00 2001 From: nyx Date: Sat, 2 Nov 2024 07:57:39 -0400 Subject: [PATCH 2/3] oopsy --- src/lib.rs | 60 +++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2be64d5..0787c22 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,25 +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 var.starts_with('$') { + 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(var[1..].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 home = env::var("HOME").unwrap(); - let expanded_path = path - .replace("$HOME", &home) - .replace("$Config", &format!("{}/.config/hypr", home)); - - let sourced_path = - if !expanded_path.starts_with('/') && !expanded_path.starts_with('~') { - format!("{}/.config/hypr/{}", home, expanded_path) - } else { - expanded_path.replacen("~", &home, 1) - }; - if let Ok(content) = fs::read_to_string(&sourced_path) { - self.parse(&content, true); - self.sourced_paths.push(sourced_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 { + 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('{') { From a2e7ddf3519ac98384dcff7d9e4c71b99bb32c28 Mon Sep 17 00:00:00 2001 From: nyx Date: Sat, 2 Nov 2024 08:03:17 -0400 Subject: [PATCH 3/3] fix clippy --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 0787c22..6f448e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -32,14 +32,14 @@ impl HyprlandConfig { 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 var.starts_with('$') { + 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(var[1..].to_string(), expanded_val); + env_vars.insert(stripped.to_string(), expanded_val); continue; } }