From 8af51ce822138e546367293e123100e5be6e1646 Mon Sep 17 00:00:00 2001 From: tittu Date: Thu, 2 Jul 2026 00:01:25 +0530 Subject: [PATCH] code cleanup --- README.md | 4 +- src/main.rs | 107 +++++++++++++++++++++++++----------------------- src/wallbash.rs | 2 +- 3 files changed, 59 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index b993847..b942e65 100644 --- a/README.md +++ b/README.md @@ -41,11 +41,11 @@ wallbash status # Show daemon status # options for "set" wallbash set [option] - -p, --palette # Generate color palette (auto, dark, light) + -w, --wall # Wallpaper file /path/to/file.img -c, --cycle # Cycle in current folder (+1, -2, etc.) + -p, --palette # Generate color palette (auto, dark, light) -m, --mode # Scaling mode (cover, fit, original) -a, --anchor <1-9> # Anchor point (1=top-left ... 9=bottom-right) - -w, --wall # Wallpaper file /path/to/file.img ``` diff --git a/src/main.rs b/src/main.rs index e26023f..253f934 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,7 +11,6 @@ pub mod wayland; pub mod vulkan; pub mod filters; pub mod colors; - use std::{ env, io::Write, os::unix::net::UnixStream, @@ -48,11 +47,11 @@ fn print_usage() { ::Options wallbash set [option] - -p, --palette | Generate color palette (auto, dark, light) + -w, --wall | Wallpaper file /path/to/file.img -c, --cycle | Cycle in current folder (+1, -2, etc.) + -p, --palette | Generate color palette (auto, dark, light) -m, --mode | Scaling mode (cover, fit, original) -a, --anchor <1-9> | Anchor point (1=top-left ... 9=bottom-right) - -w, --wall | Wallpaper file /path/to/file.img " ); } @@ -170,43 +169,6 @@ fn parse_args(args: &[String]) -> CachedState { // get previous state let mut state = load_cache(); - // color generation - default "skip" - if let Some(pos) = args.iter().position(|a| a == "--palette" || a == "-p") { - let pal = args.get(pos + 1) - .filter(|s| matches!(s.as_str(), "auto" | "dark" | "light")) - .map(|s| s.clone()).unwrap_or_else(|| "skip".into()); - state.palette = pal; - } - - // mode – default "cover" - if let Some(pos) = args.iter().position(|a| a == "--mode" || a == "-m") { - let m = args.get(pos + 1) - .filter(|s| matches!(s.as_str(), "cover" | "fit" | "original")) - .map(|s| s.clone()).unwrap_or_else(|| "cover".into()); - state.mode = m; - } - - // anchor – default "center" - if let Some(pos) = args.iter().position(|a| a == "--anchor" || a == "-a") { - let anchor = args.get(pos + 1) - .and_then(|s| s.parse::().ok()) - .filter(|&n| (1..10).contains(&n)); - let (ax, ay) = match anchor { - Some(1) => (0.0, 0.0), - Some(2) => (0.5, 0.0), - Some(3) => (1.0, 0.0), - Some(4) => (0.0, 0.5), - Some(5) => (0.5, 0.5), - Some(6) => (1.0, 0.5), - Some(7) => (0.0, 1.0), - Some(8) => (0.5, 1.0), - Some(9) => (1.0, 1.0), - _ => (0.5, 0.5), - }; - state.anchor_x = ax; - state.anchor_y = ay; - } - // wallpaper – default "cached" let wall = args.iter().position(|a| a == "--wall" || a == "-w") .and_then(|i| args.get(i + 1).cloned()) @@ -242,13 +204,50 @@ fn parse_args(args: &[String]) -> CachedState { } state.wall = cycle_wallpaper(&state.wall, cycle); } - - // cache and return state if state.wall.is_empty() { eprintln!("Missing wallpaper (use --wall or bare path)"); print_usage(); std::process::exit(1); } + + // color generation - default "skip" + if let Some(pos) = args.iter().position(|a| a == "--palette" || a == "-p") { + let pal = args.get(pos + 1) + .filter(|s| matches!(s.as_str(), "auto" | "dark" | "light")) + .map(|s| s.clone()).unwrap_or_else(|| "skip".into()); + state.palette = pal; + } + + // mode – default "cover" + if let Some(pos) = args.iter().position(|a| a == "--mode" || a == "-m") { + let m = args.get(pos + 1) + .filter(|s| matches!(s.as_str(), "cover" | "fit" | "original")) + .map(|s| s.clone()).unwrap_or_else(|| "cover".into()); + state.mode = m; + } + + // anchor – default "center" + if let Some(pos) = args.iter().position(|a| a == "--anchor" || a == "-a") { + let anchor = args.get(pos + 1) + .and_then(|s| s.parse::().ok()) + .filter(|&n| (1..10).contains(&n)); + let (ax, ay) = match anchor { + Some(1) => (0.0, 0.0), + Some(2) => (0.5, 0.0), + Some(3) => (1.0, 0.0), + Some(4) => (0.0, 0.5), + Some(5) => (0.5, 0.5), + Some(6) => (1.0, 0.5), + Some(7) => (0.0, 1.0), + Some(8) => (0.5, 1.0), + Some(9) => (1.0, 1.0), + _ => (0.5, 0.5), + }; + state.anchor_x = ax; + state.anchor_y = ay; + } + + // cache and return state save_cache(&state); state } @@ -274,13 +273,10 @@ fn main() { state.palette, state.mode, state.anchor_x, state.anchor_y, state.wall); if !check_daemon() { println!("Starting daemon"); - let log_file = std::fs::File::create(LOG_FILE).expect("Cannot create log"); + let log = std::fs::File::create(LOG_FILE).expect("Cannot create log"); let mut child = Command::new(env::current_exe().unwrap()) - .arg("start") - .stdout(log_file.try_clone().unwrap()) - .stderr(log_file) - .spawn() - .expect("Failed to start daemon"); + .arg("start").stdout(log.try_clone().unwrap()).stderr(log) + .spawn().expect("Failed to start daemon"); if let Err(e) = wait_loop() { eprintln!("Error {}", e); let _ = child.kill(); @@ -293,14 +289,23 @@ fn main() { } Some("stop") => { if let Err(e) = send_command("stop") { - eprintln!("Failed to stop daemon {}. Is it running?", e); + eprintln!("Failed to stop daemon {}. Is it even running?", e); } } Some("status") => { if check_daemon() { - println!("Daemon is running."); + println!("[wallbash] :: Daemon is running"); + } else { + println!("[wallbash] :: Daemon is not running"); + } + let state = load_cache(); + if state.wall.is_empty() { + println!("[wallbash] :: No wallpaper cached yet"); } else { - println!("Daemon is not running."); + println!("Wallpaper :: {}", state.wall); + println!("Palette :: {}", state.palette); + println!("Mode :: {}", state.mode); + println!("Anchor :: ({:.1}, {:.1})", state.anchor_x, state.anchor_y); } } _ => print_usage() diff --git a/src/wallbash.rs b/src/wallbash.rs index 952c2b8..9eda3e6 100644 --- a/src/wallbash.rs +++ b/src/wallbash.rs @@ -251,7 +251,7 @@ impl DaemonState { let resolved = std::fs::canonicalize(&path) .map(|p| p.to_string_lossy().to_string()) .unwrap_or(path); - println!("[wallbash] loading '{}' ({}|{}|ax:{:?}|ay:{:?})", resolved, palette, mode, anchor_x, anchor_y); + println!("[wallbash] loading '{}' ({}|{}|ax:{:.1}|ay:{:.1})", resolved, palette, mode, anchor_x, anchor_y); let effect = |tex: &vulkan::VulkanTexture| { if mode != "cover" {