termwiz: ensure ST sequence is grouped with OSC
When `parse_first_as_vec` is parsing an OSC sequence (e.g. `SetHyperlink`) that is terminated by the escaped form of ST (`ESC \`), ensure that the ST sequence is included in the returned vector. This is achieved by ensuring the VTParser has returned to the "ground" state: i.e. the stored state after the `ESC` is processed is not enough for `parse_first_as_vec` to terminate. We must also parse the `\` to ensure that we return a complete span to the caller. Fixes https://github.com/markbt/streampager/issues/57
This commit is contained in:
1 parent
cb0822e40f
commit
40df2f14d2
3 files changed
+95
-49
No files matched your search
Generated
+1
-1
@@ -4996,7 +4996,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "vtparse"
|
||||
version = "0.6.1"
|
||||
version = "0.6.2"
|
||||
dependencies = [
|
||||
"k9",
|
||||
"utf8parse",
|
||||
|
||||
+1
-1
@@ -39,7 +39,7 @@ terminfo = "0.7"
|
||||
thiserror = "1.0"
|
||||
unicode-segmentation = "1.8"
|
||||
ucd-trie = "0.1"
|
||||
vtparse = { version="0.6", path="../vtparse" }
|
||||
vtparse = { version="0.6.2", path="../vtparse" }
|
||||
wezterm-bidi = { path = "../bidi", version="0.1" }
|
||||
wezterm-color-types = { path = "../color-types", version="0.1" }
|
||||
wezterm-dynamic = { path = "../wezterm-dynamic" }
|
||||
|
||||
@@ -160,7 +160,9 @@ impl Parser {
|
||||
result
|
||||
}
|
||||
|
||||
/// Similar to `parse_first` but collects all actions from the first sequence.
|
||||
/// Similar to `parse_first` but collects all actions from the first sequence,
|
||||
/// and guarantees the state machine is in the ground state at the end of this
|
||||
/// sequence.
|
||||
pub fn parse_first_as_vec(&mut self, bytes: &[u8]) -> Option<(Vec<Action>, usize)> {
|
||||
let mut actions = Vec::new();
|
||||
let mut first_idx = None;
|
||||
@@ -172,7 +174,7 @@ impl Parser {
|
||||
state: &mut self.state.borrow_mut(),
|
||||
},
|
||||
);
|
||||
if !actions.is_empty() {
|
||||
if !actions.is_empty() && self.state_machine.is_ground() {
|
||||
// if we recognized any actions, record the iterator index
|
||||
first_idx = Some(idx);
|
||||
break;
|
||||
@@ -416,67 +418,111 @@ mod test {
|
||||
// <https://github.com/markbt/streampager/issues/57>
|
||||
#[test]
|
||||
fn osc_st_parse_first_as_vec() {
|
||||
let data = b"\x1b]8;;http://example.com\x1b\\example\x1b]8;;\x1b\\";
|
||||
// This string includes an assitional trailing ST sequence which should
|
||||
// be parsed separately.
|
||||
let data = b"\x1b]8;;http://example.com\x1b\\example\x1b]8;;\x1b\\\x1b\\";
|
||||
let mut p = Parser::new();
|
||||
|
||||
let mut offset = 0;
|
||||
let mut actions = vec![];
|
||||
while let Some((mut act, off)) = p.parse_first_as_vec(&data[offset..]) {
|
||||
actions.append(&mut act);
|
||||
let mut slices = vec![];
|
||||
while let Some((act, off)) = p.parse_first_as_vec(&data[offset..]) {
|
||||
// Store each vec of actions so we can confirm that the ST sequence is bundled with the
|
||||
// OSC SetHyperlink command.
|
||||
actions.push(act);
|
||||
// Additionally store all non-single-character slices so we can confirm these are split
|
||||
// correctly.
|
||||
if off > 1 {
|
||||
slices.push(&data[offset..offset + off]);
|
||||
}
|
||||
offset += off;
|
||||
}
|
||||
|
||||
assert_eq!(
|
||||
slices,
|
||||
vec![
|
||||
b"\x1b]8;;http://example.com\x1b\\".as_slice(),
|
||||
b"\x1b]8;;\x1b\\".as_slice(),
|
||||
b"\x1b\\".as_slice()
|
||||
]
|
||||
);
|
||||
|
||||
k9::snapshot!(
|
||||
actions,
|
||||
r#"
|
||||
[
|
||||
OperatingSystemCommand(
|
||||
SetHyperlink(
|
||||
Some(
|
||||
Hyperlink {
|
||||
params: {},
|
||||
uri: "http://example.com",
|
||||
implicit: false,
|
||||
},
|
||||
[
|
||||
OperatingSystemCommand(
|
||||
SetHyperlink(
|
||||
Some(
|
||||
Hyperlink {
|
||||
params: {},
|
||||
uri: "http://example.com",
|
||||
implicit: false,
|
||||
},
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
Esc(
|
||||
Code(
|
||||
StringTerminator,
|
||||
Esc(
|
||||
Code(
|
||||
StringTerminator,
|
||||
),
|
||||
),
|
||||
),
|
||||
Print(
|
||||
'e',
|
||||
),
|
||||
Print(
|
||||
'x',
|
||||
),
|
||||
Print(
|
||||
'a',
|
||||
),
|
||||
Print(
|
||||
'm',
|
||||
),
|
||||
Print(
|
||||
'p',
|
||||
),
|
||||
Print(
|
||||
'l',
|
||||
),
|
||||
Print(
|
||||
'e',
|
||||
),
|
||||
OperatingSystemCommand(
|
||||
SetHyperlink(
|
||||
None,
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'e',
|
||||
),
|
||||
),
|
||||
Esc(
|
||||
Code(
|
||||
StringTerminator,
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'x',
|
||||
),
|
||||
),
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'a',
|
||||
),
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'm',
|
||||
),
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'p',
|
||||
),
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'l',
|
||||
),
|
||||
],
|
||||
[
|
||||
Print(
|
||||
'e',
|
||||
),
|
||||
],
|
||||
[
|
||||
OperatingSystemCommand(
|
||||
SetHyperlink(
|
||||
None,
|
||||
),
|
||||
),
|
||||
Esc(
|
||||
Code(
|
||||
StringTerminator,
|
||||
),
|
||||
),
|
||||
],
|
||||
[
|
||||
Esc(
|
||||
Code(
|
||||
StringTerminator,
|
||||
),
|
||||
),
|
||||
],
|
||||
]
|
||||
"#
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user