Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Extract send_pause_signal function #17

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 29 additions & 25 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,31 +35,7 @@ fn main() -> Result<(), Box<dyn Error>> {

// 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(());
Expand Down Expand Up @@ -101,6 +77,33 @@ fn main() -> Result<(), Box<dyn Error>> {
Ok(())
}

fn send_pause_signal() -> Result<(), Box<dyn Error>> {
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<std::fs::File, Box<dyn Error>> {
let base_dirs = BaseDirs::new().ok_or("Cannot find base directories")?;
let mut lock_path = PathBuf::from(
Expand Down Expand Up @@ -168,3 +171,4 @@ fn register_signal(

Ok(())
}

Loading