tony / oxwm

Branch: 1474578
Switch branches/tags
master
tonybtw Migration to lua. faf793b 2025-11-08
171 lines · 5.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("--migrate") => {
23 let path = args.get(2).map(PathBuf::from);
24 migrate_config(path)?;
25 return Ok(());
26 }
27 Some("--config") => {
28 if let Some(path) = args.get(2) {
29 custom_config_path = Some(PathBuf::from(path));
30 } else {
31 eprintln!("Error: --config requires a path argument");
32 std::process::exit(1);
33 }
34 }
35 _ => {}
36 }
37
38 let config = load_config(custom_config_path)?;
39
40 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
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> {
55 let config_path = if let Some(path) = custom_path {
56 path
57 } else {
58 // Try to find config.lua first, then config.ron
59 let config_dir = get_config_path();
60 let lua_path = config_dir.join("config.lua");
61 let ron_path = config_dir.join("config.ron");
62
63 if lua_path.exists() {
64 lua_path
65 } else if ron_path.exists() {
66 ron_path
67 } else {
68 println!("No config found at {:?}", config_dir);
69 println!("Creating default Lua config...");
70 init_config()?;
71 config_dir.join("config.lua")
72 }
73 };
74
75 let config_str =
76 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
77
78 // Determine config format based on file extension
79 let is_lua = config_path
80 .extension()
81 .and_then(|s| s.to_str())
82 .map(|s| s == "lua")
83 .unwrap_or(false);
84
85 if is_lua {
86 oxwm::config::parse_lua_config(&config_str).with_context(|| "Failed to parse Lua config")
87 } else {
88 oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse RON config")
89 }
90 }
91
92 fn init_config() -> Result<()> {
93 let config_dir = get_config_path();
94 std::fs::create_dir_all(&config_dir)?;
95
96 let config_template = include_str!("../../templates/config.lua");
97 let config_path = config_dir.join("config.lua");
98
99 std::fs::write(&config_path, config_template)?;
100
101 println!("✓ Config created at {:?}", config_path);
102 println!(" Edit the file and reload with Mod+Shift+R");
103 println!(" No compilation needed - changes take effect immediately!");
104
105 Ok(())
106 }
107
108 fn get_config_path() -> PathBuf {
109 dirs::config_dir()
110 .expect("Could not find config directory")
111 .join("oxwm")
112 }
113
114 fn migrate_config(custom_path: Option<PathBuf>) -> Result<()> {
115 let ron_path = if let Some(path) = custom_path {
116 path
117 } else {
118 // Default to ~/.config/oxwm/config.ron
119 get_config_path().join("config.ron")
120 };
121
122 if !ron_path.exists() {
123 eprintln!("Error: RON config file not found at {:?}", ron_path);
124 eprintln!("Please specify a path: oxwm --migrate <path/to/config.ron>");
125 std::process::exit(1);
126 }
127
128 println!("Migrating RON config to Lua...");
129 println!(" Reading: {:?}", ron_path);
130
131 let ron_content = std::fs::read_to_string(&ron_path)
132 .with_context(|| format!("Failed to read RON config from {:?}", ron_path))?;
133
134 let lua_content = oxwm::config::migrate::ron_to_lua(&ron_content)
135 .map_err(|e| anyhow::anyhow!("Migration failed: {}", e))?;
136
137 // Determine output path (same directory, .lua extension)
138 let lua_path = ron_path.with_extension("lua");
139
140 std::fs::write(&lua_path, lua_content)
141 .with_context(|| format!("Failed to write Lua config to {:?}", lua_path))?;
142
143 println!("✓ Migration complete!");
144 println!(" Output: {:?}", lua_path);
145 println!("\nYour old config.ron is still intact.");
146 println!("Review the new config.lua and then you can delete config.ron if everything looks good.");
147
148 Ok(())
149 }
150
151 fn print_help() {
152 println!("OXWM - A dynamic window manager written in Rust\n");
153 println!("USAGE:");
154 println!(" oxwm [OPTIONS]\n");
155 println!("OPTIONS:");
156 println!(" --init Create default config in ~/.config/oxwm/config.lua");
157 println!(" --migrate [PATH] Convert RON config to Lua (default: ~/.config/oxwm/config.ron)");
158 println!(" --config <PATH> Use custom config file (.lua or .ron)");
159 println!(" --version Print version information");
160 println!(" --help Print this help message\n");
161 println!("CONFIG:");
162 println!(" Location: ~/.config/oxwm/config.lua (or config.ron for legacy)");
163 println!(" Edit the config file and use Mod+Shift+R to reload");
164 println!(" No compilation needed - instant hot-reload!\n");
165 println!("MIGRATION:");
166 println!(" To migrate from RON to Lua: oxwm --migrate");
167 println!(" Or specify a custom path: oxwm --migrate /path/to/config.ron\n");
168 println!("FIRST RUN:");
169 println!(" Run 'oxwm --init' to create a config file");
170 println!(" Or just start oxwm and it will create one automatically\n");
171 }