-
-
Notifications
You must be signed in to change notification settings - Fork 137
Strip OSC and DCS sequences to support e.g. OSC 8 hyperlinks over tmux. #280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khoek
wants to merge
2
commits into
console-rs:main
Choose a base branch
from
khoek:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−35
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,142 @@ use core::{ | |
| str::CharIndices, | ||
| }; | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| struct OscSequence; | ||
|
|
||
| impl EscSequence for OscSequence { | ||
| const START: char = ']'; | ||
|
|
||
| fn on_escape(next: Option<char>) -> EscAction { | ||
| if matches!(next, Some('\\')) { | ||
| EscAction { | ||
| consume_next: true, | ||
| end: true, | ||
| } | ||
| } else { | ||
| EscAction { | ||
| consume_next: false, | ||
| end: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn on_char(c: char) -> EscAction { | ||
| EscAction { | ||
| consume_next: false, | ||
| end: c == '\u{07}', | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[derive(Clone, Copy, Debug)] | ||
| struct DcsSequence; | ||
|
|
||
| impl EscSequence for DcsSequence { | ||
| const START: char = 'P'; | ||
|
|
||
| fn on_escape(next: Option<char>) -> EscAction { | ||
| match next { | ||
| Some('\\') => EscAction { | ||
| consume_next: true, | ||
| end: true, | ||
| }, | ||
| None => EscAction { | ||
| consume_next: false, | ||
| end: true, | ||
| }, | ||
| Some('\u{1b}') => EscAction { | ||
| consume_next: true, | ||
| end: false, | ||
| }, | ||
| _ => EscAction { | ||
| consume_next: false, | ||
| end: false, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| fn on_char(_c: char) -> EscAction { | ||
| EscAction { | ||
| consume_next: false, | ||
| end: false, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| trait EscSequence { | ||
| fn on_escape(next: Option<char>) -> EscAction; | ||
| fn on_char(c: char) -> EscAction; | ||
| const START: char; | ||
| } | ||
|
|
||
| struct EscAction { | ||
| consume_next: bool, | ||
| end: bool, | ||
| } | ||
|
|
||
| fn consume_end_exclusive<S: EscSequence>( | ||
|
djc marked this conversation as resolved.
|
||
| it: &mut Peekable<CharIndices<'_>>, | ||
| start: usize, | ||
| ) -> usize { | ||
| let mut end = start + 1; | ||
| let Some((idx, start_char)) = it.next() else { | ||
| return end; | ||
| }; | ||
| if start_char != S::START { | ||
| return end; | ||
| } | ||
| end = idx + 1; | ||
|
|
||
| while let Some((idx, c)) = it.next() { | ||
| end = idx + c.len_utf8(); | ||
| match c { | ||
| '\u{9c}' => return end, | ||
| '\u{1b}' => { | ||
| let action = S::on_escape(it.peek().map(|(_, next)| *next)); | ||
| if action.consume_next { | ||
| if let Some((next_idx, _)) = it.peek() { | ||
| end = *next_idx + 1; | ||
| it.next(); | ||
| } | ||
| } | ||
| if action.end { | ||
| return end; | ||
| } | ||
| } | ||
| _ => {} | ||
| } | ||
| if S::on_char(c).end { | ||
| return end; | ||
| } | ||
|
Comment on lines
+114
to
+116
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be part of the |
||
| } | ||
|
|
||
| end | ||
| } | ||
|
|
||
| fn find_dfa_end_exclusive_after_entry(it: &mut Peekable<CharIndices<'_>>) -> Option<usize> { | ||
| let mut state = State::S1; | ||
| let mut maybe_end = None; | ||
|
|
||
| loop { | ||
| let item = it.peek(); | ||
|
|
||
| if let Some((idx, c)) = item { | ||
| state.transition(*c); | ||
|
|
||
| if state.is_final() { | ||
| maybe_end = Some(*idx); | ||
| } | ||
| } | ||
|
|
||
| if state.is_trapped() || item.is_none() { | ||
| return maybe_end.map(|end| end + 1); | ||
| } | ||
|
|
||
| it.next(); | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Default)] | ||
| enum State { | ||
| #[default] | ||
|
|
@@ -143,42 +279,36 @@ impl FusedIterator for Matches<'_> {} | |
|
|
||
| fn find_ansi_code_exclusive(it: &mut Peekable<CharIndices>) -> Option<(usize, usize)> { | ||
| 'outer: loop { | ||
| if let (start, '\u{1b}') | (start, '\u{9b}') = it.peek()? { | ||
| let start = *start; | ||
| let mut state = State::default(); | ||
| let mut maybe_end = None; | ||
|
|
||
| loop { | ||
| let item = it.peek(); | ||
|
|
||
| if let Some((idx, c)) = item { | ||
| state.transition(*c); | ||
|
|
||
| if state.is_final() { | ||
| maybe_end = Some(*idx); | ||
| let (start, entry) = *it.peek()?; | ||
| match entry { | ||
| '\u{1b}' => { | ||
| it.next(); | ||
| match it.peek() { | ||
| Some((_, OscSequence::START)) => { | ||
| return Some((start, consume_end_exclusive::<OscSequence>(it, start))) | ||
| } | ||
| } | ||
|
|
||
| // The match is greedy so run till we hit the trap state no matter what. A valid | ||
| // match is just one that was final at some point | ||
| if state.is_trapped() || item.is_none() { | ||
| match maybe_end { | ||
| Some(end) => { | ||
| // All possible final characters are a single byte so it's safe to make | ||
| // the end exclusive by just adding one | ||
| return Some((start, end + 1)); | ||
| Some((_, DcsSequence::START)) => { | ||
| return Some((start, consume_end_exclusive::<DcsSequence>(it, start))) | ||
| } | ||
| _ => { | ||
| if let Some(end) = find_dfa_end_exclusive_after_entry(it) { | ||
| return Some((start, end)); | ||
| } | ||
| // The character we are peeking right now might be the start of a match so | ||
| // we want to continue the loop without popping off that char | ||
| None => continue 'outer, | ||
| continue 'outer; | ||
| } | ||
| } | ||
|
|
||
| } | ||
| '\u{9b}' => { | ||
| it.next(); | ||
| if let Some(end) = find_dfa_end_exclusive_after_entry(it) { | ||
| return Some((start, end)); | ||
| } | ||
| continue 'outer; | ||
| } | ||
| _ => { | ||
| it.next(); | ||
| } | ||
| } | ||
|
|
||
| it.next(); | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -296,12 +426,16 @@ mod tests { | |
| use proptest::prelude::*; | ||
| use regex::Regex; | ||
|
|
||
| // The manual dfa `State` is a handwritten translation from the previously used regex. That | ||
| // regex is kept here and used to ensure that the new matches are the same as the old | ||
| // The manual dfa `State` is a handwritten translation of the following regex. | ||
| static STRIP_ANSI_RE: Lazy<Regex> = Lazy::new(|| { | ||
| Regex::new( | ||
| r"[\x1b\x9b]([()][012AB]|[\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-PRZcf-nqry=><])", | ||
| ) | ||
| Regex::new(concat!( | ||
| r"(?s)(?:", | ||
| r"\x1b\].*?(?:\x07|\x9c|\x1b\\|\z)|", | ||
| r"\x1bP(?:[^\x1b\x9c]|\x1b\x1b|\x1b[^\x1b\\])*?(?:\x9c|\x1b\\|\x1b\z|\z)|", | ||
| r"[\x1b\x9b]([()][012AB]|[\[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?", | ||
| r"[0-9A-PRZcf-nqry=><])", | ||
| r")", | ||
| )) | ||
| .unwrap() | ||
| }); | ||
|
|
||
|
|
@@ -415,6 +549,28 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn strip_osc8_hyperlink_st() { | ||
| let s = "\x1b]8;;file:///tmp/test\x1b\\hello\x1b]8;;\x1b\\"; | ||
| assert_eq!(strip_ansi_codes(s).as_ref(), "hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn strip_osc8_hyperlink_bel() { | ||
| let s = "\x1b]8;;file:///tmp/test\x07hello\x1b]8;;\x07"; | ||
| assert_eq!(strip_ansi_codes(s).as_ref(), "hello"); | ||
| } | ||
|
|
||
| #[test] | ||
| fn strip_tmux_passthrough_dcs() { | ||
| let open = "\x1bPtmux;\x1b\x1b\x1b]8;;file:///tmp/test\x1b\x1b\\\x1b\\"; | ||
| let close = "\x1bPtmux;\x1b\x1b\x1b]8;;\x1b\x1b\\\x1b\\"; | ||
| assert_eq!( | ||
| strip_ansi_codes(&format!("{open}hello{close}")).as_ref(), | ||
| "hello" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_ansi_iter_re_vt100() { | ||
| let s = "\x1b(0lpq\x1b)Benglish"; | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggest simplifying this to be more like: