| 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 | let config_dir = get_config_path(); |
| 59 | let lua_path = config_dir.join("config.lua"); |
| 60 | let ron_path = config_dir.join("config.ron"); |
| 61 | |
| 62 | if lua_path.exists() { |
| 63 | lua_path |
| 64 | } else if ron_path.exists() { |
| 65 | ron_path |
| 66 | } else { |
| 67 | println!("No config found at {:?}", config_dir); |
| 68 | println!("Creating default Lua config..."); |
| 69 | init_config()?; |
| 70 | config_dir.join("config.lua") |
| 71 | } |
| 72 | }; |
| 73 | |
| 74 | let config_str = |
| 75 | std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; |
| 76 | |
| 77 | let is_lua = config_path |
| 78 | .extension() |
| 79 | .and_then(|s| s.to_str()) |
| 80 | .map(|s| s == "lua") |
| 81 | .unwrap_or(false); |
| 82 | |
| 83 | if is_lua { |
| 84 | let config_dir = config_path.parent(); |
| 85 | oxwm::config::parse_lua_config(&config_str, config_dir) |
| 86 | .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 | get_config_path().join("config.ron") |
| 119 | }; |
| 120 | |
| 121 | if !ron_path.exists() { |
| 122 | eprintln!("Error: RON config file not found at {:?}", ron_path); |
| 123 | eprintln!("Please specify a path: oxwm --migrate <path/to/config.ron>"); |
| 124 | std::process::exit(1); |
| 125 | } |
| 126 | |
| 127 | println!("Migrating RON config to Lua..."); |
| 128 | println!(" Reading: {:?}", ron_path); |
| 129 | |
| 130 | let ron_content = std::fs::read_to_string(&ron_path) |
| 131 | .with_context(|| format!("Failed to read RON config from {:?}", ron_path))?; |
| 132 | |
| 133 | let lua_content = oxwm::config::migrate::ron_to_lua(&ron_content) |
| 134 | .with_context(|| "Failed to migrate RON config to Lua")?; |
| 135 | |
| 136 | let lua_path = ron_path.with_extension("lua"); |
| 137 | |
| 138 | std::fs::write(&lua_path, lua_content) |
| 139 | .with_context(|| format!("Failed to write Lua config to {:?}", lua_path))?; |
| 140 | |
| 141 | println!("✓ Migration complete!"); |
| 142 | println!(" Output: {:?}", lua_path); |
| 143 | println!("\nYour old config.ron is still intact."); |
| 144 | println!( |
| 145 | "Review the new config.lua and then you can delete config.ron if everything looks good." |
| 146 | ); |
| 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!( |
| 158 | " --migrate [PATH] Convert RON config to Lua (default: ~/.config/oxwm/config.ron)" |
| 159 | ); |
| 160 | println!(" --config <PATH> Use custom config file (.lua or .ron)"); |
| 161 | println!(" --version Print version information"); |
| 162 | println!(" --help Print this help message\n"); |
| 163 | println!("CONFIG:"); |
| 164 | println!(" Location: ~/.config/oxwm/config.lua (or config.ron for legacy)"); |
| 165 | println!(" Edit the config file and use Mod+Shift+R to reload"); |
| 166 | println!(" No compilation needed - instant hot-reload!\n"); |
| 167 | println!("MIGRATION:"); |
| 168 | println!(" To migrate from RON to Lua: oxwm --migrate"); |
| 169 | println!(" Or specify a custom path: oxwm --migrate /path/to/config.ron\n"); |
| 170 | println!("FIRST RUN:"); |
| 171 | println!(" Run 'oxwm --init' to create a config file"); |
| 172 | println!(" Or just start oxwm and it will create one automatically\n"); |
| 173 | } |