tony / oxwm

Fix config reload to reuse original config file path Browse files
Fixes #77
emzywastaken committed 2025-12-14 parent 5bf5485 commit 54ea2dc54db1dcd6e9dc848b37f59d5457a25f8c
Showing 4 changed files with 22 additions and 12 deletions. UnifiedSplit
+4 −1 src/bin/main.rs
@@ -87,7 +87,10 @@ fn load_config(
87 87 let config_directory = config_path.parent();
88 88
89 89 match oxwm::config::parse_lua_config(&config_string, config_directory) {
90 - Ok(config) => Ok((config, false)),
90 + Ok(mut config) => {
91 + config.path = Some(config_path);
92 + Ok((config, false))
93 + }
91 94 Err(_error) => {
92 95 let template = include_str!("../../templates/config.lua");
93 96 let config = oxwm::config::parse_lua_config(template, None)
+1 −0 src/config/lua.rs
@@ -49,5 +49,6 @@ pub fn parse_lua_config(
49 49 scheme_selected: builder_data.scheme_selected,
50 50 autostart: builder_data.autostart,
51 51 auto_tile: builder_data.auto_tile,
52 + path: None,
52 53 });
53 54 }
+6 −0 src/lib.rs
@@ -1,3 +1,5 @@
1 +use std::path::PathBuf;
2 +
1 3 pub mod bar;
2 4 pub mod client;
3 5 pub mod config;
@@ -56,6 +58,9 @@ impl WindowRule {
56 58
57 59 #[derive(Debug, Clone)]
58 60 pub struct Config {
61 + // Meta
62 + pub path: Option<PathBuf>,
63 +
59 64 // Appearance
60 65 pub border_width: u32,
61 66 pub border_focused: u32,
@@ -117,6 +122,7 @@ impl Default for Config {
117 122 const TERMINAL: &str = "st";
118 123
119 124 Self {
125 + path: None,
120 126 border_width: 2,
121 127 border_focused: 0x6dade3,
122 128 border_unfocused: 0xbbbbbb,
+11 −11 src/window_manager.rs
@@ -9,6 +9,7 @@ use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}
9 9 use crate::monitor::{Monitor, detect_monitors};
10 10 use crate::overlay::{ErrorOverlay, KeybindOverlay, Overlay};
11 11 use std::collections::{HashMap, HashSet};
12 +use std::path::PathBuf;
12 13 use x11rb::cursor::Handle as CursorHandle;
13 14
14 15 use x11rb::connection::Connection;
@@ -340,27 +341,26 @@ impl WindowManager {
340 341 }
341 342
342 343 fn try_reload_config(&mut self) -> Result<(), String> {
343 - let config_dir = if let Some(xdg_config) = std::env::var_os("XDG_CONFIG_HOME") {
344 - std::path::PathBuf::from(xdg_config).join("oxwm")
345 - } else if let Some(home) = std::env::var_os("HOME") {
346 - std::path::PathBuf::from(home).join(".config").join("oxwm")
347 - } else {
348 - return Err("Could not find config directory".to_string());
349 - };
350 -
351 - let lua_path = config_dir.join("config.lua");
344 + let lua_path = self
345 + .config
346 + .path
347 + .take()
348 + .expect("Could not find config file. Config path should've been set while loading");
352 349
353 350 if !lua_path.exists() {
354 - return Err("No config.lua file found".to_string());
351 + return Err("Could not find config file, has it been moved?".to_string());
355 352 }
356 353
357 354 let config_str = std::fs::read_to_string(&lua_path)
358 355 .map_err(|e| format!("Failed to read config: {}", e))?;
359 356
360 - let new_config = crate::config::parse_lua_config(&config_str, Some(&config_dir))
357 + let config_dir = lua_path.parent();
358 +
359 + let new_config = crate::config::parse_lua_config(&config_str, config_dir)
361 360 .map_err(|e| format!("{}", e))?;
362 361
363 362 self.config = new_config;
363 + self.config.path = Some(lua_path);
364 364 self.error_message = None;
365 365
366 366 for bar in &mut self.bars {