Skip to content

Commit e140cdc

Browse files
committed
fix(render): pin OSC 8 cell width and adopt CellDiffOption::Skip
ratatui 0.30's diff iterator advances by each cell's computed `cell_width` for non-skip cells. Our OSC 8 wrap stores the full escape sequence in a single cell's symbol, so the printable characters inside it (URL, visible text) were counted as visible columns — the iterator jumped far past the trailing cells and into the next row, leaving subsequent linked rows undiffed and blank. Pin the wrap cell to `ForcedWidth(1)` so the iterator advances by one column, and replace the deprecated `set_skip(true)` calls on trailing cells with `set_diff_option(CellDiffOption::Skip)`. Mirror the read in `html_snapshot::emit_row` and its test fixtures.
1 parent 219de0f commit e140cdc

2 files changed

Lines changed: 20 additions & 14 deletions

File tree

‎src/render/list_links.rs‎

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
//!
44
//! How the OSC 8 plumbing works: ratatui's `Buffer` stores per-cell symbol strings that the
55
//! crossterm backend prints verbatim. We collapse the whole linked row into the first cell's
6-
//! symbol — `ESC ] 8 ; ; URL ESC \\ <text> ESC ] 8 ; ; ESC \\` — and mark the trailing cells with
7-
//! `skip = true` so ratatui's diff doesn't try to redraw them. That keeps the OSC 8 sequence
8-
//! atomic in the byte stream (no `MoveTo` interruption), and dodges `Buffer::diff`'s width
9-
//! calculation, which would otherwise count the printable characters inside the escape sequence
10-
//! as visible columns and incorrectly skip the cells immediately after the link.
6+
//! symbol — `ESC ] 8 ; ; URL ESC \\ <text> ESC ] 8 ; ; ESC \\` — pin its diff width to 1 via
7+
//! `CellDiffOption::ForcedWidth(1)`, and mark the trailing cells with `CellDiffOption::Skip` so
8+
//! ratatui's diff doesn't try to redraw them. The `ForcedWidth(1)` matters because ratatui 0.30's
9+
//! diff iterator advances by the cell's computed `cell_width` for non-skip cells; without the
10+
//! pin, the printable characters inside the escape sequence (URL, visible text) would be counted
11+
//! as visible columns and the iterator would jump far past the trailing cells — even spilling
12+
//! into the next row — so subsequent rows would never be diffed and would render blank.
1113
//!
1214
//! The `skip = true` trick only buys real-terminal correctness: `TestBackend` doesn't
1315
//! simulate the terminal's char-rendering of the OSC 8 visible portion, so the skipped cells
@@ -17,8 +19,9 @@
1719
//! capture, while the on-screen `CrosstermBackend` pass keeps the OSC 8 wrap.
1820
1921
use std::cell::Cell as StdCell;
22+
use std::num::NonZeroU16;
2023

21-
use ratatui::{Frame, buffer::Buffer, layout::Rect, style::Style};
24+
use ratatui::{Frame, buffer::Buffer, buffer::CellDiffOption, layout::Rect, style::Style};
2225

2326
use crate::options::OptionSchema;
2427
use crate::payload::{Body, LinkedLine, LinkedTextBlockData};
@@ -156,25 +159,28 @@ fn osc8_suppressed() -> bool {
156159
}
157160

158161
/// Re-export of the OSC 8 wrapping helper so sibling renderers (`list_cards`) can reuse the
159-
/// `set_skip` plumbing without duplicating the escape-sequence dance.
162+
/// diff-option plumbing without duplicating the escape-sequence dance.
160163
pub(crate) fn wrap_osc8(buf: &mut Buffer, x: u16, y: u16, end_x: u16, url: &str, style: Style) {
161164
if osc8_suppressed() {
162165
return;
163166
}
164167
wrap_link(buf, x, y, end_x, url, style);
165168
}
166169

170+
const FORCED_WIDTH_1: CellDiffOption = CellDiffOption::ForcedWidth(NonZeroU16::new(1).unwrap());
171+
167172
fn wrap_link(buf: &mut Buffer, x: u16, y: u16, end_x: u16, url: &str, style: Style) {
168173
let visible: String = (x..end_x)
169174
.filter_map(|col| buf.cell((col, y)).map(|c| c.symbol().to_string()))
170175
.collect();
171176
if let Some(cell) = buf.cell_mut((x, y)) {
172177
cell.set_symbol(&format!("\x1b]8;;{url}\x1b\\{visible}\x1b]8;;\x1b\\"));
173178
cell.set_style(style);
179+
cell.set_diff_option(FORCED_WIDTH_1);
174180
}
175181
for col in (x + 1)..end_x {
176182
if let Some(cell) = buf.cell_mut((col, y)) {
177-
cell.set_skip(true);
183+
cell.set_diff_option(CellDiffOption::Skip);
178184
}
179185
}
180186
}

‎xtask/src/html_snapshot.rs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
//! cells that share fg/bg/modifier. Default-styled cells emit plain text (no wrapping span) so
55
//! the rendered markup stays readable and small.
66
7-
use ratatui::buffer::Buffer;
7+
use ratatui::buffer::{Buffer, CellDiffOption};
88
use ratatui::style::{Color, Modifier, Style};
99
use unicode_width::UnicodeWidthStr;
1010

@@ -130,10 +130,10 @@ fn emit_row(out: &mut String, buf: &Buffer, y: u16, width: u16) {
130130
let mut x = 0u16;
131131
while x < width {
132132
let cell = buf[(x, y)].clone();
133-
// Cells flagged `skip` are placeholders for the visible region of an OSC 8 hyperlink
133+
// Cells flagged as Skip are placeholders for the visible region of an OSC 8 hyperlink
134134
// — the link's first cell carries the whole sequence; the trailing cells exist only
135135
// so terminal cursor math lines up. Drop them in HTML to avoid emitting stray spaces.
136-
if cell.skip {
136+
if matches!(cell.diff_option, CellDiffOption::Skip) {
137137
x += 1;
138138
continue;
139139
}
@@ -429,7 +429,7 @@ mod tests {
429429
fn osc8_hyperlink_becomes_anchor() {
430430
let mut buf = Buffer::empty(Rect::new(0, 0, 5, 1));
431431
buf[(0, 0)].set_symbol("\x1b]8;;https://example.com/\x1b\\Hi\x1b]8;;\x1b\\");
432-
buf[(1, 0)].set_skip(true);
432+
buf[(1, 0)].set_diff_option(CellDiffOption::Skip);
433433
let html = buffer_to_html(&buf);
434434
assert!(
435435
html.contains("<a href=\"https://example.com/\""),
@@ -460,8 +460,8 @@ mod tests {
460460
fn skip_cells_are_dropped() {
461461
let mut buf = Buffer::empty(Rect::new(0, 0, 4, 1));
462462
buf[(0, 0)].set_symbol("a");
463-
buf[(1, 0)].set_skip(true);
464-
buf[(2, 0)].set_skip(true);
463+
buf[(1, 0)].set_diff_option(CellDiffOption::Skip);
464+
buf[(2, 0)].set_diff_option(CellDiffOption::Skip);
465465
buf[(3, 0)].set_symbol("b");
466466
let html = buffer_to_html(&buf);
467467
assert_eq!(html.matches("<span class=\"c\">").count(), 2);

0 commit comments

Comments
 (0)