use super::*;
#[derive(Default)]
pub(super) struct WipeSniffer {
carry: Vec<u8>,
}
pub(super) const WIPE_CARRY_MAX: usize = 64;
impl WipeSniffer {
pub(super) fn scan(&mut self, chunk: &[u8], mut alt: bool) -> Vec<Wipe> {
let mut data = std::mem::take(&mut self.carry);
let carried = data.len();
data.extend_from_slice(chunk);
let mut wipes = Vec::new();
let mut wipe = |end: usize, screen: bool, hist: bool| {
let end = end.saturating_sub(carried);
wipes.push(Wipe { end, screen, hist });
};
let mut i = 0;
while i < data.len() {
if data[i] != 0x1b {
i += 1;
continue;
}
let Some(&next) = data.get(i + 1) else {
self.carry = data.split_off(i);
return wipes;
};
match next {
b'c' => {
wipe(i + 2, true, true);
alt = false;
i += 2;
}
b'[' => {
let mut j = i + 2;
while j < data.len()
&& !(0x40..=0x7e).contains(&data[j])
&& !matches!(data[j], 0x18 | 0x1a | 0x1b)
{
j += 1;
}
if j >= data.len() {
if data.len() - i <= WIPE_CARRY_MAX {
self.carry = data.split_off(i);
}
return wipes;
}
if matches!(data[j], 0x18 | 0x1a | 0x1b) {
i = if data[j] == 0x1b { j } else { j + 1 };
continue;
}
let params = &data[i + 2..j];
match data[j] {
b'J' if params == b"2" && !alt => wipe(j + 1, true, false),
b'J' if params == b"3" && !alt => wipe(j + 1, false, true),
fin @ (b'h' | b'l') if params.first() == Some(&b'?') => {
for p in params[1..].split(|&c| c == b';') {
if p == b"47" || p == b"1047" || p == b"1049" {
alt = fin == b'h';
}
}
}
_ => {}
}
i = j + 1;
}
_ => i += 2,
}
}
wipes
}
}
pub(super) const CAPTURE_SLACK: i32 = 2;
pub(super) enum RowAt {
Line(i64),
Unknown { later: usize },
}
impl CaptureRows {
pub(super) fn record(
&mut self,
term: &mut Term<VoidListener>,
clock: &mut ScrollClock,
text: &str,
) -> u64 {
let top = term.grid().topmost_line().0;
let mut line = term.grid().cursor.point.line.0 - 1;
while line > top && row_wraps(term, line - 1) {
line -= 1;
}
let id = next_mark_id();
let row = Some(clock.tick(term) + i64::from(line));
if self
.rows
.last_key_value()
.is_none_or(|(_, last)| last.row != row)
{
self.line += 1;
}
let text = text.trim_end().to_owned();
let line = self.line;
self.rows.insert(id, CapturedRow { row, text, line });
while self.rows.len() > crate::widgets::captures::CAPTURES_MAX {
self.rows.pop_first();
}
id
}
pub(super) fn line(&self, id: u64, now: i64, needle: &str) -> Option<RowAt> {
let captured = self.rows.get(&id)?;
Some(match captured.row {
Some(row) => RowAt::Line(row - now),
None => {
let mut later: Vec<u64> = self
.rows
.range((std::ops::Bound::Excluded(id), std::ops::Bound::Unbounded))
.filter(|(_, c)| c.line != captured.line && c.text.starts_with(needle))
.map(|(_, c)| c.line)
.collect();
later.dedup();
RowAt::Unknown { later: later.len() }
}
})
}
pub(super) fn follow_wipe(&mut self, wipe: Wipe, now: i64) {
self.rows.retain(|_, captured| match captured.row {
Some(row) => {
let line = row - now;
!((line >= 0 && wipe.screen) || (line < 0 && wipe.hist))
}
None => !wipe.hist,
});
}
pub(super) fn reflow(
&mut self,
term: &mut Term<VoidListener>,
clock: &mut ScrollClock,
resize: impl FnOnce(&mut Term<VoidListener>),
) {
if self.rows.is_empty() {
resize(term);
return;
}
if term.mode().contains(TermMode::ALT_SCREEN) {
resize(term);
self.rows
.values_mut()
.for_each(|captured| captured.row = None);
return;
}
let now = clock.tick(term);
let before = line_starts_upward(term);
let top = i64::from(term.grid().topmost_line().0);
let cursor = i64::from(term.grid().cursor.point.line.0);
type Held = (u64, String, u64, Option<Result<usize, i64>>);
let held: Vec<Held> = std::mem::take(&mut self.rows)
.into_iter()
.filter_map(
|(
id,
CapturedRow {
row,
text,
line: serial,
},
)| {
let Some(row) = row else {
return Some((id, text, serial, None));
};
let line = row - now;
let at = if line > cursor {
Err(line)
} else {
Ok(before.partition_point(|&s| i64::from(s) > line))
};
(line >= top).then_some((id, text, serial, Some(at)))
},
)
.collect();
resize(term);
let now = clock.tick(term);
let after = line_starts_upward(term);
for (id, text, serial, at) in held {
let row = match at {
None => None,
Some(Ok(k)) => match after.get(k) {
Some(&l) => Some(now + i64::from(l)),
None => continue,
},
Some(Err(line)) => Some(now + line),
};
self.rows.insert(
id,
CapturedRow {
row,
text,
line: serial,
},
);
}
}
}
pub(super) fn line_starts_upward(term: &Term<VoidListener>) -> Vec<i32> {
let top = term.grid().topmost_line().0;
let cursor = term.grid().cursor.point.line.0;
(top..=cursor)
.rev()
.filter(|&l| l == top || !row_wraps(term, l - 1))
.collect()
}
impl PtyTerminal {
pub fn find_captured_line(&self, needle: &str, anchor: Option<u64>) -> Option<i32> {
if needle.is_empty() {
return None;
}
let term = self.term.lock();
let cols = term.columns();
let wraps = |line: i32| {
cols > 0
&& term.grid()[Point::new(Line(line), Column(cols - 1))]
.flags
.contains(Flags::WRAPLINE)
};
let top = term.grid().topmost_line().0;
let bottom = term.screen_lines() as i32 - 1;
let starts = |line: i32| {
if !(top..=bottom).contains(&line) || (line > top && wraps(line - 1)) {
return false;
}
let (text, _) = row_text_and_cols(&term, line);
let t = text.trim_end();
!t.is_empty() && (t.starts_with(needle) || (wraps(line) && needle.starts_with(t)))
};
let Some(id) = anchor else {
return (top..=bottom).rev().find(|&line| starts(line));
};
let now = self.clock_now(&term);
let at = match self.capture_rows.lock().unwrap().line(id, now, needle)? {
RowAt::Line(at) => at,
RowAt::Unknown { later } => {
return (top..=bottom).rev().filter(|&line| starts(line)).nth(later);
}
};
if at < i64::from(top) {
return None;
}
let at = at.min(i64::from(bottom)) as i32;
(0..=CAPTURE_SLACK).find_map(|d| [at - d, at + d].into_iter().find(|&line| starts(line)))
}
pub fn rewind(&self) -> &Arc<std::sync::Mutex<crate::rewind::RewindBuffer>> {
&self.rewind
}
}