uncurses/ansi/cwd.rs
1//! Current working directory notifications through OSC 7.
2//!
3//! ## Category
4//!
5//! OSC 7 communicates a process working-directory URL to the terminal so shell
6//! integration can associate panes, tabs, or windows with a path.
7//!
8//! ## OSC framing
9//!
10//! The writer emits `ESC ] 7 ; <url> BEL`. The URL payload is passed through
11//! unchanged and should normally be a `file://host/path` URL.
12//!
13//! ## Mode interaction
14//!
15//! No terminal mode controls OSC 7 emission. Terminals that do not implement the
16//! notification ignore it as an ordinary OSC string.
17
18use std::io::{self, Write};
19
20/// Notify the terminal of the current working directory with `ESC ] 7 ; <url> BEL`.
21///
22/// `url` is emitted verbatim and should normally be a `file://host/path` URL such as `file://localhost/home/user/project`.
23pub fn write_notify_working_directory<W: Write>(w: &mut W, url: &str) -> io::Result<()> {
24 write!(w, "\x1b]7;{url}\x07")
25}
26
27#[cfg(test)]
28mod tests {
29 use super::*;
30
31 #[test]
32 fn test_cwd() {
33 let mut buf = Vec::new();
34 write_notify_working_directory(&mut buf, "file://localhost/tmp").unwrap();
35 assert_eq!(buf, b"\x1b]7;file://localhost/tmp\x07");
36 }
37}