| 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 | // Try to find config.lua first, then config.ron |
| 54 | let config_dir = get_config_path(); |
| 55 | let lua_path = config_dir.join("config.lua"); |
| 56 | let ron_path = config_dir.join("config.ron"); |
| 57 | |
| 58 | if lua_path.exists() { |
| 59 | lua_path |
| 60 | } else if ron_path.exists() { |
| 61 | ron_path |
| 62 | } else { |
| 63 | println!("No config found at {:?}", config_dir); |
| 64 | println!("Creating default Lua config..."); |
| 65 | init_config()?; |
| 66 | config_dir.join("config.lua") |
| 67 | } |
| 68 | }; |
| 69 | |
| 70 | let config_str = |
| 71 | std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?; |
| 72 | |
| 73 | // Determine config format based on file extension |
| 74 | let is_lua = config_path |
| 75 | .extension() |
| 76 | .and_then(|s| s.to_str()) |
| 77 | .map(|s| s == "lua") |
| 78 | .unwrap_or(false); |
| 79 | |
| 80 | if is_lua { |
| 81 | oxwm::config::parse_lua_config(&config_str).with_context(|| "Failed to parse Lua config") |
| 82 | } else { |
| 83 | oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse RON config") |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | fn init_config() -> Result<()> { |
| 88 | let config_dir = get_config_path(); |
| 89 | std::fs::create_dir_all(&config_dir)?; |
| 90 | |
| 91 | let config_template = include_str!("../../templates/config.lua"); |
| 92 | let config_path = config_dir.join("config.lua"); |
| 93 | |
| 94 | std::fs::write(&config_path, config_template)?; |
| 95 | |
| 96 | println!("✓ Config created at {:?}", config_path); |
| 97 | println!(" Edit the file and reload with Mod+Shift+R"); |
| 98 | println!(" No compilation needed - changes take effect immediately!"); |
| 99 | |
| 100 | Ok(()) |
| 101 | } |
| 102 | |
| 103 | fn get_config_path() -> PathBuf { |
| 104 | dirs::config_dir() |
| 105 | .expect("Could not find config directory") |
| 106 | .join("oxwm") |
| 107 | } |
| 108 | |
| 109 | fn print_help() { |
| 110 | println!("OXWM - A dynamic window manager written in Rust\n"); |
| 111 | println!("USAGE:"); |
| 112 | println!(" oxwm [OPTIONS]\n"); |
| 113 | println!("OPTIONS:"); |
| 114 | println!(" --init Create default config in ~/.config/oxwm/config.lua"); |
| 115 | println!(" --config <PATH> Use custom config file (.lua or .ron)"); |
| 116 | println!(" --version Print version information"); |
| 117 | println!(" --help Print this help message\n"); |
| 118 | println!("CONFIG:"); |
| 119 | println!(" Location: ~/.config/oxwm/config.lua (or config.ron for legacy)"); |
| 120 | println!(" Edit the config file and use Mod+Shift+R to reload"); |
| 121 | println!(" No compilation needed - instant hot-reload!\n"); |
| 122 | println!("FIRST RUN:"); |
| 123 | println!(" Run 'oxwm --init' to create a config file"); |
| 124 | println!(" Or just start oxwm and it will create one automatically\n"); |
| 125 | } |