pub fn escape_for_applescript(s: &str) -> String {
s.replace('\\', "\\\\")
.replace('"', "\\\"")
.replace('\n', "\\n")
.replace('\r', "\\r")
}
pub fn deliver_desktop_notification(title: &str, message: &str, timeout_ms: u32) {
#[cfg(not(target_os = "macos"))]
{
use notify_rust::Notification;
let notification_title = if !title.is_empty() {
title
} else {
"Terminal Notification"
};
if let Err(e) = Notification::new()
.summary(notification_title)
.body(message)
.timeout(notify_rust::Timeout::Milliseconds(timeout_ms))
.show()
{
log::warn!("Failed to send desktop notification: {}", e);
}
}
#[cfg(target_os = "macos")]
{
let _ = timeout_ms; let notification_title = if !title.is_empty() {
title
} else {
"Terminal Notification"
};
let escaped_title = escape_for_applescript(notification_title);
let escaped_message = escape_for_applescript(message);
let script = format!(
r#"display notification "{}" with title "{}""#,
escaped_message, escaped_title,
);
if let Err(e) = std::process::Command::new("osascript")
.arg("-e")
.arg(&script)
.output()
{
log::warn!("Failed to send macOS desktop notification: {}", e);
}
}
}