tony / oxwm

Branch: 0b23d5f
Switch branches/tags
master
emzywastaken use `anyhow::Context` instead of mapping 9a1df9a 2025-10-16
91 lines · 2.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 match args.get(1).map(|s| s.as_str()) {
8 Some("--version") => {
9 println!("oxwm {}", env!("CARGO_PKG_VERSION"));
10 return Ok(());
11 }
12 Some("--help") => {
13 print_help();
14 return Ok(());
15 }
16 Some("--init") => {
17 init_config()?;
18 return Ok(());
19 }
20 _ => {}
21 }
22
23 let config = load_config()?;
24
25 let mut wm = oxwm::window_manager::WindowManager::new(config)?;
26 let should_restart = wm.run()?;
27
28 drop(wm);
29
30 if should_restart {
31 use std::os::unix::process::CommandExt;
32 let err = std::process::Command::new(&args[0]).args(&args[1..]).exec();
33 eprintln!("Failed to restart: {}", err);
34 }
35
36 Ok(())
37 }
38
39 fn load_config() -> Result<oxwm::Config> {
40 let config_path = get_config_path().join("config.ron");
41
42 if !config_path.exists() {
43 println!("No config found at {:?}", config_path);
44 println!("Creating default config...");
45 init_config()?;
46 }
47
48 let config_str =
49 std::fs::read_to_string(&config_path).with_context(|| "Failed to read config file")?;
50
51 oxwm::config::parse_config(&config_str).with_context(|| "Failed to parse config")
52 }
53
54 fn init_config() -> Result<()> {
55 let config_dir = get_config_path();
56 std::fs::create_dir_all(&config_dir)?;
57
58 let config_template = include_str!("../../templates/config.ron");
59 let config_path = config_dir.join("config.ron");
60
61 std::fs::write(&config_path, config_template)?;
62
63 println!("✓ Config created at {:?}", config_path);
64 println!(" Edit the file and reload with Mod+Shift+R");
65 println!(" No compilation needed - changes take effect immediately!");
66
67 Ok(())
68 }
69
70 fn get_config_path() -> PathBuf {
71 dirs::config_dir()
72 .expect("Could not find config directory")
73 .join("oxwm")
74 }
75
76 fn print_help() {
77 println!("OXWM - A dynamic window manager written in Rust\n");
78 println!("USAGE:");
79 println!(" oxwm [OPTIONS]\n");
80 println!("OPTIONS:");
81 println!(" --init Create default config in ~/.config/oxwm/config.ron");
82 println!(" --version Print version information");
83 println!(" --help Print this help message\n");
84 println!("CONFIG:");
85 println!(" Location: ~/.config/oxwm/config.ron");
86 println!(" Edit the config file and use Mod+Shift+R to reload");
87 println!(" No compilation needed - instant hot-reload!\n");
88 println!("FIRST RUN:");
89 println!(" Run 'oxwm --init' to create a config file");
90 println!(" Or just start oxwm and it will create one automatically\n");
91 }