tony / oxwm

Branch: 44c9e5e
Switch branches/tags
master
tonybtw Added proper error handling for when a user logs in to a deprecated config. df0e7f2 2025-11-13
140 lines · 4.7 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 }
77
78 lua_path
79 };
80
81 let config_str =
82 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
83
84 let config_dir = config_path.parent();
85
86 match oxwm::config::parse_lua_config(&config_str, config_dir) {
87 Ok(config) => Ok((config, false)),
88 Err(_) => {
89 let template = include_str!("../../templates/config.lua");
90 let config = oxwm::config::parse_lua_config(template, None)
91 .with_context(|| "Failed to parse default template config")?;
92 Ok((config, true))
93 }
94 }
95 }
96
97 fn init_config() -> Result<()> {
98 let config_dir = get_config_path();
99 std::fs::create_dir_all(&config_dir)?;
100
101 let config_template = include_str!("../../templates/config.lua");
102 let config_path = config_dir.join("config.lua");
103 std::fs::write(&config_path, config_template)?;
104
105 let oxwm_lua_template = include_str!("../../templates/oxwm.lua");
106 let oxwm_lua_path = config_dir.join("oxwm.lua");
107 std::fs::write(&oxwm_lua_path, oxwm_lua_template)?;
108
109 println!("✓ Config created at {:?}", config_path);
110 println!("✓ LSP definitions installed at {:?}", oxwm_lua_path);
111 println!(" Edit the file and reload with Mod+Shift+R");
112 println!(" No compilation needed - changes take effect immediately!");
113
114 Ok(())
115 }
116
117 fn get_config_path() -> PathBuf {
118 dirs::config_dir()
119 .expect("Could not find config directory")
120 .join("oxwm")
121 }
122
123 fn print_help() {
124 println!("OXWM - A dynamic window manager written in Rust\n");
125 println!("USAGE:");
126 println!(" oxwm [OPTIONS]\n");
127 println!("OPTIONS:");
128 println!(" --init Create default config in ~/.config/oxwm/config.lua");
129 println!(" --config <PATH> Use custom config file");
130 println!(" --version Print version information");
131 println!(" --help Print this help message\n");
132 println!("CONFIG:");
133 println!(" Location: ~/.config/oxwm/config.lua");
134 println!(" Edit the config file and use Mod+Shift+R to reload");
135 println!(" No compilation needed - instant hot-reload!");
136 println!(" LSP support included with oxwm.lua type definitions\n");
137 println!("FIRST RUN:");
138 println!(" Run 'oxwm --init' to create a config file");
139 println!(" Or just start oxwm and it will create one automatically\n");
140 }