diff --git a/CHANGELOG.md b/CHANGELOG.md index 4e0ae7b4..1c197142 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -108,6 +108,7 @@ - `--sanitize=always` now also sanitizes piped output. `--sanitize=auto` now sanitizes every file type, but only when the output is a terminal, see #4141 (@curious-rabbit) - Show changes in `--diff` mode and the change-marker column in Git repositories that use the SHA-256 object format (`git init --object-format=sha256`), see #4143 (@BlueX888) - Skip untracked files and files outside a Git repository in `--diff` mode instead of printing an empty frame, see #4148 (@thejesh23) +- Recognize the end of an OSC 8 hyperlink in ANSI input, so later text is no longer wrapped in redundant hyperlink-close sequences, see #4136 (@divya0795) ## Other - Use git version of cross. See #3533 (@OctopusET) diff --git a/src/vscreen.rs b/src/vscreen.rs index 54678456..61441772 100644 --- a/src/vscreen.rs +++ b/src/vscreen.rs @@ -125,7 +125,7 @@ impl Attributes { .. } => { if command.starts_with("8;") { - return self.update_with_hyperlink(raw_sequence); + return self.update_with_hyperlink(command, raw_sequence); } /* defer to update_with_unsupported */ } @@ -214,9 +214,9 @@ impl Attributes { false } - fn update_with_hyperlink(&mut self, sequence: &str) -> bool { - if sequence == "8;;" { - // Empty hyperlink ID and HREF -> end of hyperlink. + fn update_with_hyperlink(&mut self, command: &str, sequence: &str) -> bool { + // OSC 8 ; params ; URI -- an empty URI ends the hyperlink (params may be set). + if command.splitn(3, ';').nth(2).is_none_or(str::is_empty) { self.hyperlink.clear(); } else { self.hyperlink.clear(); @@ -1199,4 +1199,33 @@ mod tests { }); assert_eq!(attrs.reverse, ""); } + + #[test] + fn test_osc8_hyperlink_is_tracked_and_closed() { + for (open, close) in [ + ("\x1B]8;;https://example.com/\x1B\\", "\x1B]8;;\x1B\\"), + ("\x1B]8;;https://example.com/\x07", "\x1B]8;;\x07"), + ( + "\x1B]8;id=1;https://example.com/\x1B\\", + "\x1B]8;id=1;\x1B\\", + ), + ] { + let mut style = crate::vscreen::AnsiStyle::new(); + + // Opening a hyperlink must be tracked so it can be re-emitted and closed. + for seq in EscapeSequenceIterator::new(open) { + assert!(style.update(seq)); + } + assert_eq!(style.to_string(), open); + assert_eq!(style.to_reset_sequence(), "\x1B]8;;\x1B\\"); + + // An OSC 8 with an empty URI ends the hyperlink, so nothing is left + // to re-emit before the next chunk or to close after it. + for seq in EscapeSequenceIterator::new(close) { + assert!(style.update(seq)); + } + assert_eq!(style.to_string(), "", "after {close:?}"); + assert_eq!(style.to_reset_sequence(), "", "after {close:?}"); + } + } }