From 2b2962f7f0b68695230e0f69e16c5b66edbb53a6 Mon Sep 17 00:00:00 2001 From: S4NKALP Date: Thu, 12 Mar 2026 19:00:20 +0545 Subject: [PATCH] feat(src/commands): add inhibit command to block sleep using systemd-inhibit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This command prevents system sleep while optionally executing a user‑provided command, offering both foreground and background inhibition modes. - New Features - Implemented inhibit.rs with logic to invoke systemd-inhibit. - Exposed inhibit functionality by adding pub mod inhibit; to src/commands/mod.rs. - Enhancements - (none) - Bug Fixes - (none) --- src/commands/inhibit.rs | 54 +++++++++++++++++++++++++++++++++++++++++ src/commands/mod.rs | 1 + 2 files changed, 55 insertions(+) create mode 100644 src/commands/inhibit.rs diff --git a/src/commands/inhibit.rs b/src/commands/inhibit.rs new file mode 100644 index 0000000..e7564ab --- /dev/null +++ b/src/commands/inhibit.rs @@ -0,0 +1,54 @@ +use log::{error, info}; +use std::process::Command; + +pub fn execute(command_args: &[String], why: &str, who: &str, daemon: bool) { + let mut args = vec![ + "--why".to_string(), + why.to_string(), + "--who".to_string(), + who.to_string(), + "--what".to_string(), + "sleep:idle".to_string(), + ]; + + if command_args.is_empty() { + if !daemon { + info!("No command provided. Inhibiting sleep indefinitely. Press Ctrl+C to stop."); + } + args.push("sleep".to_string()); + args.push("infinity".to_string()); + } else { + info!( + "Running command while inhibiting sleep: {}", + command_args.join(" ") + ); + args.extend(command_args.iter().cloned()); + } + + if daemon { + match Command::new("systemd-inhibit").args(&args).spawn() { + Ok(child) => { + info!("Inhibitor started in background with PID: {}", child.id()); + info!("To stop it, use: pkill -P {} systemd-inhibit", child.id()); + // In background mode, we just let the child run and exit lapctl + } + Err(e) => { + error!("Failed to spawn systemd-inhibit: {}", e); + } + } + } else { + match Command::new("systemd-inhibit").args(&args).status() { + Ok(status) => { + if !status.success() { + error!("systemd-inhibit exited with status: {}", status); + } + } + Err(e) => { + error!("Failed to execute systemd-inhibit: {}", e); + eprintln!( + "Error: make sure 'systemd-inhibit' is installed and available in your PATH." + ); + } + } + } +} diff --git a/src/commands/mod.rs b/src/commands/mod.rs index 8be8b09..70c780a 100644 --- a/src/commands/mod.rs +++ b/src/commands/mod.rs @@ -1,6 +1,7 @@ pub mod battery; pub mod cooling; pub mod gpu; +pub mod inhibit; pub mod install_rules; pub mod power; pub mod status;