Skip to main content

uncurses/ansi/
notification.rs

1//! Desktop notification OSC encoders.
2//!
3//! ## Category
4//!
5//! This module writes simple OSC 9 notifications and metadata-bearing OSC 99
6//! notifications. Both forms are zero-width terminal string controls.
7//!
8//! ## OSC framing
9//!
10//! Writers use the 7-bit OSC introducer and BEL terminator. OSC 99 joins
11//! metadata fields with `:` before the final body field.
12//!
13//! ## Mode interaction
14//!
15//! Notification support is not advertised through a mode here. Terminals that do
16//! not implement the OSC numbers ignore the strings.
17
18use std::io::{self, Write};
19
20/// Send a simple notification with `ESC ] 9 ; <body> BEL`.
21///
22/// `body` is emitted verbatim as the notification text.
23pub fn write_notify<W: Write>(w: &mut W, body: &str) -> io::Result<()> {
24    write!(w, "\x1b]9;{body}\x07")
25}
26
27/// Send a metadata-bearing notification with `ESC ] 99 ; <metadata> ; <body> BEL`.
28///
29/// `metadata` entries are joined with `:` and emitted before the body field; `body` is emitted verbatim.
30pub fn write_desktop_notification<W: Write>(
31    w: &mut W,
32    body: &str,
33    metadata: &[&str],
34) -> io::Result<()> {
35    w.write_all(b"\x1b]99;")?;
36    for (i, m) in metadata.iter().enumerate() {
37        if i > 0 {
38            w.write_all(b":")?;
39        }
40        w.write_all(m.as_bytes())?;
41    }
42    write!(w, ";{body}\x07")
43}