Skip to content

Commit 591ac53

Browse files
added recursive parsing for hyprland.conf
1 parent bab0e86 commit 591ac53

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/config.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,12 @@ fn get_config_data(config_files: &[String]) -> Result<ConfigData> {
102102

103103
for config in config_files {
104104
let ext = Path::new(&config).extension().unwrap_or(OsStr::new("conf"));
105+
let parent = Path::new(config).parent().unwrap_log(file!(), line!());
105106
let mut content = String::new();
106107
File::open(config)?.read_to_string(&mut content)?;
107108

108109
let (options, mut config_data) = if config.contains("hyprland.conf") || ext == "txt" {
109-
parse_config(&content)?
110+
parse_config(&content, parent)?
110111
} else {
111112
parse_hyprlang(&content)?
112113
};
@@ -168,14 +169,14 @@ fn split_args(line: String) -> Vec<String> {
168169
args
169170
}
170171

171-
fn get_hyprscratch_lines(config: &str) -> Vec<String> {
172+
fn get_lines_with(pat: &str, config: &str) -> Vec<String> {
172173
let mut lines = vec![];
173174
for line in config.lines() {
174175
if line.trim().starts_with("#") {
175176
continue;
176177
}
177178

178-
if let Some(l) = line.find("hyprscratch") {
179+
if let Some(l) = line.find(pat) {
179180
lines.push(line.split_at(l).1.to_string());
180181
}
181182
}
@@ -244,17 +245,43 @@ fn parse_args(args: &[String]) -> Option<[String; 3]> {
244245
}
245246
}
246247

247-
fn parse_config(config: &str) -> Result<ConfigData> {
248-
let lines: Vec<String> = get_hyprscratch_lines(config);
248+
fn parse_source_config(source: &str, parent: &Path) -> Result<Vec<Scratchpad>> {
249+
let source_path = match source.split_once("=") {
250+
Some((_, s)) => s.trim(),
251+
None => return Ok(vec![]),
252+
};
253+
254+
let path = parent.join(source_path);
255+
256+
if let Ok(mut conf_file) = File::open(&path) {
257+
let mut config = String::new();
258+
conf_file.read_to_string(&mut config)?;
259+
let parent = path.parent().unwrap_log(file!(), line!());
260+
261+
let (_, scratchpads) = parse_config(&config, parent)?;
262+
return Ok(scratchpads);
263+
} else {
264+
let _ = log(format!("Source file not found: {source}"), Warn);
265+
return Ok(vec![]);
266+
}
267+
}
249268

269+
fn parse_config(config: &str, parent: &Path) -> Result<ConfigData> {
250270
let mut scratchpads: Vec<Scratchpad> = vec![];
271+
272+
let lines = get_lines_with("hyprscratch", config);
251273
for line in lines {
252274
let args = split_args(line);
253275
if let Some([title, command, opts]) = parse_args(&args) {
254276
scratchpads.push(Scratchpad::new(&title, &title, &command, &opts));
255277
}
256278
}
257279

280+
for source in get_lines_with("source", config).iter() {
281+
let mut scr = parse_source_config(source, parent).unwrap_log(file!(), line!());
282+
scratchpads.append(&mut scr);
283+
}
284+
258285
Ok(("".into(), scratchpads))
259286
}
260287

@@ -490,7 +517,11 @@ mod tests {
490517

491518
#[test]
492519
fn test_parse_config() {
493-
let (_, scratchpads) = parse_config(&open_conf("./test_configs/test_config1.txt")).unwrap();
520+
let (_, scratchpads) = parse_config(
521+
&open_conf("./test_configs/test_config1.txt"),
522+
Path::new("./test_configs"),
523+
)
524+
.unwrap();
494525
let mut expected_scratchpads = expected_scratchpads();
495526
for sc in expected_scratchpads.iter_mut() {
496527
sc.name = sc.title.clone();

0 commit comments

Comments
 (0)