hyprparser 0.1.6

A parser for Hyprland's configuration file.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#![doc(
    html_favicon_url = "https://raw.githubusercontent.com/hyprutils/hyprparser/refs/heads/main/hyprparser.png"
)]
#![doc(
    html_logo_url = "https://raw.githubusercontent.com/hyprutils/hyprparser/refs/heads/main/hyprparser.png"
)]

//! A parser for [Hyprland](https://hyprland.org)'s configuration files 🚀
//!
//! [Hyprland's documentation](https://wiki.hyprland.org/Configuring)
//!
//! # Example usage
//! ```rust,ignore
//! use hyprparser::parse_config;
//! use std::{env, fs, path::Path};
//!
//! let config_path = Path::new(&env::var("XDG_CONFIG_HOME").unwrap()).join("hypr/hyprland.conf");
//! let config_str = fs::read_to_string(&config_path).expect("Failed to read the file");
//!
//! let mut parsed_config = parse_config(&config_str);
//!
//! parsed_config.add_entry("decoration", "rounding = 10");
//! parsed_config.add_entry("decoration.blur", "enabled = true");
//! parsed_config.add_entry("decoration.blur", "size = 10");
//! parsed_config.add_entry_headless("$terminal", "kitty");
//!
//! let updated_config_str = parsed_config.to_string();
//!
//! fs::write(&config_path, updated_config_str).expect("Failed to write the file");
//! ```

use std::collections::HashMap;
use std::{env, fmt, fs};

/// Core structure of the config
#[derive(Debug, Default)]
pub struct HyprlandConfig {
    pub content: Vec<String>,
    pub sections: HashMap<String, (usize, usize)>,
    pub sourced_content: Vec<Vec<String>>,
    pub sourced_sections: HashMap<String, (usize, usize)>,
    pub sourced_paths: Vec<String>,
}

impl HyprlandConfig {
    pub fn new() -> Self {
        Self::default()
    }

    /// Parse one configuration file
    pub fn parse(&mut self, config_str: &str, sourced: bool) {
        let mut section_stack = Vec::new();
        let mut sourced_content: Vec<String> = Vec::new();
        let source_index = if sourced {
            self.sourced_content.len()
        } else {
            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.split('#').next().unwrap_or(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.split('#').next().unwrap_or(p).trim())
                {
                    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('{') {
                let section_name = trimmed.trim_end_matches('{').trim().to_string();
                section_stack.push((section_name, i));
            } else if trimmed == "}" && !section_stack.is_empty() {
                let (name, start) = section_stack.pop().unwrap();
                let full_name = section_stack
                    .iter()
                    .map(|(n, _)| n.as_str())
                    .chain(std::iter::once(name.as_str()))
                    .collect::<Vec<_>>()
                    .join(".");
                if sourced {
                    self.sourced_sections
                        .insert(format!("{}_{}", full_name, source_index), (start, i));
                } else {
                    self.sections.insert(full_name, (start, i));
                }
            }
            if sourced {
                sourced_content.push(line.to_string());
            } else {
                self.content.push(line.to_string());
            }
        }
        if sourced {
            self.sourced_content.push(sourced_content);
        }
    }

    /// Add an entry to a mutable `HyprlandConfig`
    pub fn add_entry(&mut self, category: &str, entry: &str) {
        let parts: Vec<&str> = category.split('.').collect();
        let parent_category = if parts.len() > 1 {
            parts[..parts.len() - 1].join(".")
        } else {
            category.to_string()
        };

        if let Some((source_index, _)) = self.find_sourced_section(&parent_category) {
            let section_key = format!("{}_{}", parent_category, source_index);
            let (start, mut end) = *self.sourced_sections.get(&section_key).unwrap();
            let depth = parent_category.matches('.').count();
            let key = entry.split('=').next().unwrap().trim();

            let mut should_update_sections = false;
            let mut content_updated = String::new();

            if let Some(sourced_content) = self.sourced_content.get_mut(source_index) {
                let subcategory_key = format!("{}_{}", category, source_index);

                if parts.len() > 1 && !self.sourced_sections.contains_key(&subcategory_key) {
                    let last_part = parts.last().unwrap();
                    let section_start = format!("{}{} {{", "    ".repeat(depth + 1), last_part);
                    let section_end = format!("{}}}", "    ".repeat(depth + 1));

                    if end > 0
                        && end <= sourced_content.len()
                        && !sourced_content[end - 1].trim().is_empty()
                    {
                        sourced_content.insert(end, String::new());
                        end += 1;
                    }

                    sourced_content.insert(end, section_start);
                    sourced_content
                        .insert(end + 1, format!("{}{}", "    ".repeat(depth + 2), entry));
                    sourced_content.insert(end + 2, section_end);

                    self.sourced_sections
                        .insert(subcategory_key, (end + 1, end + 1));
                    should_update_sections = true;
                } else if let Some(&(sub_start, sub_end)) =
                    self.sourced_sections.get(&subcategory_key)
                {
                    let parent_category = if parts.len() > 1 {
                        parts[..parts.len()].join(".")
                    } else {
                        category.to_string()
                    };
                    let depth = parent_category.matches('.').count();

                    let formatted_entry = format!("{}{}", "    ".repeat(depth + 1), entry);
                    let existing_line = sourced_content[sub_start..=sub_end]
                        .iter()
                        .position(|line| line.trim().starts_with(key));

                    match existing_line {
                        Some(line_num) => {
                            sourced_content[sub_start + line_num] = formatted_entry;
                        }
                        None => {
                            sourced_content.insert(sub_end, formatted_entry);
                            should_update_sections = true;
                        }
                    }
                } else {
                    let formatted_entry = format!("{}{}", "    ".repeat(depth + 1), entry);
                    let existing_line = sourced_content[start..=end]
                        .iter()
                        .position(|line| line.trim().starts_with(key));

                    match existing_line {
                        Some(line_num) => {
                            sourced_content[start + line_num] = formatted_entry;
                        }
                        None => {
                            sourced_content.insert(end, formatted_entry);
                            should_update_sections = true;
                        }
                    }
                }

                content_updated = sourced_content.join("\n");
            }

            if should_update_sections {
                self.update_sourced_sections(source_index, end, 1);
            }

            if let Some(sourced_path) = self.sourced_paths.get(source_index) {
                if !sourced_path.is_empty() {
                    if let Err(e) = fs::write(sourced_path, content_updated) {
                        eprintln!("Failed to write to sourced file {}: {}", sourced_path, e);
                    }
                }
            }
            return;
        }

        let parts: Vec<&str> = category.split('.').collect();
        let mut current_section = String::new();
        let mut insert_pos = self.content.len();

        for (depth, (i, part)) in parts.iter().enumerate().enumerate() {
            if i > 0 {
                current_section.push('.');
            }
            current_section.push_str(part);

            if !self.sections.contains_key(&current_section) {
                self.create_category(&current_section, depth, &mut insert_pos);
            }

            let &(start, end) = self.sections.get(&current_section).unwrap();
            insert_pos = end;

            if i == parts.len() - 1 {
                let key = entry.split('=').next().unwrap().trim();
                let existing_line = self.content[start..=end]
                    .iter()
                    .position(|line| line.trim().starts_with(key))
                    .map(|pos| start + pos);

                let formatted_entry = format!("{}{}", "    ".repeat(depth + 1), entry);

                match existing_line {
                    Some(line_num) => {
                        self.content[line_num] = formatted_entry;
                    }
                    None => {
                        self.content.insert(end, formatted_entry);
                        self.update_sections(end, 1);
                    }
                }
                return;
            }
        }
    }

    /// Add a headless entry to a mutable `HyprlandConfig`
    ///
    /// Example of a headless entry in Hyprland's configuration:
    /// ```conf
    /// windowrulev2 = float,class:^(hyprutils.hyprwall)$
    /// ```
    pub fn add_entry_headless(&mut self, key: &str, value: &str) {
        if key.is_empty() && value.is_empty() {
            self.content.push(String::new());
        } else {
            let entry = format!("{} = {}", key, value);
            if !self.content.iter().any(|line| line.trim() == entry.trim()) {
                self.content.push(entry);
            }
        }
    }

    /// Add a [sourced config file](https://wiki.hyprland.org/Configuring/Keywords/#sourcing-multi-file)
    pub fn add_sourced(&mut self, config: Vec<String>) {
        self.sourced_content.push(config);
        self.sourced_paths.push(String::new());
    }

    fn update_sections(&mut self, pos: usize, offset: usize) {
        for (start, end) in self.sections.values_mut() {
            if *start >= pos {
                *start += offset;
                *end += offset;
            } else if *end >= pos {
                *end += offset;
            }
        }
    }

    fn update_sourced_sections(&mut self, source_index: usize, pos: usize, offset: usize) {
        for ((_, (start, end)), sourced_path) in self
            .sourced_sections
            .iter_mut()
            .filter(|(_, (start, _))| *start >= pos)
            .zip(self.sourced_paths.iter().skip(source_index))
        {
            if !sourced_path.is_empty() {
                if *start >= pos {
                    *start += offset;
                    *end += offset;
                } else if *end >= pos {
                    *end += offset;
                }
            }
        }
    }

    /// Parse a color from Hyprland's config into float RGBA values
    ///
    /// Examples:
    /// ```rust,ignore
    /// let config = HyprlandConfig::new();
    ///
    /// let rgba = config.parse_color("rgba(1E4632FF)");
    /// let rgb = config.parse_color("rgb(1E4632)");
    /// let argb = config.parse_color("0xFF1E4632");
    ///
    /// let expected = Some((0.11764706, 0.27450982, 0.19607843, 1.0));
    ///
    /// assert_eq!(expected, rgba);
    /// assert_eq!(expected, rgb);
    /// assert_eq!(expected, argb);
    /// ```
    pub fn parse_color(&self, color_str: &str) -> Option<(f32, f32, f32, f32)> {
        if color_str.starts_with("rgba(") {
            let rgba = color_str.trim_start_matches("rgba(").trim_end_matches(')');
            let rgba = u32::from_str_radix(rgba, 16).ok()?;
            Some((
                ((rgba >> 24) & 0xFF) as f32 / 255.0,
                ((rgba >> 16) & 0xFF) as f32 / 255.0,
                ((rgba >> 8) & 0xFF) as f32 / 255.0,
                (rgba & 0xFF) as f32 / 255.0,
            ))
        } else if color_str.starts_with("rgb(") {
            let rgb = color_str.trim_start_matches("rgb(").trim_end_matches(')');
            let rgb = u32::from_str_radix(rgb, 16).ok()?;
            Some((
                ((rgb >> 16) & 0xFF) as f32 / 255.0,
                ((rgb >> 8) & 0xFF) as f32 / 255.0,
                (rgb & 0xFF) as f32 / 255.0,
                1.0,
            ))
        } else if let Some(stripped) = color_str.strip_prefix("0x") {
            let argb = u32::from_str_radix(stripped, 16).ok()?;
            Some((
                ((argb >> 16) & 0xFF) as f32 / 255.0,
                ((argb >> 8) & 0xFF) as f32 / 255.0,
                (argb & 0xFF) as f32 / 255.0,
                ((argb >> 24) & 0xFF) as f32 / 255.0,
            ))
        } else {
            None
        }
    }

    /// Format a float RGBA color into Hyprland's RGBA
    pub fn format_color(&self, red: f32, green: f32, blue: f32, alpha: f32) -> String {
        format!(
            "rgba({:02x}{:02x}{:02x}{:02x})",
            (red * 255.0) as u8,
            (green * 255.0) as u8,
            (blue * 255.0) as u8,
            (alpha * 255.0) as u8
        )
    }

    fn create_category(&mut self, category: &str, depth: usize, insert_pos: &mut usize) {
        let part = category.split('.').last().unwrap();
        let new_section = format!("{}{} {{", "    ".repeat(depth), part);

        let mut lines_added = 0;
        if *insert_pos > 0 && !self.content[*insert_pos - 1].trim().is_empty() {
            self.content.insert(*insert_pos, String::new());
            *insert_pos += 1;
            lines_added += 1;
        }

        self.content.insert(*insert_pos, new_section);
        *insert_pos += 1;
        self.content
            .insert(*insert_pos, format!("{}}}", "    ".repeat(depth)));
        *insert_pos += 1;
        self.content.insert(*insert_pos, String::new());
        *insert_pos += 1;

        self.update_sections(*insert_pos - 3 - lines_added, 3 + lines_added);
        self.sections.insert(
            category.to_string(),
            (*insert_pos - 3 - lines_added, *insert_pos - 2),
        );
    }

    fn find_sourced_section(&self, category: &str) -> Option<(usize, (usize, usize))> {
        for (idx, _) in self.sourced_content.iter().enumerate() {
            let section_key = format!("{}_{}", category, idx);
            if let Some(&section) = self.sourced_sections.get(&section_key) {
                if self.sourced_paths.get(idx).map_or(false, |p| !p.is_empty()) {
                    return Some((idx, section));
                }
            }
        }
        None
    }
}

/// Automatically parse the whole configuration from str
pub fn parse_config(config_str: &str) -> HyprlandConfig {
    let mut config = HyprlandConfig::new();
    config.parse(config_str, false);
    config
}

impl fmt::Display for HyprlandConfig {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        for (i, line) in self.content.iter().enumerate() {
            if i == self.content.len() - 1 {
                write!(f, "{}", line)?;
            } else {
                writeln!(f, "{}", line)?;
            }
        }
        Ok(())
    }
}

impl PartialEq for HyprlandConfig {
    fn eq(&self, other: &Self) -> bool {
        self.content == other.content
    }
}