enum Kind {
Osc9,
Kitty,
Osc777,
AppleScript,
Bell,
}
fn kind(var: impl Fn(&str) -> Option<String>) -> Kind {
let program = var("TERM_PROGRAM").unwrap_or_default();
let term = var("TERM").unwrap_or_default();
match program.as_str() {
"iTerm.app" | "WezTerm" | "ghostty" => Kind::Osc9,
"Apple_Terminal" => Kind::AppleScript,
_ if term.contains("kitty") || var("KITTY_WINDOW_ID").is_some() => Kind::Kitty,
_ if var("WT_SESSION").is_some() => Kind::Osc9,
_ if term.starts_with("foot") || term.contains("rxvt") => Kind::Osc777,
_ => Kind::Bell,
}
}
const OSASCRIPT: &str = if cfg!(test) { "true" } else { "osascript" };
fn clean(s: &str) -> String {
crate::text::strip_ansi(s)
.chars()
.filter(|c| !c.is_control() && !matches!(c, ';' | '"' | '\\'))
.collect()
}
pub fn notify(title: &str, body: &str) {
if !crate::output::is_terminal() {
return;
}
let kind = kind(|name| std::env::var(name).ok());
send(kind, title, body, std::env::var_os("TMUX").is_some());
}
fn send(kind: Kind, title: &str, body: &str, tmux: bool) {
let (title, body) = (clean(title), clean(body));
let seq = match kind {
Kind::Osc9 => format!("\x1b]9;{title}: {body}\x07"),
Kind::Kitty => format!("\x1b]99;;{title}: {body}\x1b\\"),
Kind::Osc777 => format!("\x1b]777;notify;{title};{body}\x07"),
Kind::Bell => "\x07".into(),
Kind::AppleScript => {
let script = format!("display notification \"{body}\" with title \"{title}\"");
let _ = std::process::Command::new(OSASCRIPT)
.args(["-e", &script])
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status();
return;
}
};
let seq = if tmux {
format!("\x1bPtmux;{}\x1b\\", seq.replace('\x1b', "\x1b\x1b"))
} else {
seq
};
crate::output::write(&seq);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::output::{capture, with_terminal};
fn kind_with(vars: &[(&str, &str)]) -> &'static str {
let found = kind(|name| vars.iter().find(|(k, _)| *k == name).map(|(_, v)| v.to_string()));
match found {
Kind::Osc9 => "osc9",
Kind::Kitty => "kitty",
Kind::Osc777 => "osc777",
Kind::AppleScript => "applescript",
Kind::Bell => "bell",
}
}
#[test]
fn picks_what_the_terminal_understands() {
assert_eq!(kind_with(&[("TERM_PROGRAM", "WezTerm")]), "osc9");
assert_eq!(kind_with(&[("TERM_PROGRAM", "Apple_Terminal")]), "applescript");
assert_eq!(kind_with(&[("TERM", "xterm-kitty")]), "kitty");
assert_eq!(kind_with(&[("KITTY_WINDOW_ID", "1")]), "kitty");
assert_eq!(kind_with(&[("WT_SESSION", "x")]), "osc9");
assert_eq!(kind_with(&[("TERM", "foot")]), "osc777");
assert_eq!(kind_with(&[("TERM", "rxvt-unicode")]), "osc777");
assert_eq!(kind_with(&[("TERM", "xterm")]), "bell");
}
#[test]
fn sends_the_escape_the_terminal_understands() {
let sent = |kind, tmux| capture(|| send(kind, "build;", "done\x1b[31m\"", tmux));
assert!(sent(Kind::Osc9, false).contains("\x1b]9;build: done\x07"));
assert!(sent(Kind::Kitty, false).contains("\x1b]99;;build: done\x1b\\"));
assert!(sent(Kind::Osc777, false).contains("\x1b]777;notify;build;done\x07"));
assert!(sent(Kind::Bell, false).contains('\x07'));
assert!(sent(Kind::Osc9, true).contains("\x1bPtmux;\x1b\x1b]9;build: done\x07\x1b\\"));
assert!(!sent(Kind::AppleScript, false).contains("build"));
}
#[test]
fn only_notifies_on_a_terminal() {
assert!(!capture(|| with_terminal(false, || notify("n-title", "body"))).contains("n-title"));
capture(|| with_terminal(true, || notify("n-title", "body")));
}
}