1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
use std::borrow::Cow;
use std::fmt::Write;
use crate::term_detection;
/// Utility for integrating with terminal emulators.
#[derive(Default)]
pub(crate) struct TerminalIntegration {
/// Info about the hosting terminal.
term: term_detection::TerminalInfo,
}
#[allow(dead_code)]
impl TerminalIntegration {
/// Creates a new terminal integration utility.
///
/// # Arguments
///
/// * `term_info` - Information about the terminal capabilities.
pub const fn new(term_info: term_detection::TerminalInfo) -> Self {
Self { term: term_info }
}
/// Returns the terminal escape sequence that should be emitted to initialize terminal
/// integration.
pub fn initialize(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;P;HasRichCommandDetection=True\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted before the prompt.
pub fn pre_prompt(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;A\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence to report the current working directory.
pub fn report_cwd(&self, cwd: &std::path::Path) -> Cow<'_, str> {
if self.term.supports_osc_633 {
let escaped_cwd_str = osc_633_escape(cwd.to_string_lossy().as_ref());
format!("\x1b]633;P;Cwd={escaped_cwd_str}\x1b\\").into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted before executing a command,
/// but after the prompt and the user has finished entering input.
///
/// # Arguments
///
/// * `command` - The command that is about to be executed.
pub fn pre_exec_command(&self, command: &str) -> Cow<'_, str> {
if self.term.supports_osc_633 {
let mut escaped_command = osc_633_escape(command);
escaped_command.insert_str(0, "\x1b]633;E;");
if let Some(session_nonce) = &self.term.session_nonce {
escaped_command.push(';');
escaped_command.push_str(session_nonce);
}
escaped_command.push_str("\x1b\\\x1b]633;C\x1b\\");
escaped_command.into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted after executing a command.
pub fn post_exec_command(&self, exit_code: i32) -> Cow<'_, str> {
if self.term.supports_osc_633 {
std::format!("\x1b]633;D;{exit_code}\x1b\\").into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted after the prompt.
pub fn post_prompt(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;B\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted before the continuation prompt.
pub fn pre_input_line_continuation(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;F\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted after the input line
/// continuation.
pub fn post_input_line_continuation(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;G\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted before the right-side prompt.
pub fn pre_right_prompt(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;H\x1b\\".into()
} else {
"".into()
}
}
/// Returns the terminal escape sequence that should be emitted after the right-side prompt.
pub fn post_right_prompt(&self) -> Cow<'_, str> {
if self.term.supports_osc_633 {
"\x1b]633;I\x1b\\".into()
} else {
"".into()
}
}
}
/// Escapes a string for safe inclusion in an OSC 633 escape sequence.
/// Reference: <https://github.com/microsoft/vscode/blob/main/src/vs/workbench/contrib/terminal/common/scripts/shellIntegration-bash.sh>
fn osc_633_escape(command: &str) -> String {
let mut result = String::new();
for c in command.chars() {
match c {
// Escape ASCII control characters (< 0x1f, i.e., < 31)
'\x00'..='\x1e' => {
let _ = write!(result, r"\x{:02x}", c as u8);
}
// Escape backslash with an extra prefixed backslash
'\\' => result.push_str(r"\\"),
// Escape semicolon via \xNN syntax (like control chars)
';' => result.push_str(r"\x3b"),
// Keep other characters as-is
_ => result.push(c),
}
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn osc_633_escape_basic() {
// Test simple alphanumeric string
assert_eq!(osc_633_escape("echo hello"), "echo hello");
assert_eq!(osc_633_escape("ls -la"), "ls -la");
}
#[test]
fn osc_633_escape_semicolon() {
// Semicolons should be escaped
assert_eq!(osc_633_escape("cmd1; cmd2"), r"cmd1\x3b cmd2");
assert_eq!(osc_633_escape(";"), r"\x3b");
assert_eq!(osc_633_escape("a;b;c"), r"a\x3bb\x3bc");
}
#[test]
fn osc_633_escape_backslash() {
// Backslashes should be escaped
assert_eq!(osc_633_escape(r"echo \n"), r"echo \\n");
assert_eq!(osc_633_escape(r"\"), r"\\");
assert_eq!(osc_633_escape(r"C:\path\to\file"), r"C:\\path\\to\\file");
}
#[test]
fn osc_633_escape_control_chars() {
// ASCII control characters (0x00-0x1e, i.e., 0-30) should be escaped
assert_eq!(osc_633_escape("\x00"), r"\x00");
assert_eq!(osc_633_escape("\x01"), r"\x01");
assert_eq!(osc_633_escape("\t"), r"\x09"); // tab
assert_eq!(osc_633_escape("\n"), r"\x0a"); // newline
assert_eq!(osc_633_escape("\r"), r"\x0d"); // carriage return
assert_eq!(osc_633_escape("\x1e"), r"\x1e"); // last control char (30)
// 0x1f (31) should NOT be escaped as a control char (not < 31)
assert_eq!(osc_633_escape("\x1f"), "\x1f");
// Space (0x20, 32) should NOT be escaped
assert_eq!(osc_633_escape(" "), " ");
}
#[test]
fn osc_633_escape_mixed() {
// Test combinations of different escape scenarios
assert_eq!(
osc_633_escape("echo\nhello; world\\n"),
r"echo\x0ahello\x3b world\\n"
);
assert_eq!(osc_633_escape("cmd\t\t; \\path"), r"cmd\x09\x09\x3b \\path");
// Test with null bytes
assert_eq!(osc_633_escape("a\x00b\x01c"), r"a\x00b\x01c");
// Test all three special cases together
assert_eq!(osc_633_escape("\\;\n"), r"\\\x3b\x0a");
}
#[test]
fn osc_633_escape_empty() {
assert_eq!(osc_633_escape(""), "");
}
#[test]
fn osc_633_escape_unicode() {
// Unicode characters should pass through unchanged
assert_eq!(osc_633_escape("echo 你好"), "echo 你好");
assert_eq!(osc_633_escape("café"), "café");
assert_eq!(osc_633_escape("🦀"), "🦀");
// But should still escape special chars
assert_eq!(osc_633_escape("你好;世界"), r"你好\x3b世界");
}
}