Skip to content

A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.

perf(term): write plain text to rows in runs

RoversXd3728f919df4
diff --git a/mux/src/lib.rs b/mux/src/lib.rs
index 153ce8f4..07d12ddf 100644
--- a/mux/src/lib.rs
+++ b/mux/src/lib.rs
@@ -817,6 +817,7 @@ fn parse_buffered_data(
// the reader's count whenever nothing is left waiting to be sent.
let mut unapplied = 0usize;
let mut parser = termwiz::escape::parser::Parser::new();
+ parser.set_text_keeps_line_controls(true);
let mut actions = vec![];
let mut hold = false;
let mut action_size = 0;
diff --git a/term/src/screen/scroll_tests.rs b/term/src/screen/scroll_tests.rs
index c29af367..e9e9c50b 100644
--- a/term/src/screen/scroll_tests.rs
+++ b/term/src/screen/scroll_tests.rs
@@ -182,7 +182,7 @@ fn recycled_scrolling_matches_original_rows_and_stable_indices() {
3,
);
if row % 4 == 3 {
- line.set_ascii_cells(
+ line.set_printable_ascii_cells(
0,
&"x".repeat(size.cols),
&CellAttributes::default(),
diff --git a/term/src/terminal.rs b/term/src/terminal.rs
index 879c050e..c05607b3 100644
--- a/term/src/terminal.rs
+++ b/term/src/terminal.rs
@@ -171,9 +171,11 @@ impl Terminal {
// writing to the writer sends data to input of the pty
writer: Box<dyn std::io::Write + Send>,
) -> Terminal {
+ let mut parser = Parser::new();
+ parser.set_text_keeps_line_controls(true);
Terminal {
state: TerminalState::new(size, config, term_program, term_version, writer),
- parser: Parser::new(),
+ parser,
}
}
@@ -192,7 +194,7 @@ impl Terminal {
self.parser
.parse_with_borrowed_text(bytes, |event| match event {
ParsedAction::Action(action) => performer.perform(action),
- ParsedAction::Print(text) => performer.print_text(text),
+ ParsedAction::Print(text) => performer.print_run(text),
});
}
self.state.kitty_scene_refresh();
diff --git a/term/src/terminalstate/mod.rs b/term/src/terminalstate/mod.rs
index 94b8da89..f76222c3 100644
--- a/term/src/terminalstate/mod.rs
+++ b/term/src/terminalstate/mod.rs
@@ -2621,20 +2621,25 @@ impl TerminalState {
let line_idx = screen.phys_row(y);
let line = screen.line_mut(line_idx);
- match line.cells_mut().get(to_copy).cloned() {
- None => Cell::blank(),
- Some(candidate) => {
- if candidate.str() == " " && to_copy > 0 {
- // It's a blank. It may be the second part of
- // a double-wide pair; look ahead of it.
- let prior = &line.cells_mut()[to_copy - 1];
- if prior.width() > 1 {
- prior.clone()
+ // A row of ASCII can give the cell up as it is.
+ if let Some(cell) = line.ascii_cell(to_copy) {
+ cell
+ } else {
+ match line.cells_mut().get(to_copy).cloned() {
+ None => Cell::blank(),
+ Some(candidate) => {
+ if candidate.str() == " " && to_copy > 0 {
+ // It's a blank. It may be the second part of
+ // a double-wide pair; look ahead of it.
+ let prior = &line.cells_mut()[to_copy - 1];
+ if prior.width() > 1 {
+ prior.clone()
+ } else {
+ candidate
+ }
} else {
candidate
}
- } else {
- candidate
}
}
}
diff --git a/term/src/terminalstate/performer.rs b/term/src/terminalstate/performer.rs
index fbaecdb5..0becf71d 100644
--- a/term/src/terminalstate/performer.rs
+++ b/term/src/terminalstate/performer.rs
@@ -15,7 +15,7 @@ use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
use url::Url;
use wezterm_bidi::ParagraphDirectionHint;
use wezterm_cell::{
- grapheme_column_width, is_white_space_grapheme, Cell, CellAttributes, SemanticType,
+ grapheme_column_width, is_white_space_grapheme, Cell, CellAttributes, PlainWidths, SemanticType,
};
use wezterm_escape_parser::csi::{
CharacterPath, EraseInDisplay, Keyboard, KittyKeyboardFlags, KittyKeyboardMode,
@@ -137,16 +137,25 @@ impl<'a> Performer<'a> {
return;
}
- let seqno = self.seqno;
let mut p = std::mem::take(&mut self.print);
+ self.print_now(&p, false);
+ std::mem::swap(&mut self.print, &mut p);
+ self.print.clear();
+ }
+
+ /// Write `text` to the screen. `ascii` says it is known to be printable
+ /// ASCII, which saves looking.
+ fn print_now(&mut self, text: &str, ascii: bool) {
+ let seqno = self.seqno;
let normalized: String;
- let text = if self.config_normalize_output_to_unicode_nfc()
- && is_nfc_quick(p.chars()) != IsNormalized::Yes
+ let text = if !ascii
+ && self.config_normalize_output_to_unicode_nfc()
+ && is_nfc_quick(text.chars()) != IsNormalized::Yes
{
- normalized = p.as_str().nfc().collect();
+ normalized = text.nfc().collect();
normalized.as_str()
} else {
- p.as_str()
+ text
};
// Mixed Unicode must still be segmented as a whole: an ASCII base may
@@ -160,7 +169,7 @@ impl<'a> Performer<'a> {
let ascii_batch = !self.insert
&& charset == CharSet::Ascii
&& self.unicode_version.cell_widths.is_none()
- && text.bytes().all(|b| (b' '..=b'~').contains(&b));
+ && (ascii || wezterm_escape_parser::printable_ascii_len(text.as_bytes()) == text.len());
#[cfg(test)]
let ascii_batch = ascii_batch && !self.disable_ascii_batch;
if ascii_batch {
@@ -176,11 +185,44 @@ impl<'a> Performer<'a> {
&& self.unicode_version.cell_widths.is_none();
#[cfg(test)]
let runs = runs && !self.disable_grapheme_runs;
+ // Writing a few characters is no quicker by way of `print_plain`.
+ let plain = if runs && text.len() >= PLAIN_TEXT_MIN_LEN {
+ self.unicode_version.plain_widths()
+ } else {
+ None
+ };
let mut run = GraphemeRun::default();
// A row found unable to take a run, until the cursor next wraps.
let mut no_run_row = None;
let mut offset = 0;
- for g in Graphemes::new(text) {
+ let mut graphemes = Graphemes::new(text);
+ // Graphemes to go before looking for plain text again, and how
+ // many to give it next time: text with marks on every letter
+ // should not pay for looking at each one. An emoji on its own
+ // says nothing about what follows it.
+ let mut not_plain = 0;
+ let mut back_off = 1;
+ loop {
+ if not_plain > 0 {
+ not_plain -= 1;
+ } else if let Some(plain) = plain {
+ let printed = self.print_plain(text, offset, plain, &mut run, &mut no_run_row);
+ if printed == offset {
+ not_plain = back_off;
+ back_off = (back_off * 2).min(64);
+ } else {
+ back_off = 1;
+ offset = printed;
+ graphemes = Graphemes::new(&text[offset..]);
+ }
+ }
+ let Some(g) = graphemes.next() else {
+ break;
+ };
+ if not_plain > 0 && g.len() == 4 && g.as_bytes()[0] >= 0xf0 {
+ not_plain = 0;
+ back_off = 1;
+ }
let start = offset;
offset += g.len();
let g = self.remap_grapheme(g);
@@ -216,6 +258,7 @@ impl<'a> Performer<'a> {
// Wrapping marks the row and may scroll it away.
self.flush_grapheme_run(&mut run, text, seqno);
no_run_row = None;
+ run.written_to = None;
}
self.wrap_if_needed();
@@ -256,8 +299,18 @@ impl<'a> Performer<'a> {
run.push(x, y, start, offset, print_width);
} else {
let pen = self.pen.clone();
- self.screen_mut()
- .set_cell_grapheme(x, y, g, print_width, pen, seqno);
+ let follows = run.written_to == Some(start);
+ run.written_to = Some(offset);
+ let screen = self.screen_mut();
+ let row = screen.phys_row(y);
+ screen.line_mut(row).set_cell_grapheme_after(
+ x,
+ g,
+ print_width,
+ pen,
+ follows,
+ seqno,
+ );
}
if !wrappable {
@@ -269,8 +322,136 @@ impl<'a> Performer<'a> {
}
self.flush_grapheme_run(&mut run, text, seqno);
}
- std::mem::swap(&mut self.print, &mut p);
- self.print.clear();
+ }
+
+ /// Write the plain text at `offset` of `text` (see `PlainWidths`) that
+ /// goes in one row at a time, for as long as it lasts, and return where
+ /// it stopped. A character with something else after it is left for the
+ /// general path to take with what follows it, and so is the one that
+ /// reaches the margin, which has more to do.
+ fn print_plain(
+ &mut self,
+ text: &str,
+ offset: usize,
+ plain: &PlainWidths,
+ run: &mut GraphemeRun,
+ no_run_row: &mut Option<VisibleRowIndex>,
+ ) -> usize {
+ let seqno = self.seqno;
+ let bytes = text.as_bytes();
+ let mut offset = offset;
+ loop {
+ // The first grapheme, and whether it can go at all.
+ let Some(mut this) = plain.first(&bytes[offset..]) else {
+ return offset;
+ };
+ let Some(mut step) = plain_grapheme(plain, &bytes[offset..], this.0) else {
+ return offset;
+ };
+ // A run waiting is cheaper to go on with than to cut short for a
+ // little plain text.
+ if run.count > 0 && !plain_lasts(plain, &bytes[offset..]) {
+ return offset;
+ }
+ // What is waiting has to be in the row to tell where this goes.
+ self.flush_grapheme_run(run, text, seqno);
+ if self.wrap_next {
+ *no_run_row = None;
+ run.written_to = None;
+ }
+ self.wrap_if_needed();
+
+ let x = self.cursor.x;
+ let y = self.cursor.y;
+ let margin = self.left_and_right_margins.end;
+ // A row that can't take it all at once takes it a cell at a time.
+ let at_once = self.can_start_grapheme_run(x, y);
+
+ let start = offset;
+ let mut cells = 0;
+ let mut wide = [0; 128];
+ let mut num_wide = 0;
+ let mut max_len = 0;
+ let mut last_width = 0;
+ loop {
+ let (len, next) = step;
+ let width = this.1;
+ if x + cells + width >= margin {
+ break;
+ }
+ if !at_once {
+ let pen = self.pen.clone();
+ let g = &text[offset..offset + len];
+ let follows = run.written_to == Some(offset);
+ run.written_to = Some(offset + len);
+ let screen = self.screen_mut();
+ let row = screen.phys_row(y);
+ screen.line_mut(row).set_cell_grapheme_after(
+ x + cells,
+ g,
+ width,
+ pen,
+ follows,
+ seqno,
+ );
+ } else if width > 1 {
+ if num_wide == wide.len() {
+ break;
+ }
+ wide[num_wide] = cells as u16;
+ num_wide += 1;
+ }
+ let first = cells == 0;
+ cells += width;
+ offset += len;
+ max_len = max_len.max(len);
+ last_width = width;
+ let Some(next) = next else {
+ break;
+ };
+ this = next;
+ let Some(after) = plain_grapheme(plain, &bytes[offset..], this.0) else {
+ break;
+ };
+ step = after;
+ // Writing past the end of a row fills in the gap, after which
+ // the rest can go at once.
+ if first && !at_once && self.can_start_grapheme_run(x + cells, y) {
+ break;
+ }
+ }
+ if offset == start {
+ return offset;
+ }
+ if at_once {
+ let pen = self.pen.clone();
+ let follows = run.written_to == Some(start);
+ let appended = {
+ let screen = self.screen_mut();
+ let idx = screen.phys_row(y);
+ screen.line_mut(idx).append_grapheme_run(
+ x,
+ &text[start..offset],
+ max_len,
+ cells,
+ &wide[..num_wide],
+ last_width as u8,
+ &pen,
+ follows,
+ seqno,
+ )
+ };
+ if !appended {
+ self.write_graphemes(x, y, &text[start..offset], &pen, follows, seqno);
+ }
+ run.written_to = Some(offset);
+ }
+ self.cursor.x += cells;
+ self.wrap_next = false;
+ if offset == bytes.len() {
+ return offset;
+ }
+ }
}
fn can_start_grapheme_run(&mut self, x: usize, y: VisibleRowIndex) -> bool {
@@ -292,6 +473,8 @@ impl<'a> Performer<'a> {
}
let run_text = &text[run.start..run.end];
let pen = self.pen.clone();
+ let follows = run.written_to == Some(run.start);
+ run.written_to = Some(run.end);
let appended = {
let screen = self.screen_mut();
let idx = screen.phys_row(run.y);
@@ -303,21 +486,38 @@ impl<'a> Performer<'a> {
&run.wide[..run.num_wide],
run.last_width,
&pen,
+ follows,
seqno,
)
};
if !appended {
- // Refused for an overflowing cluster width, or a grapheme too
- // long to tell how the row's text grows; write each grapheme
- // instead. Zero-width ones in a run are white space, shown as
- // one cell.
- let mut x = run.x;
- for g in Graphemes::new(run_text) {
- let width = grapheme_column_width(g, Some(&self.unicode_version)).max(1);
- self.screen_mut()
- .set_cell_grapheme(x, run.y, g, width, pen.clone(), seqno);
- x += width;
- }
+ self.write_graphemes(run.x, run.y, run_text, &pen, follows, seqno);
+ }
+ }
+
+ /// Write the graphemes of a run the row would not take in one go, for an
+ /// overflowing cluster width or a grapheme too long to tell how the
+ /// row's text grows, or that would run into what is before it. Zero-width
+ /// ones in a run are white space, shown as one cell. `follows` says that
+ /// the text is known not to.
+ fn write_graphemes(
+ &mut self,
+ mut x: usize,
+ y: VisibleRowIndex,
+ text: &str,
+ pen: &CellAttributes,
+ mut follows: bool,
+ seqno: wezterm_surface::SequenceNo,
+ ) {
+ for g in Graphemes::new(text) {
+ let width = grapheme_column_width(g, Some(&self.unicode_version)).max(1);
+ let screen = self.screen_mut();
+ let row = screen.phys_row(y);
+ screen
+ .line_mut(row)
+ .set_cell_grapheme_after(x, g, width, pen.clone(), follows, seqno);
+ x += width;
+ follows = true;
}
}
@@ -329,6 +529,7 @@ impl<'a> Performer<'a> {
// so that we can correctly reflow it if the window is
// resized.
{
+ let x = self.cursor.x;
let y = self.cursor.y;
let is_conpty = self.state.enable_conpty_quirks;
let screen = self.screen_mut();
@@ -350,7 +551,9 @@ impl<'a> Performer<'a> {
.map(|cell| makes_sense_to_wrap(cell.str()))
.unwrap_or(false);
if should_mark_wrapped {
- screen.line_mut(y).set_last_cell_was_wrapped(true, seqno);
+ let line = screen.line_mut(y);
+ line.keep_blanks_to(x + 1);
+ line.set_last_cell_was_wrapped(true, seqno);
}
}
self.new_line(true);
@@ -369,10 +572,9 @@ impl<'a> Performer<'a> {
let pen = self.pen.clone();
let screen = self.screen_mut();
let row = screen.phys_row(y);
- let written = screen
+ screen
.line_mut(row)
- .set_ascii_cells(x, &text[..count], &pen, seqno);
- debug_assert!(written);
+ .set_printable_ascii_cells(x, &text[..count], &pen, seqno);
if x + count < width {
self.cursor.x += count;
self.wrap_next = false;
@@ -408,6 +610,40 @@ impl<'a> Performer<'a> {
}
}
+ /// `print_text` for a parser's run of text, with the tabs and line ends
+ /// it may have left in it acted on in place. Text ahead of one of those
+ /// goes straight to the screen: nothing can follow it to join it, so
+ /// there is no need to gather it up first. What is left at the end can
+ /// still be joined by what comes next.
+ pub(crate) fn print_run(&mut self, text: &str) {
+ let bytes = text.as_bytes();
+ let mut start = 0;
+ while start < bytes.len() {
+ let mut end = start + wezterm_escape_parser::printable_ascii_len(&bytes[start..]);
+ let ascii = !matches!(bytes.get(end), Some(0x7f..));
+ if !ascii {
+ end += wezterm_escape_parser::c0_position(&bytes[end..]);
+ }
+ let control = match bytes.get(end) {
+ None => return self.print_text(&text[start..]),
+ Some(b'\t') => ControlCode::HorizontalTab,
+ Some(b'\n') => ControlCode::LineFeed,
+ Some(b'\r') => ControlCode::CarriageReturn,
+ // Anything else is the business of the general path.
+ Some(_) => return self.print_text(&text[start..]),
+ };
+ if self.print.is_empty() && self.accumulating_title.is_none() {
+ if end > start {
+ self.print_now(&text[start..end], ascii);
+ }
+ } else {
+ self.print_text(&text[start..end]);
+ }
+ self.control(control);
+ start = end + 1;
+ }
+ }
+
pub fn perform(&mut self, action: Action) {
debug!("perform {:?}", action);
if self.suppress_initial_title_change {
@@ -425,7 +661,7 @@ impl<'a> Performer<'a> {
}
match action {
Action::Print(c) => self.print(c),
- Action::PrintString(s) => self.print_text(&s),
+ Action::PrintString(s) => self.print_run(&s),
Action::Control(code) => self.control(code),
Action::DeviceControl(ctrl) => self.device_control(ctrl),
Action::OperatingSystemCommand(osc) => self.osc_dispatch(*osc),
@@ -1346,6 +1582,57 @@ fn selection_to_selection(sel: Selection) -> ClipboardSelection {
}
}
+/// How far the grapheme goes that starts with the plain character of `len`
+/// bytes at the start of `bytes`, and the plain character after it if there
+/// is one; None if it takes the general path to tell. The diacritics a letter
+/// can have on it are the one thing allowed to follow it, these being how
+/// macOS spells accented file names.
+#[inline(always)]
+fn plain_grapheme(
+ plain: &PlainWidths,
+ bytes: &[u8],
+ len: usize,
+) -> Option<(usize, Option<(usize, usize)>)> {
+ let rest = &bytes[len..];
+ if rest.is_empty() {
+ return Some((len, None));
+ }
+ if let Some(next) = plain.first(rest) {
+ return Some((len, Some(next)));
+ }
+ let marks = if len == 1 {
+ wezterm_cell::diacritics_len(rest)
+ } else {
+ 0
+ };
+ if marks == 0 {
+ return None;
+ }
+ let rest = &rest[marks..];
+ if rest.is_empty() {
+ return Some((len + marks, None));
+ }
+ plain.first(rest).map(|next| (len + marks, Some(next)))
+}
+
+/// How long text has to be for `print_plain` to have a go at it.
+const PLAIN_TEXT_MIN_LEN: usize = 12;
+
+/// Whether `bytes` starts with enough plain graphemes to be worth the end of
+/// a run of graphemes.
+fn plain_lasts(plain: &PlainWidths, mut bytes: &[u8]) -> bool {
+ for _ in 0..4 {
+ let Some((len, _)) = plain.first(bytes) else {
+ return false;
+ };
+ let Some((len, _)) = plain_grapheme(plain, bytes, len) else {
+ return false;
+ };
+ bytes = &bytes[len..];
+ }
+ true
+}
+
/// Graphemes waiting to be appended to one row in a single write.
struct GraphemeRun {
x: usize,
@@ -1361,6 +1648,9 @@ struct GraphemeRun {
wide: [u16; GraphemeRun::MAX],
num_wide: usize,
last_width: u8,
+ /// Where in the text the row was last written up to. Text that goes on
+ /// from there has already been cut at a grapheme boundary.
+ written_to: Option<usize>,
}
impl Default for GraphemeRun {
@@ -1376,6 +1666,7 @@ impl Default for GraphemeRun {
wide: [0; GraphemeRun::MAX],
num_wide: 0,
last_width: 0,
+ written_to: None,
}
}
}
@@ -1496,17 +1787,25 @@ mod ascii_tests {
let mut expected = make();
let mut actual = make();
let mut direct = make();
+ let mut kept = make();
let mut parser = Parser::new();
let mut direct_parser = Parser::new();
+ let mut kept_parser = Parser::new();
+ kept_parser.set_text_keeps_line_controls(true);
let input = format!("{setup}abc #q~0123456789az next\rOVERWRITE\nmore\te\u{301}1\u{fe0f}\u{20e3}界\x1b[0m\x1bkASCII title界\x1b\\end");
for chunk in input.as_bytes().chunks(chunk_size) {
let mut actions = Vec::new();
parser.parse_with_borrowed_text(chunk, |action| {
action.append_to(&mut actions)
});
+ let mut kept_actions = Vec::new();
+ kept_parser.parse_with_borrowed_text(chunk, |action| {
+ action.append_to(&mut kept_actions)
+ });
actual.increment_seqno();
expected.increment_seqno();
direct.increment_seqno();
+ kept.increment_seqno();
{
let mut direct_performer = Performer::new(&mut direct);
direct_parser.parse_with_borrowed_text(
@@ -1523,6 +1822,10 @@ mod ascii_tests {
let mut scalar = Performer::new(&mut expected);
scalar.disable_ascii_batch = true;
let mut batched = Performer::new(&mut actual);
+ let mut line_controls_kept = Performer::new(&mut kept);
+ for action in kept_actions {
+ line_controls_kept.perform(action);
+ }
for action in actions {
batched.perform(action.clone());
match action {
@@ -1535,10 +1838,13 @@ mod ascii_tests {
}
}
}
- assert_eq!(actual.snapshot(), expected.snapshot(),
+ let expected_rows = expected.snapshot().with_rows_as_cells();
+ assert_eq!(actual.snapshot().with_rows_as_cells(), expected_rows,
"rows={rows}, cols={cols}, setup={setup:?}, normalize={normalize}, custom_widths={custom_widths}, conpty={conpty}, chunk_size={chunk_size}");
- assert_eq!(direct.snapshot(), expected.snapshot(),
+ assert_eq!(direct.snapshot().with_rows_as_cells(), expected_rows,
"direct: rows={rows}, cols={cols}, setup={setup:?}, normalize={normalize}, custom_widths={custom_widths}, conpty={conpty}, chunk_size={chunk_size}");
+ assert_eq!(kept.snapshot().with_rows_as_cells(), expected_rows,
+ "kept: rows={rows}, cols={cols}, setup={setup:?}, normalize={normalize}, custom_widths={custom_widths}, conpty={conpty}, chunk_size={chunk_size}");
}
}
}
@@ -1546,6 +1852,225 @@ mod ascii_tests {
}
}
+ /// Write `input` with and without grapheme runs, and with the parser as
+ /// the mux has it, and compare what each makes of it, whatever each is
+ /// stored as.
+ fn check_runs(rows: usize, cols: usize, input: &str, chunk_size: usize) {
+ let make = || {
+ TerminalState::new(
+ TerminalSize {
+ rows,
+ cols,
+ pixel_width: cols * 8,
+ pixel_height: rows * 16,
+ dpi: 96,
+ },
+ Arc::new(TestConfig {
+ normalize: false,
+ custom_widths: false,
+ }),
+ "test",
+ "test",
+ Box::new(Vec::new()),
+ )
+ };
+ let mut runs = make();
+ let mut single = make();
+ let mut parser = Parser::new();
+ let mut kept_parser = Parser::new();
+ kept_parser.set_text_keeps_line_controls(true);
+ for chunk in input.as_bytes().chunks(chunk_size) {
+ let mut actions = Vec::new();
+ parser.parse_with_borrowed_text(chunk, |action| action.append_to(&mut actions));
+ let mut kept_actions = Vec::new();
+ kept_parser
+ .parse_with_borrowed_text(chunk, |action| action.append_to(&mut kept_actions));
+ runs.increment_seqno();
+ single.increment_seqno();
+ let mut batched = Performer::new(&mut runs);
+ for action in kept_actions {
+ batched.perform(action);
+ }
+ drop(batched);
+ let mut scalar = Performer::new(&mut single);
+ scalar.disable_grapheme_runs = true;
+ for action in actions {
+ scalar.perform(action);
+ }
+ drop(scalar);
+ // A row of cells keeps the wrap flag of a wide cell in its second
+ // half, where the cell doesn't show it.
+ let cells = |line: &wezterm_surface::Line| {
+ line.visible_cells()
+ .map(|cell| {
+ let mut attrs = cell.attrs().clone();
+ if cell.width() > 1 {
+ attrs.set_wrapped(false);
+ }
+ (
+ cell.cell_index(),
+ cell.str().to_string(),
+ cell.width(),
+ attrs,
+ )
+ })
+ .collect::<Vec<_>>()
+ };
+ let actual = runs.snapshot();
+ let expected = single.snapshot();
+ assert_eq!(actual.screen.lines.len(), expected.screen.lines.len());
+ for (row, (a, e)) in actual
+ .screen
+ .lines
+ .iter()
+ .zip(&expected.screen.lines)
+ .enumerate()
+ {
+ assert_eq!(
+ (a.len(), a.current_seqno(), cells(a)),
+ (e.len(), e.current_seqno(), cells(e)),
+ "row {row}, rows={rows}, cols={cols}, input={input:?}, chunk_size={chunk_size}"
+ );
+ }
+ assert_eq!(runs.cursor_pos(), single.cursor_pos(), "{input:?}");
+ }
+ }
+
+ #[test]
+ fn grapheme_runs_over_erased_rows_match_one_grapheme_at_a_time() {
+ let texts = [
+ "\u{4e2d}\u{6587}\u{5b57}\u{7b26}\u{4e2d}\u{6587}",
+ "abc\u{4e2d}def",
+ "\u{1f600}\u{4e2d}\u{6587}\u{1f600} hello",
+ "\u{2764}\u{fe0f}\u{4e2d}",
+ "e\u{301}xe\u{301}",
+ "\u{e01}\u{e33}x",
+ "\u{1f1fa}\u{1f1f8}",
+ ];
+ for (rows, cols) in [(3, 7), (4, 12), (4, 80)] {
+ for setup in [
+ "",
+ "abcdef\x1b[2K",
+ "abcdef\x1b[2J\x1b[H",
+ "\x1b[2K\x1b[3G",
+ "ab\x1b[K\x1b[5G",
+ "\u{4e2d}\u{6587}\x1b[2K",
+ "abcdef\r\x1b[2K\x1b[32m",
+ "\x1b[44m\x1b[2K\x1b[0m",
+ "ab\x1b[1b\r\x1b[3C",
+ ] {
+ for text in texts {
+ for chunk_size in [1, 5, 4096] {
+ let input =
+ format!("{setup}{text}\x1b[31m{text}\x1b[0m\x1b[K{text}\r\n{text}");
+ check_runs(rows, cols, &input, chunk_size);
+ }
+ }
+ }
+ }
+ }
+
+ /// `print_plain` starts as soon as what is waiting has been written.
+ #[test]
+ fn plain_text_follows_what_is_not() {
+ for text in [
+ "\u{1f600}\u{4e2d}\u{6587}\u{5b57}\u{7b26}\r\n",
+ "\u{2764}\u{fe0f}\u{4e2d}\u{6587}\u{5b57}\u{7b26}\r\n",
+ "\u{1f600} hello world\r\n",
+ "\u{4e2d}\u{1f600}\u{4e2d}\u{1f600}\u{4e2d}\r\n",
+ ] {
+ for chunk_size in [1, 7, 4096] {
+ check_runs(4, 20, &text.repeat(9), chunk_size);
+ }
+ }
+ }
+
+ /// A row a run can't go on keeps it for the rest of the row, plain or not.
+ #[test]
+ fn plain_text_over_text_matches_one_grapheme_at_a_time() {
+ for chunk_size in [1, 4096] {
+ check_runs(
+ 3,
+ 20,
+ "\u{4e2d}\u{6587}abc\x1b[H\u{5b57}\u{7b26}\x1b[H\u{6587}x",
+ chunk_size,
+ );
+ }
+ }
+
+ #[test]
+ fn a_line_control_after_a_c1_control_is_a_control() {
+ for control in ['\n', '\r', '\t'] {
+ for tail in ["cd", "\x1b[32mcd"] {
+ let input = format!("ab\u{94}{control}{tail}");
+ check_runs(4, 20, &input, 4);
+ check_runs(4, 20, &input, 4096);
+ }
+ }
+ }
+
+ /// What a row wraps from has blanks in it that were written, whatever
+ /// the row is stored as.
+ #[test]
+ fn blanks_before_a_wrap_survive_a_resize() {
+ for input in [
+ "abc\x1b[10G next",
+ "qqqqq\r\x1b[Kabc\x1b[K\x1b[10G next",
+ "xxxxxxxxxx\x1b[2K\x1b[Khello",
+ ] {
+ let mut term = TerminalState::new(
+ TerminalSize {
+ rows: 4,
+ cols: 10,
+ pixel_width: 80,
+ pixel_height: 64,
+ dpi: 96,
+ },
+ Arc::new(TestConfig {
+ normalize: false,
+ custom_widths: false,
+ }),
+ "test",
+ "test",
+ Box::new(Vec::new()),
+ );
+ let mut parser = Parser::new();
+ let mut actions = Vec::new();
+ parser.parse_with_borrowed_text(input.as_bytes(), |action| {
+ action.append_to(&mut actions)
+ });
+ term.increment_seqno();
+ let mut performer = Performer::new(&mut term);
+ for action in actions {
+ performer.perform(action);
+ }
+ drop(performer);
+ let (_, lines) = term.screen().lines_in_phys_range(0..2).into_iter().fold(
+ (0, Vec::new()),
+ |(n, mut lines), line| {
+ lines.push(line);
+ (n + 1, lines)
+ },
+ );
+ assert_eq!(lines[0].len(), 10, "{input:?}");
+ assert!(lines[0].last_cell_was_wrapped(), "{:?}", input);
+ }
+ }
+
+ #[test]
+ fn deleting_on_a_row_of_cells_written_apart_does_not_panic() {
+ for cols in [10, 11] {
+ for input in [
+ "abbbb\u{e01}\x1b[1m\u{e33}xy\x1b[P",
+ "ab\x1b[3b\u{e01}\x1b[1m\u{e33}xy\x1b[P",
+ "ab\x1b[3b\u{1f1fa}\x1b[1m\u{1f1f8}\x1b[P",
+ ] {
+ check_runs(3, cols, input, 4096);
+ check_runs(3, cols, input, 1);
+ }
+ }
+ }
+
#[test]
fn grapheme_runs_match_one_grapheme_at_a_time() {
let long = format!("{}e\u{301}{}", "界".repeat(45), "字".repeat(40));
diff --git a/term/src/terminalstate/repeat_tests.rs b/term/src/terminalstate/repeat_tests.rs
index f77607b8..6a77009e 100644
--- a/term/src/terminalstate/repeat_tests.rs
+++ b/term/src/terminalstate/repeat_tests.rs
@@ -133,8 +133,8 @@ fn batched_repeat_matches_scalar_screen_cursor_and_scrollback() {
let mut actual = make();
actual.perform_csi_edit(Edit::Repeat(n));
assert_eq!(
- actual.snapshot(),
- expected.snapshot(),
+ actual.snapshot().with_rows_as_cells(),
+ expected.snapshot().with_rows_as_cells(),
"rows={rows}, cols={cols}, scrollback={scrollback}, setup={setup:?}, n={n}"
);
}
diff --git a/term/src/terminalstate/snapshot.rs b/term/src/terminalstate/snapshot.rs
index 7e9a5bcc..299c9b9e 100644
--- a/term/src/terminalstate/snapshot.rs
+++ b/term/src/terminalstate/snapshot.rs
@@ -72,6 +72,21 @@ pub struct ScreenSnapshot {
pub saved_cursor: Option<SavedCursor>,
}
+#[cfg(test)]
+impl TerminalSnapshot {
+ /// This snapshot with every row in cells, for comparing what two
+ /// terminals hold however each came to store it: a row of ASCII written
+ /// over in place stays compact, and written a cell at a time does not.
+ pub(crate) fn with_rows_as_cells(mut self) -> Self {
+ for screen in [&mut self.screen, &mut self.alt_screen] {
+ for line in &mut screen.lines {
+ line.cells_mut();
+ }
+ }
+ self
+ }
+}
+
/// A picture's place in a cell, with the picture itself replaced by its
/// hash. Keeps the kitty image and placement ids, which the wire format's
/// image cell drops: a delete-by-id after the restore has to find them.
diff --git a/wezterm-surface/src/line/clusterline.rs b/wezterm-surface/src/line/clusterline.rs
index 42683e02..e6ad9c09 100644
--- a/wezterm-surface/src/line/clusterline.rs
+++ b/wezterm-surface/src/line/clusterline.rs
@@ -9,6 +9,7 @@ use wezterm_cell::{Cell, CellAttributes};
extern crate alloc;
use alloc::boxed::Box;
+#[cfg(any(feature = "use_serde", test))]
use alloc::string::String;
use alloc::vec;
use alloc::vec::Vec;
@@ -24,9 +25,14 @@ struct Cluster {
/// clusters of attribute data describing attributed ranges
/// within the line
#[cfg_attr(feature = "use_serde", derive(Serialize))]
-#[derive(Debug, Clone, PartialEq)]
+#[derive(Clone, PartialEq)]
pub(crate) struct ClusteredLine {
- pub text: String,
+ /// Always valid UTF-8: only text goes in, and only ASCII over ASCII. It
+ /// is held as bytes for that second part, which a program moving around
+ /// the screen does for every character, and checked by `text()` when it
+ /// is read, which is rarer and goes through the whole line anyway.
+ #[cfg_attr(feature = "use_serde", serde(serialize_with = "serialize_text"))]
+ pub text: Vec<u8>,
#[cfg_attr(
feature = "use_serde",
serde(
@@ -40,6 +46,107 @@ pub(crate) struct ClusteredLine {
last_cell_width: Option<NonZeroU8>,
}
+/// Whether `after`, written right behind `before`, would become part of its
+/// last grapheme. A clustered line has nothing but its text to say where the
+/// cells are, so two cells that read back as one grapheme are one cell: text
+/// that comes apart like this has to be kept as cells.
+#[inline]
+pub(crate) fn runs_into(before: &[u8], after: &str) -> bool {
+ match (before.last(), after.as_bytes().first()) {
+ (Some(last), Some(first)) => {
+ !(ends_simply(before, *last) && starts_simply(*first))
+ && !plain_meets_plain(before, after)
+ && chars_join(before, after)
+ }
+ _ => false,
+ }
+}
+
+/// Whether a character with this first byte can only be ASCII, a Han
+/// ideograph (U+4000..=U+9FFF) or a private use one (U+E000..=U+EFFF, which
+/// is where icons in prompts are), which is what nearly all text comes down
+/// to, and which join nothing on either side of them unless the other asks.
+#[inline]
+fn starts_simply(first: u8) -> bool {
+ first.is_ascii() || matches!(first, 0xe4..=0xe9 | 0xee)
+}
+
+/// `starts_simply` of the character at the end of `before`.
+#[inline]
+fn ends_simply(before: &[u8], last: u8) -> bool {
+ last.is_ascii() || matches!(*before, [.., 0xe4..=0xe9 | 0xee, 0x80..=0xbf, 0x80..=0xbf])
+}
+
+/// Characters that are the same to segmenting whichever version of Unicode
+/// their widths come from.
+#[cfg(feature = "std")]
+fn plain_chars() -> Option<&'static wezterm_cell::PlainWidths> {
+ wezterm_cell::UnicodeVersion::new(9).plain_widths()
+}
+
+/// Whether the characters that meet are ones that never join.
+#[cfg(feature = "std")]
+fn plain_meets_plain(before: &[u8], after: &str) -> bool {
+ let earliest = before.len().saturating_sub(3);
+ let start = (earliest..before.len())
+ .rfind(|&index| before[index] & 0xc0 != 0x80)
+ .unwrap_or(earliest);
+ plain_chars().is_some_and(|plain| {
+ plain.first(&before[start..]).is_some() && plain.first(after.as_bytes()).is_some()
+ })
+}
+
+#[cfg(not(feature = "std"))]
+fn plain_meets_plain(_before: &[u8], _after: &str) -> bool {
+ false
+}
+
+#[inline(never)]
+fn chars_join(before: &[u8], after: &str) -> bool {
+ // What joins can depend on the characters before the last one, though
+ // not on many of them.
+ const CONTEXT: usize = 16;
+ let Some(first) = after.chars().next() else {
+ return false;
+ };
+ // Except that regional indicators pair off from the first of them.
+ if ('\u{1f1e6}'..='\u{1f1ff}').contains(&first) {
+ let (_, indicators) = before.as_rchunks::<4>();
+ return indicators
+ .iter()
+ .rev()
+ .take_while(|c| matches!(c, [0xf0, 0x9f, 0x87, 0xa6..=0xbf]))
+ .count()
+ % 2
+ == 1;
+ }
+ let earliest = before.len().saturating_sub(CONTEXT);
+ let start = (earliest..before.len())
+ .find(|&index| before[index] & 0xc0 != 0x80)
+ .unwrap_or(before.len());
+ let context = &before[start..];
+ let mut joined = [0; CONTEXT + 4];
+ joined[..context.len()].copy_from_slice(context);
+ let end = context.len() + first.encode_utf8(&mut joined[context.len()..]).len();
+ core::str::from_utf8(&joined[..end]).is_ok_and(|text| {
+ Graphemes::new(text)
+ .last()
+ .is_some_and(|g| g.len() > first.len_utf8())
+ })
+}
+
+impl core::fmt::Debug for ClusteredLine {
+ fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+ f.debug_struct("ClusteredLine")
+ .field("text", &self.text())
+ .field("is_double_wide", &self.is_double_wide)
+ .field("clusters", &self.clusters)
+ .field("len", &self.len)
+ .field("last_cell_width", &self.last_cell_width)
+ .finish()
+ }
+}
+
#[cfg(feature = "use_serde")]
impl<'de> Deserialize<'de> for ClusteredLine {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
@@ -68,12 +175,23 @@ impl<'de> Deserialize<'de> for ClusteredLine {
None
};
Ok(Self {
- text: wire.text, is_double_wide, clusters: wire.clusters,
+ text: wire.text.into_bytes(), is_double_wide, clusters: wire.clusters,
len: wire.len, last_cell_width: wire.last_cell_width,
})
}
}
+/// As the string it is on the wire.
+#[cfg(feature = "use_serde")]
+fn serialize_text<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
+where
+ S: Serializer,
+{
+ core::str::from_utf8(value)
+ .map_err(serde::ser::Error::custom)?
+ .serialize(serializer)
+}
+
/// Serialize the bitset as a vector of the indices of just the 1 bits;
/// the thesis is that most of the cells on a given line are single width.
/// That may not be strictly true for users that heavily use asian scripts,
@@ -93,6 +211,10 @@ where
}
impl ClusteredLine {
+ /// How many clusters a line can have for one in the middle to be worth
+ /// changing in place.
+ const MAX_CLUSTERS: usize = 16;
+
/// Reuse only small buffers when a discarded screen row becomes blank.
/// Drop all attributes and wide-cell metadata, including image/link refs.
pub(crate) fn clear_for_scrolling(&mut self, max_text_capacity: usize) -> bool {
@@ -112,7 +234,7 @@ impl ClusteredLine {
pub fn new() -> Self {
Self {
- text: String::with_capacity(Self::NEW_TEXT_CAPACITY),
+ text: Vec::with_capacity(Self::NEW_TEXT_CAPACITY),
is_double_wide: None,
clusters: vec![],
len: 0,
@@ -144,6 +266,12 @@ impl ClusteredLine {
}
}
+ // Cells can only read back short of `len()` from a line that was not
+ // written here; there is no use in believing all of what that claims.
+ let len = self.len().min(cells.len() + 256);
+ if cells.len() < len {
+ cells.resize_with(len, Cell::blank);
+ }
cells.shrink_to_fit();
cells
}
@@ -155,7 +283,7 @@ impl ClusteredLine {
// of growing one realloc at a time and allocating a bitset that the
// typical all-narrow line immediately throws away.
let mut is_double_wide: Option<Box<FixedBitSet>> = None;
- let mut text = String::with_capacity(hint);
+ let mut text = Vec::with_capacity(hint);
let mut clusters = vec![];
let mut len = 0;
let mut last_cell_width = None;
@@ -170,7 +298,7 @@ impl ClusteredLine {
.set(cell.cell_index(), true);
}
- text.push_str(cell.str());
+ text.extend_from_slice(cell.str().as_bytes());
last_cluster = match last_cluster.take() {
None => Some(Cluster {
@@ -213,6 +341,46 @@ impl ClusteredLine {
self.len as usize
}
+ /// The line's text. It can only be anything but valid from a line off
+ /// the wire that lied about its cells, which is not worth a crash.
+ pub(crate) fn text(&self) -> &str {
+ core::str::from_utf8(&self.text).unwrap_or_default()
+ }
+
+ /// Whether `text` at the end of the line would run into what is there.
+ #[inline]
+ pub(crate) fn joins(&self, text: &str) -> bool {
+ runs_into(&self.text, text)
+ }
+
+ /// Whether the line's graphemes are `cells`, as they have to be for each
+ /// to be read back as the cell it was.
+ pub(crate) fn reads_back_as<'a>(&self, mut cells: impl Iterator<Item = CellRef<'a>>) -> bool {
+ self.text.is_ascii()
+ || self.is_plain()
+ || Graphemes::new(self.text())
+ .all(|g| matches!(cells.next(), Some(cell) if cell.str() == g))
+ && cells.next().is_none()
+ }
+
+ /// Whether the text is all characters that never join each other.
+ #[cfg(feature = "std")]
+ fn is_plain(&self) -> bool {
+ let Some(plain) = plain_chars() else {
+ return false;
+ };
+ let mut rest = &self.text[..];
+ while let Some((len, _)) = plain.first(rest) {
+ rest = &rest[len..];
+ }
+ rest.is_empty()
+ }
+
+ #[cfg(not(feature = "std"))]
+ fn is_plain(&self) -> bool {
+ false
+ }
+
fn is_double_wide(&self, cell_index: usize) -> bool {
match &self.is_double_wide {
Some(bitset) => bitset.contains(cell_index),
@@ -224,7 +392,7 @@ impl ClusteredLine {
let mut clusters = self.clusters.iter();
let cluster = clusters.next();
ClusterLineCellIter {
- graphemes: Graphemes::new(&self.text),
+ graphemes: Graphemes::new(self.text()),
clusters,
cluster,
idx: 0,
@@ -255,11 +423,197 @@ impl ClusteredLine {
});
remaining -= count;
}
- self.text.push_str(text);
+ self.text.extend_from_slice(text.as_bytes());
self.len += text.len() as u32;
self.last_cell_width = NonZeroU8::new(1);
}
+ /// Whether each cell of the line is one byte of its text, which is what
+ /// lets a cell be found, and replaced, without going through the rest.
+ fn cells_are_bytes(&self) -> bool {
+ self.is_double_wide.is_none() && self.text.len() == self.len as usize
+ }
+
+ /// Whether the line is nothing but default blanks.
+ pub(crate) fn is_all_blank(&self) -> bool {
+ self.cells_are_bytes()
+ && self
+ .clusters
+ .iter()
+ .all(|c| c.attrs == CellAttributes::blank())
+ && self.text.iter().all(|&b| b == b' ')
+ }
+
+ /// Whether nothing but default blanks are left from cell `idx` on, which
+ /// a run can take the place of without looking at the cells. Not for a
+ /// line with a lot of clusters already: going through them all for each
+ /// new one is what cells are for.
+ pub(crate) fn is_blank_from(&self, idx: usize) -> bool {
+ let Some(count) = (self.len as usize).checked_sub(idx) else {
+ return false;
+ };
+ let Some(end) = self.text.len().checked_sub(count) else {
+ return false;
+ };
+ let (words, rest) = self.text[end..].as_chunks::<8>();
+ self.clusters.len() < Self::MAX_CLUSTERS
+ && matches!(self.clusters.last(), Some(cluster)
+ if cluster.cell_width as usize >= count && cluster.attrs == CellAttributes::blank())
+ && words.iter().all(|word| *word == [b' '; 8])
+ && rest.iter().all(|&b| b == b' ')
+ && self.no_wide_cells_from(idx.saturating_sub(1))
+ && !runs_into(&self.text[..end], " ")
+ }
+
+ /// Whether there are no wide cells from cell `idx` on. The bits stop at
+ /// the last wide cell unless the line was built all at once.
+ fn no_wide_cells_from(&self, idx: usize) -> bool {
+ self.is_double_wide
+ .as_ref()
+ .is_none_or(|bits| (idx..bits.len()).all(|cell| !bits.contains(cell)))
+ }
+
+ /// The cell at `cell_index`, if `cells_are_bytes` says it can be told
+ /// without going through the graphemes before it.
+ pub(crate) fn get_byte_cell(&self, cell_index: usize) -> Option<CellRef<'_>> {
+ if cell_index >= self.len as usize || !self.cells_are_bytes() {
+ return None;
+ }
+ let mut end = 0;
+ let cluster = self.clusters.iter().find(|cluster| {
+ end += cluster.cell_width as usize;
+ cell_index < end
+ })?;
+ Some(CellRef::ClusterRef {
+ cell_index,
+ text: core::str::from_utf8(self.text.get(cell_index..cell_index + 1)?).ok()?,
+ width: 1,
+ attrs: &cluster.attrs,
+ })
+ }
+
+ /// Replace `range` of the cells with ASCII written by `write`, all with
+ /// `attrs`, keeping the line compact. False, changing nothing, unless the
+ /// range is on a line `cells_are_bytes` is true of. The clusters are the
+ /// only part of it to go through, and a row that has many of them is
+ /// going to be cheaper as cells.
+ pub(crate) fn overwrite_ascii(
+ &mut self,
+ range: core::ops::Range<usize>,
+ attrs: &CellAttributes,
+ write: impl FnOnce(&mut [u8]),
+ ) -> bool {
+ if range.end > self.len as usize || range.is_empty() || !self.cells_are_bytes() {
+ return false;
+ }
+ // A cell's image placements outlive what is written over it. Clusters
+ // holding any are left for the cells to sort out.
+ #[cfg(feature = "use_image")]
+ if attrs.has_images() || self.clusters.iter().any(|c| c.attrs.has_images()) {
+ return false;
+ }
+ // The clusters `range` touches, and how far into them it starts and
+ // ends.
+ let mut pos = 0;
+ let mut first = None;
+ let mut last = None;
+ for (index, cluster) in self.clusters.iter().enumerate() {
+ let end = pos + cluster.cell_width as usize;
+ if first.is_none() && range.start < end {
+ first = Some((index, range.start - pos));
+ }
+ if range.end <= end {
+ last = Some((index, end - range.end));
+ break;
+ }
+ pos = end;
+ }
+ let (Some((first, before)), Some((last, after))) = (first, last) else {
+ return false;
+ };
+ let same = first == last && self.clusters[first].attrs == *attrs;
+ if !same {
+ if self.clusters.len() >= Self::MAX_CLUSTERS {
+ return false;
+ }
+ let cells = range.len();
+ if cells > u16::MAX as usize {
+ return false;
+ }
+ let mut pieces: [Option<Cluster>; 3] = [None, None, None];
+ if before > 0 {
+ pieces[0] = Some(Cluster {
+ cell_width: before as u16,
+ attrs: self.clusters[first].attrs.clone(),
+ });
+ }
+ pieces[1] = Some(Cluster {
+ cell_width: cells as u16,
+ attrs: attrs.clone(),
+ });
+ if after > 0 {
+ pieces[2] = Some(Cluster {
+ cell_width: after as u16,
+ attrs: self.clusters[last].attrs.clone(),
+ });
+ }
+ self.clusters
+ .splice(first..=last, IntoIterator::into_iter(pieces).flatten());
+ }
+ write(&mut self.text[range.clone()]);
+ debug_assert!(self.text[range].is_ascii());
+ true
+ }
+
+ /// `append_ascii` of `count` copies of the printable ASCII character `c`.
+ pub fn append_ascii_repeated(&mut self, c: char, count: usize, attrs: &CellAttributes) {
+ assert!((' '..='~').contains(&c));
+ let extend = matches!(self.clusters.last(), Some(cluster) if cluster.attrs == *attrs);
+ let mut remaining = count;
+ if extend {
+ if let Some(cluster) = self.clusters.last_mut() {
+ let more = remaining.min((u16::MAX - cluster.cell_width) as usize);
+ cluster.cell_width += more as u16;
+ remaining -= more;
+ }
+ }
+ while remaining > 0 {
+ let more = remaining.min(u16::MAX as usize);
+ self.clusters.push(Cluster {
+ attrs: attrs.clone(),
+ cell_width: more as u16,
+ });
+ remaining -= more;
+ }
+ self.text.resize(self.text.len() + count, c as u8);
+ self.len += count as u32;
+ self.last_cell_width = NonZeroU8::new(1);
+ }
+
+ /// Whether `text` at cell `idx`, which is the end of the line or where
+ /// only blanks are left of it, would run into what is before it.
+ pub(crate) fn joins_at(&self, idx: usize, text: &str) -> bool {
+ let end = self.text.len() - (self.len as usize - idx);
+ runs_into(&self.text[..end], text)
+ }
+
+ /// Take away the blanks from cell `idx` on (see `is_blank_from`).
+ pub(crate) fn truncate_blanks(&mut self, idx: usize) {
+ let blanks = self.len as usize - idx;
+ if blanks == 0 {
+ return;
+ }
+ self.text.truncate(self.text.len() - blanks);
+ self.len = idx as u32;
+ self.last_cell_width = None;
+ if let Some(cluster) = self.clusters.last_mut() {
+ cluster.cell_width -= blanks as u16;
+ if cluster.cell_width == 0 {
+ self.clusters.pop();
+ }
+ }
+ }
+
pub fn append_grapheme(&mut self, text: &str, cell_width: usize, attrs: CellAttributes) {
let cell_width = cell_width as u16;
let new_cluster = match self.clusters.last() {
@@ -281,7 +635,7 @@ impl ClusteredLine {
} else if let Some(cluster) = self.clusters.last_mut() {
cluster.cell_width += cell_width;
}
- self.text.push_str(text);
+ self.text.extend_from_slice(text.as_bytes());
if cell_width > 1 {
let bitset = match self.is_double_wide.take() {
@@ -344,7 +698,7 @@ impl ClusteredLine {
}),
}
self.text.reserve_exact(capacity - self.text.len());
- self.text.push_str(text);
+ self.text.extend_from_slice(text.as_bytes());
if let (Some(&first), Some(&last)) = (wide.first(), wide.last()) {
let base = self.len as usize;
let bitset = self.is_double_wide.get_or_insert_with(|| {
@@ -395,7 +749,7 @@ impl ClusteredLine {
} else if let Some(cluster) = self.clusters.last_mut() {
cluster.cell_width += cell_width;
}
- self.text.push_str(cell.str());
+ self.text.extend_from_slice(cell.str().as_bytes());
if cell_width > 1 {
let bitset = match self.is_double_wide.take() {
@@ -417,7 +771,7 @@ impl ClusteredLine {
}
pub fn prune_trailing_blanks(&mut self) -> bool {
- let num_spaces = self.text.chars().rev().take_while(|&c| c == ' ').count();
+ let num_spaces = self.text.iter().rev().take_while(|&&b| b == b' ').count();
if num_spaces == 0 {
return false;
}
@@ -517,10 +871,10 @@ impl<'a> Iterator for ClusterLineCellIter<'a> {
}
}
-/// The capacity a String of `capacity` ends with when graphemes of at most
+/// The capacity text of `capacity` ends with when graphemes of at most
/// `max_len` bytes are pushed one at a time until it holds `len` bytes, or
-/// None where that would depend on each one's length. A String grows to
-/// twice its capacity, or to what it needs if more, and to at least 8 bytes.
+/// None where that would depend on each one's length. Text grows to twice
+/// its capacity, or to what it needs if more, and to at least 8 bytes.
fn grown_capacity(capacity: usize, len: usize, max_len: usize) -> Option<usize> {
if len <= capacity {
return Some(capacity);
diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rs
index 5216d561..7d4e444c 100644
--- a/wezterm-surface/src/line/line.rs
+++ b/wezterm-surface/src/line/line.rs
@@ -1,7 +1,7 @@
use crate::cellcluster::CellCluster;
use crate::hyperlink::Rule;
use crate::line::cellref::CellRef;
-use crate::line::clusterline::ClusteredLine;
+use crate::line::clusterline::{runs_into, ClusteredLine};
use crate::line::linebits::LineBits;
use crate::line::storage::{CellStorage, VisibleCellIter};
use crate::line::vecstorage::{VecStorage, VecStorageIter};
@@ -109,6 +109,14 @@ impl Clone for Line {
}
}
+/// The character of a cell of one printable ASCII character.
+fn printable_ascii(cell: &Cell) -> Option<char> {
+ match *cell.str().as_bytes() {
+ [byte @ b' '..=b'~'] => Some(byte as char),
+ _ => None,
+ }
+}
+
impl PartialEq for Line {
fn eq(&self, other: &Self) -> bool {
self.seqno == other.seqno && self.bits == other.bits && self.cells == other.cells
@@ -715,7 +723,7 @@ impl Line {
}
Cow::Owned(s)
}
- CellStorage::C(cl) => Cow::Borrowed(&cl.text),
+ CellStorage::C(cl) => Cow::Borrowed(cl.text()),
}
}
@@ -842,13 +850,27 @@ impl Line {
width: usize,
attr: CellAttributes,
seqno: SequenceNo,
+ ) {
+ self.set_cell_grapheme_after(idx, text, width, attr, false, seqno);
+ }
+
+ /// `set_cell_grapheme`, where `follows` says that `text` is known to be
+ /// separate from what is before it.
+ pub fn set_cell_grapheme_after(
+ &mut self,
+ idx: usize,
+ text: &str,
+ width: usize,
+ attr: CellAttributes,
+ follows: bool,
+ seqno: SequenceNo,
) {
if attr.hyperlink().is_some() {
self.bits |= LineBits::HAS_HYPERLINK;
}
if let CellStorage::C(cl) = &mut self.cells {
- if idx == cl.len() {
+ if idx == cl.len() && (follows || !cl.joins(text)) {
cl.append_grapheme(text, width, attr);
self.invalidate_implicit_hyperlinks(seqno);
self.invalidate_zones();
@@ -860,12 +882,14 @@ impl Line {
}
/// Whether `append_grapheme_run` can take graphemes starting at `idx`:
- /// the end of clustered storage without implicit hyperlinks, which the
- /// first write would otherwise expand to cells.
+ /// the end of clustered storage, or where only blanks are left of it,
+ /// without implicit hyperlinks, which the first write would otherwise
+ /// expand to cells.
pub fn can_append_grapheme_run(&self, idx: usize) -> bool {
match &self.cells {
CellStorage::C(cl) => {
- idx == cl.len() && (self.bits & LineBits::HAS_IMPLICIT_HYPERLINKS) == LineBits::NONE
+ (idx == cl.len() || cl.is_blank_from(idx))
+ && (self.bits & LineBits::HAS_IMPLICIT_HYPERLINKS) == LineBits::NONE
}
_ => false,
}
@@ -873,7 +897,8 @@ impl Line {
/// `set_cell_grapheme` for each grapheme of a run written one after
/// another from `idx`, all with `attr`; see `ClusteredLine::append_run`
- /// for the arguments. Returns false, changing nothing, unless
+ /// for the arguments. `follows` says that `text` is known to be separate
+ /// from what is before it. Returns false, changing nothing, unless
/// `can_append_grapheme_run(idx)` and `append_run` takes the run; the
/// caller then writes the graphemes one at a time.
pub fn append_grapheme_run(
@@ -885,6 +910,7 @@ impl Line {
wide: &[u16],
last_width: u8,
attr: &CellAttributes,
+ follows: bool,
seqno: SequenceNo,
) -> bool {
if !self.can_append_grapheme_run(idx) {
@@ -893,7 +919,20 @@ impl Line {
let CellStorage::C(cl) = &mut self.cells else {
return false;
};
- if !cl.append_run(text, max_len, cells, wide, last_width, attr) {
+ if !follows && cl.joins_at(idx, text) {
+ return false;
+ }
+ // Blanks make way for the run, and those it does not reach stay.
+ let len = cl.len();
+ if idx < len && runs_into(text.as_bytes(), " ") {
+ return false;
+ }
+ cl.truncate_blanks(idx);
+ let appended = cl.append_run(text, max_len, cells, wide, last_width, attr);
+ if cl.len() < len {
+ cl.append_ascii_repeated(' ', len - cl.len(), &CellAttributes::blank());
+ }
+ if !appended {
return false;
}
// What each set_cell_grapheme would do; repeating it changes nothing.
@@ -925,7 +964,7 @@ impl Line {
}
Self::pad_clustered_line(cl, idx);
}
- if idx == cl.len() {
+ if idx == cl.len() && !cl.joins(text) {
cl.append_grapheme(text, width, attr);
self.invalidate_implicit_hyperlinks(seqno);
self.invalidate_zones();
@@ -940,35 +979,67 @@ impl Line {
// Keep gap filling off the overwrite path, which never needs padding.
#[inline(never)]
fn pad_clustered_line(cl: &mut ClusteredLine, idx: usize) {
- // Tabs, cursor movement and LF without CR leave gaps. Their
- // padding has one attribute run, so append it in chunks instead
- // of repeating grapheme/attribute bookkeeping for every space.
- const SPACES: &str = concat!(
- " ",
- " "
- );
- let blank = CellAttributes::blank();
- while cl.len() < idx {
- let count = (idx - cl.len()).min(SPACES.len());
- cl.append_ascii(&SPACES[..count], &blank);
+ // Tabs, cursor movement and LF without CR leave gaps.
+ let count = idx.saturating_sub(cl.len());
+ cl.append_ascii_repeated(' ', count, &CellAttributes::blank());
+ }
+
+ /// Write ASCII over `range` of the cells without expanding compact storage;
+ /// see `ClusteredLine::overwrite_ascii`. What each write of a cell would
+ /// do to the rest of the line is done here if that went ahead.
+ fn overwrite_ascii(
+ &mut self,
+ range: Range<usize>,
+ attr: &CellAttributes,
+ seqno: SequenceNo,
+ write: impl FnOnce(&mut [u8]),
+ ) -> bool {
+ if (self.bits & LineBits::HAS_IMPLICIT_HYPERLINKS) != LineBits::NONE {
+ return false;
}
+ let CellStorage::C(cl) = &mut self.cells else {
+ return false;
+ };
+ if !cl.overwrite_ascii(range, attr, write) {
+ return false;
+ }
+ if attr.hyperlink().is_some() {
+ self.bits |= LineBits::HAS_HYPERLINK;
+ }
+ self.invalidate_implicit_hyperlinks(seqno);
+ self.invalidate_zones();
+ self.update_last_change_seqno(seqno);
+ true
}
/// Write a run of printable ASCII with the same semantics as individual
- /// `set_cell_grapheme` calls. Invalid input returns false without mutation.
- /// Appending keeps compact storage; overwriting preserves image placements.
- pub fn set_ascii_cells(
+ /// `set_cell_grapheme` calls. Appending keeps compact storage; overwriting
+ /// preserves image placements.
+ pub fn set_printable_ascii_cells(
&mut self,
idx: usize,
text: &str,
attr: &CellAttributes,
seqno: SequenceNo,
- ) -> bool {
- if !text.bytes().all(|b| (b' '..=b'~').contains(&b)) {
- return false;
- }
+ ) {
+ debug_assert!(text.bytes().all(|b| (b' '..=b'~').contains(&b)));
if text.is_empty() {
- return true;
+ return;
+ }
+ // Over cells of the same sort, a row can stay as it is.
+ let over = text.len().min(self.len().saturating_sub(idx));
+ if over > 0
+ && self.overwrite_ascii(idx..idx + over, attr, seqno, |cells| {
+ cells.copy_from_slice(&text.as_bytes()[..over])
+ })
+ {
+ if over == text.len() {
+ return;
+ }
+ if let CellStorage::C(cl) = &mut self.cells {
+ cl.append_ascii(&text[over..], attr);
+ }
+ return;
}
// An append cannot overlap a wide cell. Write the whole run without
// allocating attributes for a scalar first cell, unless hyperlink
@@ -980,14 +1051,14 @@ impl Line {
== LineBits::NONE
{
if let CellStorage::C(cl) = &mut self.cells {
- if idx == cl.len() {
+ if idx == cl.len() && !cl.joins(text) {
cl.append_ascii(text, attr);
if attr.hyperlink().is_some() {
self.bits |= LineBits::HAS_HYPERLINK;
}
self.invalidate_zones();
self.update_last_change_seqno(seqno);
- return true;
+ return;
}
}
}
@@ -1025,7 +1096,6 @@ impl Line {
}
}
}
- true
}
pub fn set_cell_clearing_image_placements(
@@ -1069,7 +1139,7 @@ impl Line {
// their intended cell content
cl.append_grapheme(" ", 1, CellAttributes::blank());
}
- if idx == cl.len() {
+ if idx == cl.len() && !cl.joins(cell.str()) {
cl.append(cell);
return;
}
@@ -1238,6 +1308,28 @@ impl Line {
// them all away again before we return; NOP
return;
}
+ if cols.start == 0
+ && cols.end >= self.len()
+ && cols.end <= u16::MAX as usize
+ && matches!(&self.cells, CellStorage::C(_))
+ && (self.bits & LineBits::HAS_IMPLICIT_HYPERLINKS) == LineBits::NONE
+ && *cell == Cell::blank()
+ {
+ // The whole row: no cell of it survives to need looking at, and
+ // blanks need no more than a run of spaces. A row of cells stays
+ // one, or a row that has to be would go back and forth. Taking
+ // links back out of a row turns it into cells, so not one of those.
+ self.invalidate_implicit_hyperlinks(seqno);
+ self.invalidate_zones();
+ self.update_last_change_seqno(seqno);
+ if let CellStorage::C(cl) = &mut self.cells {
+ if !cl.clear_for_scrolling(cols.end.max(80).saturating_mul(2)) {
+ *cl = ClusteredLine::new();
+ }
+ cl.append_ascii_repeated(' ', cols.end, cell.attrs());
+ }
+ return;
+ }
if !cols.is_empty()
&& cols.start < self.len()
&& cols.end >= self.len()
@@ -1268,7 +1360,15 @@ impl Line {
// Preserve that behavior, including styled wide-cell padding.
cols.start += 1;
}
+ // Compact storage only keeps a row of blanks it was overwriting: one
+ // that got this far by appending blanks has always lost them again.
+ let overwriting = cols.start < self.len() && !cols.is_empty();
self.set_cell_range_impl(cols, cell, true, seqno);
+ if let CellStorage::C(cl) = &self.cells {
+ if overwriting && *cell == Cell::blank() && cl.is_all_blank() {
+ return;
+ }
+ }
self.prune_trailing_blanks(seqno);
}
@@ -1286,6 +1386,26 @@ impl Line {
clear_image_placements: bool,
seqno: SequenceNo,
) {
+ if cols.is_empty() {
+ return;
+ }
+ if let Some(c) = printable_ascii(cell) {
+ let over = cols.end.min(self.len());
+ if cols.start < over
+ && self.overwrite_ascii(cols.start..over, cell.attrs(), seqno, |cells| {
+ cells.fill(c as u8)
+ })
+ {
+ cols.start = over;
+ if cols.is_empty() {
+ return;
+ }
+ if let CellStorage::C(cl) = &mut self.cells {
+ cl.append_ascii_repeated(c, cols.len(), cell.attrs());
+ return;
+ }
+ }
+ }
let Some(first) = cols.next() else {
return;
};
@@ -1310,6 +1430,12 @@ impl Line {
}
return;
}
+ if let (CellStorage::C(cl), Some(c)) = (&mut self.cells, printable_ascii(cell)) {
+ if cols.start == cl.len() {
+ cl.append_ascii_repeated(c, cols.len(), cell.attrs());
+ return;
+ }
+ }
}
// Preserve compact append storage and the scalar semantics of wide or
@@ -1386,6 +1512,15 @@ impl Line {
.find(|cell| cell.cell_index() == cell_index)
}
+ /// The cell at `cell_index` of a row of nothing but one-byte cells in
+ /// compact storage, where it can be had without expanding the row.
+ pub fn ascii_cell(&self, cell_index: usize) -> Option<Cell> {
+ match &self.cells {
+ CellStorage::C(cl) => cl.get_byte_cell(cell_index).map(|cell| cell.as_cell()),
+ CellStorage::V(_) => None,
+ }
+ }
+
pub fn cluster(&self, bidi_hint: Option<ParagraphDirectionHint>) -> Vec<CellCluster> {
CellCluster::make_cluster(self.len(), self.visible_cells(), bidi_hint)
}
@@ -1417,7 +1552,21 @@ impl Line {
CellStorage::V(v) => ClusteredLine::from_cell_vec(v.len(), self.visible_cells()),
CellStorage::C(_) => return,
};
- self.cells = CellStorage::C(cv);
+ if cv.reads_back_as(self.visible_cells()) {
+ self.cells = CellStorage::C(cv);
+ }
+ }
+
+ /// Make a compact line as long as `len` cells, where blanks written
+ /// beyond its end have been left implicit. They matter once what follows
+ /// on the next row is to be read as following them.
+ pub fn keep_blanks_to(&mut self, len: usize) {
+ if let CellStorage::C(cl) = &mut self.cells {
+ if cl.len() < len {
+ Self::pad_clustered_line(cl, len);
+ self.invalidate_zones();
+ }
+ }
}
pub fn cells_mut(&mut self) -> &mut [Cell] {
diff --git a/wezterm-surface/src/line/test.rs b/wezterm-surface/src/line/test.rs
index ed79f328..8cab6fa2 100644
--- a/wezterm-surface/src/line/test.rs
+++ b/wezterm-surface/src/line/test.rs
@@ -82,7 +82,7 @@ fn compact_gap_padding_matches_individual_blank_cells() {
#[test]
fn scroll_reset_discards_oversized_storage() {
let mut line = Line::new(1);
- line.set_ascii_cells(0, &"x".repeat(8192), &CellAttributes::default(), 2);
+ line.set_printable_ascii_cells(0, &"x".repeat(8192), &CellAttributes::default(), 2);
line.reset_for_scrolling(3, 116);
assert_eq!(line, Line::new(3));
match &line.cells {
@@ -167,6 +167,208 @@ fn recycled_rows_match_new_rows_and_release_links_and_cached_data() {
}
}
+/// Two rows are the same row whichever storage each happens to be in: writing
+/// ASCII over a compact row leaves it compact, where a cell at a time expands it.
+#[track_caller]
+fn assert_same_row(actual: &Line, expected: &Line, what: impl core::fmt::Debug) {
+ let cells = |line: &Line| {
+ line.visible_cells()
+ .map(|cell| {
+ (
+ cell.cell_index(),
+ cell.str().to_string(),
+ cell.width(),
+ cell.attrs().clone(),
+ )
+ })
+ .collect::<Vec<_>>()
+ };
+ assert_eq!(cells(actual), cells(expected), "{what:?}");
+ assert_eq!(actual.len(), expected.len(), "{what:?}");
+ assert_eq!(actual.current_seqno(), expected.current_seqno(), "{what:?}");
+ assert_eq!(
+ actual.compute_shape_hash(),
+ expected.compute_shape_hash(),
+ "{what:?}"
+ );
+}
+
+/// Drawing a row looks for links in it, and leaves it clustered with them.
+#[test]
+fn erasing_a_row_scanned_for_links_erases_it() {
+ let rules = [Rule::new(r"\b\w+://\S+", "$0").unwrap()];
+ let mut line: Line = "see https://example.com/x".into();
+ line.compress_for_scrollback();
+ Line::apply_hyperlink_rules(&rules, &mut [&mut line]);
+ assert!(line.has_hyperlink());
+ assert!(matches!(&line.cells, super::storage::CellStorage::C(_)));
+
+ let len = line.len();
+ line.fill_range(0..len, &Cell::blank(), 2);
+ assert_eq!(line.as_str().trim_end(), "");
+ assert!(!line.has_hyperlink());
+}
+
+/// A clustered row has only its text to say where its cells are, so cells
+/// written one by one that would read back as one grapheme can't be kept in it.
+#[test]
+fn cells_that_would_read_back_as_one_stay_cells() {
+ let plain = CellAttributes::default();
+ let cells = |line: &Line| {
+ line.visible_cells()
+ .map(|cell| (cell.cell_index(), cell.str().to_string()))
+ .collect::<Vec<_>>()
+ };
+ for (first, second) in [
+ ("\u{e01}", "\u{e33}"),
+ ("🇺", "🇸"),
+ ("👍", "🏻"),
+ ("\u{1100}", "\u{1161}"),
+ ] {
+ let first_cell = Cell::new_grapheme(first, plain.clone(), None);
+ let second_cell = Cell::new_grapheme(second, plain.clone(), None);
+ let (first_width, second_width) = (first_cell.width(), second_cell.width());
+ let (next, last) = (1 + first_width, 1 + first_width + second_width);
+ let expected = vec![
+ (0, "a".to_string()),
+ (1, first.to_string()),
+ (next, second.to_string()),
+ (last, "z".to_string()),
+ ];
+ let mut one_by_one = Line::new(1);
+ one_by_one.set_cell_grapheme(0, "a", 1, plain.clone(), 2);
+ one_by_one.set_cell_grapheme(1, first, first_width, plain.clone(), 2);
+ one_by_one.set_cell_grapheme(next, second, second_width, plain.clone(), 2);
+ one_by_one.set_cell_grapheme(last, "z", 1, plain.clone(), 2);
+ assert_eq!(cells(&one_by_one), expected);
+
+ let mut as_cells = Line::new(1);
+ as_cells.set_cell(0, Cell::new('a', plain.clone()), 2);
+ as_cells.set_cell(1, first_cell, 2);
+ as_cells.set_cell(next, second_cell, 2);
+ as_cells.set_cell(last, Cell::new('z', plain.clone()), 2);
+ as_cells.compress_for_scrollback();
+ assert_eq!(cells(&as_cells), expected);
+
+ // A run is refused, for the caller to write it a cell at a time.
+ let mut run = Line::new(1);
+ run.set_cell_grapheme(0, first, first_width, plain.clone(), 2);
+ let wide: &[u16] = if second_width == 2 { &[0] } else { &[] };
+ let last_width = second_width as u8;
+ assert!(!run.append_grapheme_run(
+ first_width,
+ second,
+ 4,
+ second_width,
+ wide,
+ last_width,
+ &plain,
+ false,
+ 2
+ ));
+ }
+ // Text that was written together is one grapheme, as it always was.
+ let mut together = Line::new(1);
+ together.set_cell_grapheme(0, "\u{e01}\u{e33}", 1, plain.clone(), 2);
+ together.set_cell_grapheme(1, "z", 1, plain, 2);
+ assert_eq!(
+ cells(&together),
+ vec![(0, "\u{e01}\u{e33}".to_string()), (1, "z".to_string())]
+ );
+}
+
+/// Nothing assumes a row's cells add up to its length: rows can come off the
+/// wire from a build that merged them.
+#[test]
+fn cells_read_back_short_of_the_length_are_made_up_with_blanks() {
+ let mut cl = ClusteredLine::new();
+ cl.append_grapheme("\u{e01}", 1, CellAttributes::default());
+ cl.append_grapheme("\u{e33}", 1, CellAttributes::default());
+ cl.append_grapheme("z", 1, CellAttributes::default());
+ assert_eq!(cl.len(), 3);
+ assert_eq!(cl.to_cell_vec().len(), 3);
+}
+
+#[test]
+fn blanks_up_to_a_wrap_are_kept() {
+ let mut line = Line::new(1);
+ line.set_cell_grapheme(0, "a", 1, CellAttributes::default(), 2);
+ line.keep_blanks_to(5);
+ assert_eq!(line.len(), 5);
+ assert_eq!(line.as_str(), "a ");
+ line.keep_blanks_to(3);
+ assert_eq!(line.len(), 5);
+}
+
+/// A run over a tail of blanks has to come out as writing each of its
+/// graphemes over them does, and leave the row as long as it was.
+#[test]
+fn runs_over_blanks_match_scalar_writes() {
+ let plain = CellAttributes::default();
+ let mut bold = CellAttributes::default();
+ bold.set_intensity(wezterm_cell::Intensity::Bold);
+ let row: &[(&str, usize)] = &[("x", 1), ("中", 2), ("y", 1)];
+ let run: &[(&str, usize)] = &[("a", 1), ("中", 2), ("😀", 2), ("b", 1)];
+ let text: alloc::string::String = run.iter().map(|(g, _)| *g).collect();
+ let cells: usize = run.iter().map(|(_, w)| w).sum();
+ for prefix in 0..=row.len() {
+ for blanks in [1, 3, 6, 7, 40] {
+ let mut base = Line::new(1);
+ let mut x = 0;
+ for (g, w) in &row[..prefix] {
+ base.set_cell_grapheme(x, g, *w, plain.clone(), 2);
+ x += w;
+ }
+ base.set_cell_range(x..x + blanks, &Cell::blank(), 3);
+ // Blanks past the end of a compact row are left out of it.
+ let start = x;
+ if base.len() != x + blanks {
+ base.keep_blanks_to(x + blanks);
+ }
+ for attrs in [&plain, &bold] {
+ let mut expected = base.clone();
+ expected.cells_mut();
+ let mut at = start;
+ for (g, w) in run {
+ expected.set_cell_grapheme(at, g, *w, attrs.clone(), 4);
+ at += w;
+ }
+ let mut actual = base.clone();
+ if let crate::line::storage::CellStorage::C(cl) = &mut actual.cells {
+ cl.text.reserve_exact(80);
+ }
+ assert!(
+ actual.can_append_grapheme_run(start),
+ "{} {}",
+ prefix,
+ blanks
+ );
+ assert!(actual.append_grapheme_run(
+ start,
+ &text,
+ 4,
+ cells,
+ &[1, 3],
+ 1,
+ attrs,
+ false,
+ 4
+ ));
+ assert!(actual.is_compressed_for_scrollback());
+ assert_same_row(&actual, &expected, (prefix, blanks));
+ }
+ }
+ }
+ // Anything but a default blank in the way has to be written cell by cell.
+ let mut base = Line::new(1);
+ base.set_cell_grapheme(0, "a", 1, plain.clone(), 2);
+ base.set_cell_grapheme(1, " ", 1, bold.clone(), 2);
+ base.set_cell_grapheme(2, " ", 1, plain.clone(), 2);
+ assert!(!base.can_append_grapheme_run(1));
+ assert!(base.can_append_grapheme_run(2));
+ assert!(!base.can_append_grapheme_run(4));
+}
+
// Keep a scalar reference so the fast path is checked against the old behavior,
// including less visible state such as hyperlinks, sequence numbers and zones.
fn scalar_range(line: &mut Line, range: core::ops::Range<usize>, cell: &Cell, clear: bool) {
@@ -199,10 +401,7 @@ fn check_range_against_scalar(
} else {
actual.set_cell_range(range.clone(), cell, 42);
}
- assert_eq!(
- actual, expected,
- "range={range:?}, cell={cell:?}, clear={clear}"
- );
+ assert_same_row(&actual, &expected, (&range, cell, clear));
assert_eq!(
actual.semantic_zone_ranges(),
expected.semantic_zone_ranges()
@@ -942,8 +1141,8 @@ fn check_ascii_against_scalar(base: &Line, start: usize, text: &str, attrs: &Cel
);
}
let mut actual = base.clone();
- assert!(actual.set_ascii_cells(start, text, attrs, 42));
- assert_eq!(actual, expected, "start={start}, text={text:?}");
+ actual.set_printable_ascii_cells(start, text, attrs, 42);
+ assert_same_row(&actual, &expected, (start, text));
assert_eq!(
actual.semantic_zone_ranges(),
expected.semantic_zone_ranges()
@@ -1023,21 +1222,11 @@ fn ascii_append_keeps_compact_storage_and_splits_full_attribute_clusters() {
for len in [65534, 65535, 65536, 131073] {
let text = "x".repeat(len);
check_ascii_against_scalar(&base, base.len(), &text, &attrs);
- assert!(base.set_ascii_cells(base.len(), &text, &attrs, 42));
+ base.set_printable_ascii_cells(base.len(), &text, &attrs, 42);
assert!(base.is_compressed_for_scrollback());
}
}
-#[test]
-fn invalid_ascii_input_is_rejected_without_mutating_the_line() {
- for text in ["a\nb", "a\0b", "a\x7fb", "a界", "e\u{301}"] {
- let mut line: Line = "original".into();
- let original = line.clone();
- assert!(!line.set_ascii_cells(3, text, &CellAttributes::default(), 42));
- assert_eq!(line, original);
- }
-}
-
#[cfg(feature = "use_image")]
#[test]
fn ascii_overwrite_preserves_image_placements() {
@@ -1091,7 +1280,7 @@ fn blank_fill_to_the_end_matches_scalar_writes_then_prune() {
expected.set_cell(x, Cell::blank(), 7);
}
expected.prune_trailing_blanks(7);
- assert_eq!(actual, expected, "text={text:?} {start}..{end}");
+ assert_same_row(&actual, &expected, (text, start, end));
}
}
}
@@ -1215,7 +1404,7 @@ fn grapheme_runs_match_one_grapheme_at_a_time() {
};
let can = actual.can_append_grapheme_run(idx);
let appended = actual.append_grapheme_run(
- idx, &text, max_len, cells, &wide, last, attrs, 5,
+ idx, &text, max_len, cells, &wide, last, attrs, false, 5,
);
// Refused where the text's growth would depend
// on each grapheme's length: one longer than the