From 0200db4179b1e5988f3737e7caecf755a3227566 Mon Sep 17 00:00:00 2001 From: BlockListed <44610569+BlockListed@users.noreply.github.com> Date: Sun, 8 Oct 2023 09:22:11 +0200 Subject: [PATCH] fix not exciting when no notification daemon is available --- src/main.rs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 60af80e..d4817f7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ -use std::collections::HashSet; use std::path::PathBuf; +use std::time::Instant; +use std::{collections::HashSet, time::Duration}; use log::LevelFilter; use sctk::{ @@ -260,6 +261,8 @@ fn main_inner() { } fn main() { + const NOTIFY_TIMEOUT: Duration = Duration::from_micros(500); + let res = std::panic::catch_unwind(main_inner); if let Err(err) = res { @@ -271,13 +274,28 @@ fn main() { "unknown panic" }; - let _ = std::process::Command::new("notify-send") + let child_started = Instant::now(); + + let mut child = std::process::Command::new("notify-send") .args(&[ concat!("--app-name=", prog_name!()), concat!(prog_name!(), " has panicked!"), msg, ]) - .status(); + .spawn() + .unwrap(); + + loop { + let child_exited = child.try_wait().unwrap().is_some(); + if child_exited { + break; + } else if child_started.elapsed() > NOTIFY_TIMEOUT { + log::error!("Couldn't contact notification daemon. Timeout"); + break; + } else { + std::thread::sleep(Duration::from_millis(10)); + } + } log::error!("panic: {}", msg);