tony / oxwm

Branch: 259cca3
Switch branches/tags
master
tonybtw Fix bar flickering and improve configuration management 19dcad1 2025-11-16
167 lines · 5.4 KB Raw Blame History
1 use anyhow::{Context, Result};
2 use std::path::PathBuf;
3
4 fn main() -> Result<()> {
5 let args: Vec<String> = std::env::args().collect();
6
7 let mut custom_config_path: Option<PathBuf> = None;
8
9 match args.get(1).map(|s| s.as_str()) {
10 Some("--version") => {
11 println!("oxwm {}", env!("CARGO_PKG_VERSION"));
12 return Ok(());
13 }
14 Some("--help") => {
15 print_help();
16 return Ok(());
17 }
18 Some("--init") => {
19 init_config()?;
20 return Ok(());
21 }
22 Some("--config") => {
23 if let Some(path) = args.get(2) {
24 custom_config_path = Some(PathBuf::from(path));
25 } else {
26 eprintln!("Error: --config requires a path argument");
27 std::process::exit(1);
28 }
29 }
30 _ => {}
31 }
32
33 let (config, had_broken_config) = load_config(custom_config_path)?;
34
35 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
36
37 if had_broken_config {
38 wm.show_migration_overlay();
39 }
40
41 let should_restart = wm.run()?;
42
43 drop(wm);
44
45 if should_restart {
46 use std::os::unix::process::CommandExt;
47 let err = std::process::Command::new(&args[0]).args(&args[1..]).exec();
48 eprintln!("Failed to restart: {}", err);
49 }
50
51 Ok(())
52 }
53
54 fn load_config(custom_path: Option<PathBuf>) -> Result<(oxwm::Config, bool)> {
55 let config_path = if let Some(path) = custom_path {
56 path
57 } else {
58 let config_dir = get_config_path();
59 let lua_path = config_dir.join("config.lua");
60
61 if !lua_path.exists() {
62 let ron_path = config_dir.join("config.ron");
63 let had_ron_config = ron_path.exists();
64
65 println!("No config found at {:?}", config_dir);
66 println!("Creating default Lua config...");
67 init_config()?;
68
69 if had_ron_config {
70 println!("\n NOTICE: OXWM has migrated to Lua configuration.");
71 println!(" Your old config.ron has been preserved, but is no longer used.");
72 println!(" Your settings have been reset to defaults.");
73 println!(" Please manually port your configuration to the new Lua format.");
74 println!(" See the new config.lua template for examples.\n");
75 }
76 } else {
77 // Always update LSP files to match the running version
78 update_lsp_files()?;
79 }
80
81 lua_path
82 };
83
84 let config_str =
85 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
86
87 let config_dir = config_path.parent();
88
89 match oxwm::config::parse_lua_config(&config_str, config_dir) {
90 Ok(config) => Ok((config, false)),
91 Err(_) => {
92 let template = include_str!("../../templates/config.lua");
93 let config = oxwm::config::parse_lua_config(template, None)
94 .with_context(|| "Failed to parse default template config")?;
95 Ok((config, true))
96 }
97 }
98 }
99
100 fn init_config() -> Result<()> {
101 let config_dir = get_config_path();
102 std::fs::create_dir_all(&config_dir)?;
103
104 let config_template = include_str!("../../templates/config.lua");
105 let config_path = config_dir.join("config.lua");
106 std::fs::write(&config_path, config_template)?;
107
108 // Update LSP files
109 update_lsp_files()?;
110
111 println!("✓ Config created at {:?}", config_path);
112 println!("✓ LSP definitions installed at {:?}/lib/oxwm.lua", config_dir);
113 println!(" Edit the file and reload with Mod+Shift+R");
114 println!(" No compilation needed - changes take effect immediately!");
115
116 Ok(())
117 }
118
119 fn update_lsp_files() -> Result<()> {
120 let config_dir = get_config_path();
121
122 // Create lib directory for LSP files
123 let lib_dir = config_dir.join("lib");
124 std::fs::create_dir_all(&lib_dir)?;
125
126 // Always overwrite oxwm.lua with the version from this binary
127 let oxwm_lua_template = include_str!("../../templates/oxwm.lua");
128 let oxwm_lua_path = lib_dir.join("oxwm.lua");
129 std::fs::write(&oxwm_lua_path, oxwm_lua_template)?;
130
131 // Create/update .luarc.json for LSP configuration
132 let luarc_content = r#"{
133 "workspace.library": [
134 "lib"
135 ]
136 }
137 "#;
138 let luarc_path = config_dir.join(".luarc.json");
139 std::fs::write(&luarc_path, luarc_content)?;
140
141 Ok(())
142 }
143
144 fn get_config_path() -> PathBuf {
145 dirs::config_dir()
146 .expect("Could not find config directory")
147 .join("oxwm")
148 }
149
150 fn print_help() {
151 println!("OXWM - A dynamic window manager written in Rust\n");
152 println!("USAGE:");
153 println!(" oxwm [OPTIONS]\n");
154 println!("OPTIONS:");
155 println!(" --init Create default config in ~/.config/oxwm/config.lua");
156 println!(" --config <PATH> Use custom config file");
157 println!(" --version Print version information");
158 println!(" --help Print this help message\n");
159 println!("CONFIG:");
160 println!(" Location: ~/.config/oxwm/config.lua");
161 println!(" Edit the config file and use Mod+Shift+R to reload");
162 println!(" No compilation needed - instant hot-reload!");
163 println!(" LSP support included with oxwm.lua type definitions\n");
164 println!("FIRST RUN:");
165 println!(" Run 'oxwm --init' to create a config file");
166 println!(" Or just start oxwm and it will create one automatically\n");
167 }