use std::process::{Command, Stdio};
pub fn send(title: &str, body: &str) -> bool {
if cfg!(test) {
return false;
}
let Some(mut cmd) = command(title, body) else {
return false;
};
cmd.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.is_ok_and(|s| s.success())
}
#[cfg(target_os = "macos")]
fn command(title: &str, body: &str) -> Option<Command> {
let mut cmd = Command::new("osascript");
cmd.args([
"-e",
"on run argv",
"-e",
"display notification (item 2 of argv) with title (item 1 of argv)",
"-e",
"end run",
title,
body,
]);
Some(cmd)
}
#[cfg(all(unix, not(target_os = "macos")))]
fn command(title: &str, body: &str) -> Option<Command> {
if std::env::var_os("DISPLAY").is_none() && std::env::var_os("WAYLAND_DISPLAY").is_none() {
return None;
}
let mut cmd = Command::new("notify-send");
cmd.args(["--app-name=lean-ctx", "--urgency=low", "--", title, body]);
Some(cmd)
}
#[cfg(windows)]
fn command(title: &str, body: &str) -> Option<Command> {
const SCRIPT: &str = "\
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null; \
$t = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02); \
$x = $t.GetElementsByTagName('text'); \
$x.Item(0).AppendChild($t.CreateTextNode($env:LEAN_CTX_TOAST_TITLE)) | Out-Null; \
$x.Item(1).AppendChild($t.CreateTextNode($env:LEAN_CTX_TOAST_BODY)) | Out-Null; \
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier('{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\\WindowsPowerShell\\v1.0\\powershell.exe').Show([Windows.UI.Notifications.ToastNotification]::new($t))";
let mut cmd = Command::new("powershell");
cmd.args(["-NoProfile", "-NonInteractive", "-Command", SCRIPT])
.env("LEAN_CTX_TOAST_TITLE", title)
.env("LEAN_CTX_TOAST_BODY", body);
Some(cmd)
}
#[cfg(not(any(unix, windows)))]
fn command(_title: &str, _body: &str) -> Option<Command> {
None
}