1
1
mirror of https://github.com/wez/wezterm.git synced 2024-12-24 05:42:03 +03:00

add parse_first_as_vec

Add a convenience function to the escape parser that, like `parse_first`,
matches only the first escape sequence, but instead collects all matching
actions.
This commit is contained in:
Mark Thomas 2019-06-25 08:47:29 +01:00 committed by Wez Furlong
parent c6a14a131e
commit dbb0bd565e

View File

@ -80,6 +80,26 @@ impl Parser {
self.parse(bytes, |action| result.push(action));
result
}
/// Similar to `parse_first` but collects all actions from the first 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;
for (idx, b) in bytes.iter().enumerate() {
self.state_machine.advance(
&mut Performer {
callback: &mut |action| actions.push(action),
},
*b,
);
if !actions.is_empty() {
// if we recognized any actions, record the iterator index
first_idx = Some(idx);
break;
}
}
first_idx.map(|idx| (actions, idx + 1))
}
}
struct Performer<'a, F: FnMut(Action) + 'a> {