use std::process::Command;
pub fn send_mac_notification(
title: &str,
message: &str,
pr_url: Option<&str>,
auto_open: bool,
) -> bool {
let escaped_title = title.replace('"', "\\\"");
let escaped_message = message.replace('"', "\\\"");
if let Some(url) = pr_url {
let escaped_url = url.replace('"', "\\\"");
if auto_open {
let script = format!(
"display notification \"{}\" with title \"🦀 {}\" subtitle \"PRCtrl\" sound name \"Glass\"\ntell application \"Google Chrome\" to open location \"{}\"",
escaped_message, escaped_title, escaped_url
);
return Command::new("osascript")
.arg("-e")
.arg(script)
.status()
.is_ok_and(|status| status.success());
} else {
let notification_with_url = format!("{}\n\n🔗 {}", message, url);
let apple_script = format!(
r#"display notification "{}" with title "🦀 {}" subtitle "PRCtrl" sound name "Glass""#,
notification_with_url.replace('"', "\\\""),
escaped_title
);
return Command::new("osascript")
.arg("-e")
.arg(apple_script)
.status()
.is_ok_and(|status| status.success());
}
}
let apple_script = format!(
r#"display notification "{}" with title "🦀 {}" subtitle "PRCtrl" sound name "Glass""#,
escaped_message, escaped_title
);
Command::new("osascript")
.arg("-e")
.arg(apple_script)
.status()
.is_ok_and(|status| status.success())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_send_mac_notification() {
let result = send_mac_notification("Test Title", "Test Message", None, false);
let _ = result; }
#[test]
fn test_send_mac_notification_with_url_no_auto_open() {
let result = send_mac_notification(
"Test PR",
"Test message",
Some("https://github.com/test/repo/pull/1"),
false,
);
let _ = result; }
#[test]
fn test_send_mac_notification_with_url_auto_open() {
let result = send_mac_notification(
"Test PR",
"Test message",
Some("https://github.com/test/repo/pull/1"),
true,
);
let _ = result; }
}