From d327527a3db3b0ea03e1f6d09f9070b5d0f35f85 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Mon, 17 Aug 2026 19:23:48 +0000 Subject: [PATCH] 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 --- ...perlinks_follow_scrolled_wrapped_rows.snap | 6 ++ codex-rs/tui/src/terminal_hyperlinks.rs | 69 +++++++++++++++++-- 2 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 codex-rs/tui/src/snapshots/codex_tui__terminal_hyperlinks__tests__buffer_hyperlinks_follow_scrolled_wrapped_rows.snap diff --git a/codex-rs/tui/src/snapshots/codex_tui__terminal_hyperlinks__tests__buffer_hyperlinks_follow_scrolled_wrapped_rows.snap b/codex-rs/tui/src/snapshots/codex_tui__terminal_hyperlinks__tests__buffer_hyperlinks_follow_scrolled_wrapped_rows.snap new file mode 100644 index 0000000000..093045056c --- /dev/null +++ b/codex-rs/tui/src/snapshots/codex_tui__terminal_hyperlinks__tests__buffer_hyperlinks_follow_scrolled_wrapped_rows.snap @@ -0,0 +1,6 @@ +--- +source: tui/src/terminal_hyperlinks.rs +expression: terminal.backend() +--- +visible- +link diff --git a/codex-rs/tui/src/terminal_hyperlinks.rs b/codex-rs/tui/src/terminal_hyperlinks.rs index b7ea9d75d9..62bc57f5c3 100644 --- a/codex-rs/tui/src/terminal_hyperlinks.rs +++ b/codex-rs/tui/src/terminal_hyperlinks.rs @@ -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::(); + 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";