use std::io;
use super::emit::cluster_start;
use super::predicates::can_clear_with;
use crate::ansi;
use crate::cell::Cell;
use crate::renderer::caps::Optimizations;
use crate::renderer::{RenderBuffer, Renderer};
use crate::text::WidthMode;
impl Renderer {
pub(super) fn uncertain_bail(
&self,
new_line: &[Cell],
old_line: Option<&[Cell]>,
first_diff: usize,
) -> Option<usize> {
let new_at = self.uncertain_from(new_line);
let old_end = new_at.unwrap_or(new_line.len());
let old_at = old_line
.and_then(|l| l.get(first_diff..l.len().min(old_end)))
.and_then(|l| self.uncertain_from(l))
.map(|at| first_diff + at);
let at = old_at.or(new_at)?;
if at > first_diff {
(at..new_line.len()).find(|&x| {
old_line
.and_then(|l| l.get(x))
.is_none_or(|o| o != &new_line[x])
})?;
}
Some(cluster_start(new_line, at))
}
pub(super) fn uncertain_from(&self, line: &[Cell]) -> Option<usize> {
if self.width_mode != WidthMode::Wc {
return None;
}
(0..line.len()).find(|&x| self.width_is_uncertain(&line[x]))
}
pub(crate) fn move_to_resting(
&mut self,
out: &mut Vec<u8>,
buf: &RenderBuffer,
y: u16,
x: u16,
) -> io::Result<()> {
if self.walk_to_uncertain(out, buf, y, x)? {
Ok(())
} else {
self.move_to(out, buf, y, x)
}
}
pub(crate) fn walk_to_uncertain(
&mut self,
out: &mut Vec<u8>,
buf: &RenderBuffer,
y: u16,
x: u16,
) -> io::Result<bool> {
let Some(from) = buf
.line(y)
.and_then(|l| self.uncertain_from(&l[..l.len().min(x as usize)]))
else {
return Ok(false);
};
let line = buf.line(y).expect("row scanned above");
self.move_to(out, buf, y, from as u16)?;
let stop = cluster_start(line, x as usize);
if stop > from {
ansi::mode::Mode::AUTO_WRAP.reset(out)?;
if self.emit_range(out, buf, line, from, stop - 1, true)? {
ansi::cursor::write_cuf(out, stop as u16 - self.cur.pos().x)?;
}
ansi::mode::Mode::AUTO_WRAP.set(out)?;
self.reset_pen(out)?;
}
self.cur.x = None;
Ok(true)
}
pub(super) fn repaint_tail(
&mut self,
out: &mut Vec<u8>,
new_buf: &RenderBuffer,
new_line: &[Cell],
y: u16,
from: usize,
) -> io::Result<()> {
let width = new_line.len();
let blank = &new_line[width - 1];
let bce = self.opts.contains(Optimizations::BCE);
self.move_to(out, new_buf, y, from as u16)?;
let mut last = width - 1;
if can_clear_with(blank, bce) {
self.update_pen(out, Some(blank))?;
ansi::screen::write_erase_to_eol(out)?;
while last > from && &new_line[last] == blank {
last -= 1;
}
if &new_line[last] == blank {
self.reanchor_to_row_start(out);
return Ok(());
}
}
ansi::mode::Mode::AUTO_WRAP.reset(out)?;
self.emit_range(out, new_buf, new_line, from, last, true)?;
ansi::mode::Mode::AUTO_WRAP.set(out)?;
self.reanchor_to_row_start(out);
Ok(())
}
}