1#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum Notice {
8 Osc777,
10 Osc9,
12 Osc99,
14}
15
16const WITH_777: [&str; 6] = ["ghostty", "wezterm", "foot", "rxvt", "konsole", "contour"];
18
19impl Notice {
20 pub fn for_terminal(names: &str) -> Self {
23 let names = names.to_lowercase();
24 if names.contains("kitty") {
25 Self::Osc99
26 } else if WITH_777.iter().any(|t| names.contains(t)) {
27 Self::Osc777
28 } else {
29 Self::Osc9
30 }
31 }
32}
33
34pub fn terminal_names(var: impl Fn(&str) -> Option<String>) -> String {
37 let mut names: Vec<String> = ["TERM", "TERM_PROGRAM", "LC_TERMINAL"]
38 .iter()
39 .filter_map(|name| var(name))
40 .collect();
41 if var("TMUX").is_some_and(|v| !v.is_empty()) {
42 let asked = std::process::Command::new("tmux")
43 .args(["display", "-p", "#{client_termname} #{client_termtype}"])
44 .output();
45 if let Ok(output) = asked
46 && output.status.success()
47 {
48 names.push(String::from_utf8_lossy(&output.stdout).trim().to_string());
49 }
50 }
51 names.join(" ")
52}
53
54fn clean(text: &str) -> String {
57 let flat: String = text
58 .chars()
59 .map(|c| if c.is_control() { ' ' } else { c })
60 .collect();
61 flat.split_whitespace().collect::<Vec<_>>().join(" ")
62}
63
64pub fn sequence(notice: Notice, title: &str, body: &str, tmux: bool) -> String {
67 let (title, body) = (clean(title), clean(body));
68 let inner = match notice {
69 Notice::Osc777 => format!("\x1b]777;notify;{};{body}\x1b\\", title.replace(';', ",")),
71 Notice::Osc9 => format!("\x1b]9;{title}: {body}\x1b\\"),
72 Notice::Osc99 => {
74 format!(
75 "\x1b]99;i=cellix:d=0:p=title;{title}\x1b\\\x1b]99;i=cellix:d=1:p=body;{body}\x1b\\"
76 )
77 }
78 };
79 if tmux {
80 format!("\x1bPtmux;{}\x1b\\", inner.replace('\x1b', "\x1b\x1b"))
81 } else {
82 inner
83 }
84}
85
86#[cfg(test)]
87mod tests {
88 use super::*;
89
90 #[test]
91 fn each_terminal_gets_the_sequence_it_reads() {
92 assert_eq!(
93 Notice::for_terminal("xterm-ghostty ghostty"),
94 Notice::Osc777
95 );
96 assert_eq!(
97 Notice::for_terminal("xterm-256color WezTerm"),
98 Notice::Osc777
99 );
100 assert_eq!(Notice::for_terminal("xterm-kitty"), Notice::Osc99);
101 assert_eq!(
102 Notice::for_terminal("xterm-256color iTerm.app iTerm2"),
103 Notice::Osc9
104 );
105 assert_eq!(Notice::for_terminal(""), Notice::Osc9);
106 assert_eq!(
108 Notice::for_terminal("tmux-256color tmux xterm-ghostty"),
109 Notice::Osc777
110 );
111 }
112
113 #[test]
114 fn a_notification_is_one_clean_sequence() {
115 assert_eq!(
116 sequence(Notice::Osc777, "Cellix · api", "Allow Bash?", false),
117 "\x1b]777;notify;Cellix · api;Allow Bash?\x1b\\"
118 );
119 assert_eq!(
120 sequence(Notice::Osc9, "Cellix · api", "Plan ready", false),
121 "\x1b]9;Cellix · api: Plan ready\x1b\\"
122 );
123 assert_eq!(
125 sequence(Notice::Osc777, "a;b", "run\x1b]0;x\x07 now\nplease", false),
126 "\x1b]777;notify;a,b;run ]0;x now please\x1b\\"
127 );
128 let kitty = sequence(Notice::Osc99, "Cellix", "Done", false);
129 assert!(kitty.contains("p=title;Cellix\x1b\\") && kitty.contains("p=body;Done\x1b\\"));
130 }
131
132 #[test]
133 fn inside_tmux_it_goes_past_tmux() {
134 assert_eq!(
135 sequence(Notice::Osc9, "Cellix", "Done", true),
136 "\x1bPtmux;\x1b\x1b]9;Cellix: Done\x1b\x1b\\\x1b\\"
137 );
138 }
139}