Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

fix(term): stop reading an OSC 133;D without a status as success

RoversXa328d329b5d1
diff --git a/term/src/terminal.rs b/term/src/terminal.rs
index b0dee4df..879c050e 100644
--- a/term/src/terminal.rs
+++ b/term/src/terminal.rs
@@ -72,8 +72,8 @@ pub enum Alert {
/// A change to the progress bar state
Progress(Progress),
/// The shell reported (OSC 133) that a command finished, with the
- /// status it gave, or `None` when a new prompt ended the command
- /// without one.
+ /// status it gave, or `None` when it gave none: its end mark left the
+ /// status out, or a new prompt ended the command.
CommandFinished {
exit_code: Option<i32>,
},
diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs
index 79f918b8..fbaecdb5 100644
--- a/term/src/terminalstate/performer.rs
+++ b/term/src/terminalstate/performer.rs
@@ -1106,7 +1106,7 @@ impl<'a> Performer<'a> {
FinalTermSemanticPrompt::CommandStatus { status, aid },
) => {
if self.counts_shell_mark(aid.as_deref()) {
- self.finish_shell_command(Some(status));
+ self.finish_shell_command(status);
}
}
diff --git a/term/src/test/mod.rs b/term/src/test/mod.rs
index 8bb80ca4..24f5a552 100644
--- a/term/src/test/mod.rs
+++ b/term/src/test/mod.rs
@@ -414,6 +414,23 @@ fn shell_commands_follow_the_osc_133_marks() {
assert_eq!(finished, vec![Some(2), None, Some(5), Some(4)]);
}
+#[test]
+fn a_command_end_without_a_status_is_not_a_success() {
+ let mut term = TestTerm::new(4, 20, 0);
+ term.print("\x1b]133;A;aid=1\x07$ \x1b]133;B\x07\x1b]133;C;\x07");
+ // OSC 133;D may leave the status out: the command ended, how is unknown.
+ term.print("\x1b]133;D;aid=1\x07");
+ assert_eq!(term.shell_commands().finished, 1);
+ assert_eq!(term.shell_commands().last_exit, None);
+
+ // Without its status, a nested shell's end still keeps its aid, and is
+ // not taken for the end of the outer shell's command.
+ term.print("\x1b]133;A;aid=1\x07\x1b]133;C;\x07\x1b]133;A;aid=2\x07");
+ term.print("\x1b]133;C;\x07\x1b]133;D;aid=2\x07");
+ assert!(term.shell_commands().running);
+ assert_eq!(term.shell_commands().finished, 1);
+}
+
#[test]
fn application_palette_override_tracks_osc_set_query_and_reset() {
let mut term = TestTerm::new(4, 8, 0);
diff --git a/wezterm-escape-parser/src/osc.rs b/wezterm-escape-parser/src/osc.rs
index e2113c58..0d9aa4ca 100644
--- a/wezterm-escape-parser/src/osc.rs
+++ b/wezterm-escape-parser/src/osc.rs
@@ -746,7 +746,8 @@ pub enum FinalTermSemanticPrompt {
/// Indicates the result of the command
CommandStatus {
- status: i32,
+ /// The command's exit status, which the shell may leave out.
+ status: Option<i32>,
aid: Option<String>,
},
}
@@ -771,7 +772,13 @@ impl FinalTermSemanticPrompt {
let mut params = HashMap::new();
use core::convert::TryInto;
- for s in osc.iter().skip(if param == "D" { 3 } else { 2 }) {
+ // D's first field is the exit status, unless it was left out and the
+ // field is already one of the options.
+ let status_field = match osc.get(2) {
+ Some(field) if param == "D" && !field.contains(&b'=') => Some(*field),
+ _ => None,
+ };
+ for s in osc.iter().skip(if status_field.is_some() { 3 } else { 2 }) {
if let Some(equal) = s.iter().position(|c| *c == b'=') {
let key = &s[..equal];
let value = &s[equal + 1..];
@@ -798,13 +805,10 @@ impl FinalTermSemanticPrompt {
}
if param == "D" {
- let status = match osc.get(2).map(|&p| p) {
- Some(s) => match str::from_utf8(s) {
- Ok(s) => s.parse().unwrap_or(0),
- _ => 0,
- },
- _ => 0,
- };
+ // Left out or unreadable, the status is unknown, not a success.
+ let status = status_field
+ .and_then(|s| str::from_utf8(s).ok())
+ .and_then(|s| s.parse().ok());
return Ok(Self::CommandStatus {
status,
@@ -872,14 +876,23 @@ impl Display for FinalTermSemanticPrompt {
}
}
Self::CommandStatus {
- status,
+ status: Some(status),
aid: Some(aid),
} => {
write!(f, "D;{};err={};aid={}", status, status, aid)?;
}
- Self::CommandStatus { status, aid: None } => {
+ Self::CommandStatus {
+ status: Some(status),
+ aid: None,
+ } => {
write!(f, "D;{}", status)?;
}
+ Self::CommandStatus { status: None, aid } => {
+ write!(f, "D")?;
+ if let Some(aid) = aid {
+ write!(f, ";aid={}", aid)?;
+ }
+ }
}
Ok(())
}
@@ -1760,7 +1773,7 @@ mod test {
parse(&["133", "D", "1"], "\x1b]133;D;1\x1b\\"),
OperatingSystemCommand::FinalTermSemanticPrompt(
FinalTermSemanticPrompt::CommandStatus {
- status: 1,
+ status: Some(1),
aid: None
}
)
@@ -1770,12 +1783,61 @@ mod test {
parse(&["133", "D", "0"], "\x1b]133;D;0\x1b\\"),
OperatingSystemCommand::FinalTermSemanticPrompt(
FinalTermSemanticPrompt::CommandStatus {
- status: 0,
+ status: Some(0),
aid: None
}
)
);
+ // The status is optional; left out, it is unknown rather than 0.
+ assert_eq!(
+ parse(&["133", "D"], "\x1b]133;D\x1b\\"),
+ OperatingSystemCommand::FinalTermSemanticPrompt(
+ FinalTermSemanticPrompt::CommandStatus {
+ status: None,
+ aid: None
+ }
+ )
+ );
+
+ assert_eq!(
+ parse(&["133", "D", "aid=23"], "\x1b]133;D;aid=23\x1b\\"),
+ OperatingSystemCommand::FinalTermSemanticPrompt(
+ FinalTermSemanticPrompt::CommandStatus {
+ status: None,
+ aid: Some("23".to_owned())
+ }
+ )
+ );
+
+ // An empty or unreadable status, and options in its place, are none.
+ for (fields, encoded, aid) in [
+ (&["133", "D", ""][..], "\x1b]133;D\x1b\\", None),
+ (
+ &["133", "D", "", "aid=23"][..],
+ "\x1b]133;D;aid=23\x1b\\",
+ Some("23"),
+ ),
+ (&["133", "D", "abc"][..], "\x1b]133;D\x1b\\", None),
+ (
+ &["133", "D", "err=CANCEL", "aid=23"][..],
+ "\x1b]133;D;aid=23\x1b\\",
+ Some("23"),
+ ),
+ ] {
+ assert_eq!(
+ parse(fields, encoded),
+ OperatingSystemCommand::FinalTermSemanticPrompt(
+ FinalTermSemanticPrompt::CommandStatus {
+ status: None,
+ aid: aid.map(str::to_owned)
+ }
+ ),
+ "{:?}",
+ fields
+ );
+ }
+
assert_eq!(
parse(
&["133", "D", "0", "aid=23"],
@@ -1783,7 +1845,7 @@ mod test {
),
OperatingSystemCommand::FinalTermSemanticPrompt(
FinalTermSemanticPrompt::CommandStatus {
- status: 0,
+ status: Some(0),
aid: Some("23".to_owned())
}
)
@@ -1796,7 +1858,7 @@ mod test {
),
OperatingSystemCommand::FinalTermSemanticPrompt(
FinalTermSemanticPrompt::CommandStatus {
- status: 1,
+ status: Some(1),
aid: Some("23".to_owned())
}
)