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.

codec: keep compressed lines compressed across serialization

RoversX68b0ac3b6d7c
diff --git a/codec/src/lib.rs b/codec/src/lib.rs
index 7ee8ff3d..0b42342d 100644
--- a/codec/src/lib.rs
+++ b/codec/src/lib.rs
@@ -1435,6 +1435,13 @@ impl From<Vec<(StableRowIndex, Line)>> for SerializedLines {
let mut images = vec![];
for (line_idx, (stable_row_idx, line)) in lines.iter_mut().enumerate() {
+ // The mutable pass below coerces the line to per-cell storage,
+ // undoing the compression scrollback just applied and shipping
+ // the fat form to the client. Only lines that actually carry
+ // links or images have anything to extract; skip the rest.
+ if !line.has_hyperlinks_or_images() {
+ continue;
+ }
let mut current_link: Option<Arc<Hyperlink>> = None;
let mut current_range = 0..0;
@@ -1821,6 +1828,42 @@ mod golden {
#[cfg(test)]
mod test {
use super::*;
+ use termwiz::cell::CellAttributes;
+
+ #[test]
+ fn serializing_lines_keeps_plain_lines_compressed() {
+ let mut line = Line::from_text("hello world", &CellAttributes::default(), 1, None);
+ line.compress_for_scrollback();
+
+ let serialized = SerializedLines::from(vec![(0, line)]);
+ assert!(serialized.hyperlinks.is_empty());
+
+ let (lines, images) = serialized.extract_data();
+ assert!(images.is_empty());
+ assert!(
+ lines[0].1.is_compressed_for_scrollback(),
+ "a line with nothing to extract must not be coerced to per-cell storage"
+ );
+ assert_eq!(lines[0].1.as_str(), "hello world");
+ }
+
+ #[test]
+ fn serializing_lines_still_extracts_and_restores_hyperlinks() {
+ let mut attrs = CellAttributes::default();
+ attrs.set_hyperlink(Some(Arc::new(Hyperlink::new("https://example.com"))));
+ let mut line = Line::from_text("link", &attrs, 1, None);
+ line.compress_for_scrollback();
+
+ let serialized = SerializedLines::from(vec![(0, line)]);
+ assert!(!serialized.hyperlinks.is_empty());
+
+ let (lines, _) = serialized.extract_data();
+ let cell = lines[0].1.visible_cells().next().unwrap();
+ assert_eq!(
+ cell.attrs().hyperlink().map(|link| link.uri()),
+ Some("https://example.com")
+ );
+ }
/// First coverage of the compressed path: every pre-existing test payload
/// sits under COMPRESS_THRESH and never touches zstd.
diff --git a/wezterm-surface/src/line/clusterline.rs b/wezterm-surface/src/line/clusterline.rs
index c6f5bca5..eb76f9f5 100644
--- a/wezterm-surface/src/line/clusterline.rs
+++ b/wezterm-surface/src/line/clusterline.rs
@@ -88,6 +88,14 @@ impl ClusteredLine {
}
}
+ /// Whether any cluster's attributes satisfy `pred`. A cell cannot carry
+ /// an attribute its cluster does not, so this answers per-cell questions
+ /// without expanding the line.
+ pub(crate) fn any_cluster_attrs(&self, pred: impl FnMut(&CellAttributes) -> bool) -> bool {
+ let mut pred = pred;
+ self.clusters.iter().any(|cluster| pred(&cluster.attrs))
+ }
+
pub fn to_cell_vec(&self) -> Vec<Cell> {
let mut cells = vec![];
diff --git a/wezterm-surface/src/line/line.rs b/wezterm-surface/src/line/line.rs
index 7c7bb322..b4d5780b 100644
--- a/wezterm-surface/src/line/line.rs
+++ b/wezterm-surface/src/line/line.rs
@@ -1016,6 +1016,32 @@ impl Line {
}
}
+ /// Whether the line currently sits in compact cluster storage.
+ pub fn is_compressed_for_scrollback(&self) -> bool {
+ matches!(&self.cells, CellStorage::C(_))
+ }
+
+ /// Whether any cell carries a hyperlink or attached image, answered
+ /// without decompressing cluster storage. Serialization uses this to
+ /// leave the great majority of lines, which carry neither, in their
+ /// compact form.
+ pub fn has_hyperlinks_or_images(&self) -> bool {
+ fn wants_extraction(attrs: &CellAttributes) -> bool {
+ if attrs.hyperlink().is_some() {
+ return true;
+ }
+ #[cfg(feature = "use_image")]
+ if attrs.images().is_some() {
+ return true;
+ }
+ false
+ }
+ match &self.cells {
+ CellStorage::V(cells) => cells.iter().any(|cell| wants_extraction(cell.attrs())),
+ CellStorage::C(cl) => cl.any_cluster_attrs(wants_extraction),
+ }
+ }
+
/// Iterates the visible cells, respecting the width of the cell.
/// For instance, a double-width cell overlaps the following (blank)
/// cell, so that blank cell is omitted from the iterator results.