tony / oxwm

Branch: 5d148df
Switch branches/tags
master
tonybtw Lua Api. 5d148df 2025-11-12
130 lines · 4.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 = load_config(custom_config_path)?;
34
35 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
36 let should_restart = wm.run()?;
37
38 drop(wm);
39
40 if should_restart {
41 use std::os::unix::process::CommandExt;
42 let err = std::process::Command::new(&args[0]).args(&args[1..]).exec();
43 eprintln!("Failed to restart: {}", err);
44 }
45
46 Ok(())
47 }
48
49 fn load_config(custom_path: Option<PathBuf>) -> Result<oxwm::Config> {
50 let config_path = if let Some(path) = custom_path {
51 path
52 } else {
53 let config_dir = get_config_path();
54 let lua_path = config_dir.join("config.lua");
55
56 if !lua_path.exists() {
57 // Check if user had an old RON config
58 let ron_path = config_dir.join("config.ron");
59 let had_ron_config = ron_path.exists();
60
61 println!("No config found at {:?}", config_dir);
62 println!("Creating default Lua config...");
63 init_config()?;
64
65 if had_ron_config {
66 println!("\n⚠️ NOTICE: OXWM has migrated to Lua configuration.");
67 println!(" Your old config.ron has been preserved, but is no longer used.");
68 println!(" Your settings have been reset to defaults.");
69 println!(" Please manually port your configuration to the new Lua format.");
70 println!(" See the new config.lua template for examples.\n");
71 }
72 }
73
74 lua_path
75 };
76
77 let config_str =
78 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
79
80 let config_dir = config_path.parent();
81 oxwm::config::parse_lua_config(&config_str, config_dir)
82 .with_context(|| "Failed to parse Lua config")
83 }
84
85 fn init_config() -> Result<()> {
86 let config_dir = get_config_path();
87 std::fs::create_dir_all(&config_dir)?;
88
89 // Copy config.lua template
90 let config_template = include_str!("../../templates/config.lua");
91 let config_path = config_dir.join("config.lua");
92 std::fs::write(&config_path, config_template)?;
93
94 // Copy oxwm.lua API definitions for LSP support
95 let oxwm_lua_template = include_str!("../../templates/oxwm.lua");
96 let oxwm_lua_path = config_dir.join("oxwm.lua");
97 std::fs::write(&oxwm_lua_path, oxwm_lua_template)?;
98
99 println!("✓ Config created at {:?}", config_path);
100 println!("✓ LSP definitions installed at {:?}", oxwm_lua_path);
101 println!(" Edit the file and reload with Mod+Shift+R");
102 println!(" No compilation needed - changes take effect immediately!");
103
104 Ok(())
105 }
106
107 fn get_config_path() -> PathBuf {
108 dirs::config_dir()
109 .expect("Could not find config directory")
110 .join("oxwm")
111 }
112
113 fn print_help() {
114 println!("OXWM - A dynamic window manager written in Rust\n");
115 println!("USAGE:");
116 println!(" oxwm [OPTIONS]\n");
117 println!("OPTIONS:");
118 println!(" --init Create default config in ~/.config/oxwm/config.lua");
119 println!(" --config <PATH> Use custom config file");
120 println!(" --version Print version information");
121 println!(" --help Print this help message\n");
122 println!("CONFIG:");
123 println!(" Location: ~/.config/oxwm/config.lua");
124 println!(" Edit the config file and use Mod+Shift+R to reload");
125 println!(" No compilation needed - instant hot-reload!");
126 println!(" LSP support included with oxwm.lua type definitions\n");
127 println!("FIRST RUN:");
128 println!(" Run 'oxwm --init' to create a config file");
129 println!(" Or just start oxwm and it will create one automatically\n");
130 }