Limit terminal hyperlink layout to the visible viewport (#39065)

## What changed

- Skip hyperlink layout for wrapped rows above the scroll offset and stop once
  processing reaches the bottom of the viewport.
- Resolve each rendered link destination once while marking its visible cells.
- Add coverage for hyperlinks that span visible wrapped rows after scrolling.

GitOrigin-RevId: c1506d47fa3f1b895966dc2cde7d5b859c201792
This commit is contained in:
Charlie Marsh
2026-08-17 20:06:41 +00:00
committed by copyberry
parent 1aa3a68e7b
commit d327527a3d
2 changed files with 68 additions and 7 deletions
@@ -0,0 +1,6 @@
---
source: tui/src/terminal_hyperlinks.rs
expression: terminal.backend()
---
visible-
link
+62 -7
View File
@@ -500,15 +500,20 @@ pub(crate) fn mark_buffer_hyperlinks(
lines: &[HyperlinkLine],
scroll_rows: usize,
) {
if area.width == 0 || lines.iter().all(|line| line.hyperlinks.is_empty()) {
if area.width == 0 || area.height == 0 || lines.iter().all(|line| line.hyperlinks.is_empty()) {
return;
}
let viewport_end = scroll_rows.saturating_add(usize::from(area.height));
let mut logical_row = 0usize;
for line in lines {
if logical_row >= viewport_end {
break;
}
let paragraph =
Paragraph::new(Text::from(line_to_borrowed(&line.line))).wrap(Wrap { trim: false });
let rendered_height = paragraph.line_count(area.width).max(/*other*/ 1);
if line.hyperlinks.is_empty() {
if line.hyperlinks.is_empty() || logical_row.saturating_add(rendered_height) <= scroll_rows
{
logical_row += rendered_height;
continue;
}
@@ -542,17 +547,18 @@ pub(crate) fn mark_buffer_hyperlinks(
})
.collect();
for (row, rendered) in remap_wrapped_line(line, rendered_lines).iter().enumerate() {
let row = logical_row + row;
if row < scroll_rows || row >= viewport_end {
continue;
}
for link in &rendered.hyperlinks {
let destination = link.terminal_destination();
let mut trailing_columns = 0usize;
for column in link.columns.clone() {
if trailing_columns > 0 {
trailing_columns -= 1;
continue;
}
let row = logical_row + row;
if row < scroll_rows || row - scroll_rows >= usize::from(area.height) {
continue;
}
let x = area.x + column as u16;
let y = area.y + (row - scroll_rows) as u16;
let cell = &mut buf[(x, y)];
@@ -560,7 +566,7 @@ pub(crate) fn mark_buffer_hyperlinks(
continue;
}
trailing_columns = usize::from(cell.cell_width()).saturating_sub(1);
let symbol = link.terminal_destination().map_or_else(
let symbol = destination.as_ref().map_or_else(
|| cell.symbol().to_string(),
|destination| {
format!("\x1b]8;;{destination}\x07{}\x1b]8;;\x07", cell.symbol())
@@ -789,6 +795,55 @@ mod tests {
assert_eq!(linked_text, destination);
}
#[test]
fn buffer_hyperlinks_follow_scrolled_wrapped_rows() {
let hidden_destination = "https://example.com/hidden";
let visible_destination = "https://example.com/visible";
let trailing_destination = "https://example.com/trailing";
let mut hidden = HyperlinkLine::new(Line::default());
hidden.push_span("hidden".into(), Some(hidden_destination));
let mut visible = HyperlinkLine::new(Line::from("prefix "));
visible.push_span("visible-link".into(), Some(visible_destination));
let mut trailing = HyperlinkLine::new(Line::default());
trailing.push_span("trailing".into(), Some(trailing_destination));
let lines = vec![hidden, visible, trailing];
let area = Rect::new(
/*x*/ 0, /*y*/ 0, /*width*/ 8, /*height*/ 2,
);
let backend = crate::test_backend::VT100Backend::new(area.width, area.height);
let mut terminal =
crate::custom_terminal::Terminal::with_options(backend).expect("terminal");
terminal.set_viewport_area(area);
terminal
.draw(|frame| {
let buf = frame.buffer_mut();
Paragraph::new(Text::from(visible_lines_ref(&lines)))
.wrap(Wrap { trim: false })
.scroll((/*vertical*/ 2, /*horizontal*/ 0))
.render(area, buf);
mark_buffer_hyperlinks(buf, area, &lines, /*scroll_rows*/ 2);
let linked_text = area
.positions()
.filter_map(|position| {
let symbol = buf[position].symbol();
symbol
.contains(&format!("\x1b]8;;{visible_destination}\x07"))
.then(|| strip_osc8(symbol))
})
.collect::<String>();
assert_eq!(linked_text, "visible-link");
})
.expect("render scrolled hyperlinks");
insta::assert_snapshot!(
"buffer_hyperlinks_follow_scrolled_wrapped_rows",
terminal.backend()
);
}
#[test]
fn buffer_hyperlinks_follow_wrapped_wide_glyphs() {
let destination = "https://example.com/wide";