From 675a217a485af6b5097ecc022b86d6e4a7fe4cc2 Mon Sep 17 00:00:00 2001 From: n00b <59286911+1m-N00b@users.noreply.github.com> Date: Mon, 17 Jun 2024 03:38:04 +0900 Subject: [PATCH] refactor: Extract send_pause_signal function --- src/main.rs | 54 ++++++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 25 deletions(-) diff --git a/src/main.rs b/src/main.rs index d7baeca..8bf599c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -35,31 +35,7 @@ fn main() -> Result<(), Box> { // Send pause signal if cli.toggle_pause { - let processes = all_processes().expect("Failed to read processes"); - let mut target_pid = None; - for process in processes { - match process { - Ok(proc) => { - if let Ok(stat) = proc.stat() { - if stat.comm == "push2talk" { - target_pid = Some(stat.pid); - break; - } - } - } - Err(e) => { - error!("Error reading process: {}", e); - } - } - } - - if let Some(pid) = target_pid { - let pid = Pid::from_raw(pid); - signal::kill(pid, Signal::SIGUSR1).expect("Failed to send SIGUSR1"); - } else { - panic!("Process 'bla' not found"); - } - + send_pause_signal()?; println!("Toggle pause."); return Ok(()); @@ -101,6 +77,33 @@ fn main() -> Result<(), Box> { Ok(()) } +fn send_pause_signal() -> Result<(), Box> { + let processes = all_processes().expect("Failed to read processes"); + let mut target_pid = None; + + for process in processes { + if let Ok(proc) = process { + if let Ok(stat) = proc.stat() { + if stat.comm == "push2talk" { + target_pid = Some(stat.pid); + break; + } + } + } else { + error!("Error reading process"); + } + } + + if let Some(pid) = target_pid { + let pid = Pid::from_raw(pid); + signal::kill(pid, Signal::SIGUSR1).expect("Failed to send SIGUSR1"); + } else { + panic!("Process 'push2talk' not found"); + } + + Ok(()) +} + fn take_lock() -> Result> { let base_dirs = BaseDirs::new().ok_or("Cannot find base directories")?; let mut lock_path = PathBuf::from( @@ -168,3 +171,4 @@ fn register_signal( Ok(()) } +