Render bidirectional text in full-screen transcripts (#63845)

## Why

The terminal's bidirectional layout was already used for scrollback, but
full-screen transcripts could still show mixed right-to-left and left-to-right
text in source order.

## What changed

Use the terminal's bidirectional layout setting in the live transcript,
history pager, and resume picker previews. Reorder each wrapped row for
display and map its screen columns back to the source graphemes. This keeps
hyperlink hit targets and search highlights aligned with the displayed text.
Mouse selection follows the displayed glyphs, while copying preserves source
order.

## Testing

Add regression coverage for mixed Arabic, Hebrew, and Latin text across
terminal widths, including link hit targets, highlights, and logical text
order. Exercise mouse and keyboard selection and verify right-to-left
rendering in the resume picker preview.

GitOrigin-RevId: ea0518ea8791d0fcc031dd0ebf306775918f373b
This commit is contained in:
Eric Traut
2026-11-25 06:40:44 +00:00
committed by copyberry
parent f0c0f0a14d
commit ebd0765aaf
19 changed files with 457 additions and 37 deletions
+27 -5
View File
@@ -12,6 +12,7 @@ use icu_properties::CodePointMapData;
use icu_properties::props::BidiMirroringGlyph;
use ratatui::text::Line;
use std::borrow::Cow;
use std::ops::Range;
use std::sync::Arc;
use unicode_bidi::BidiClass;
use unicode_bidi::Level;
@@ -26,6 +27,13 @@ pub(crate) fn prepare_message(lines: &mut [HyperlinkLine]) {
}
}
pub(crate) fn session_lines(mut lines: Vec<HyperlinkLine>, enabled: bool) -> Vec<HyperlinkLine> {
if enabled {
resolve_paragraphs(&mut lines);
}
lines
}
pub(crate) fn resolve_paragraphs(lines: &mut [HyperlinkLine]) {
for group in lines.chunk_by_mut(|a, b| {
matches!(
@@ -77,6 +85,12 @@ pub(crate) fn resolve_paragraphs(lines: &mut [HyperlinkLine]) {
}
}
/// A projected row and the logical body bytes under each of its visible body cells.
pub(crate) struct VisualRow {
pub(crate) line: HyperlinkLine,
pub(crate) offsets: Vec<Range<usize>>,
}
pub(crate) fn paint_rows(lines: &mut [HyperlinkLine], width: usize) {
resolve_paragraphs(lines);
for line in lines {
@@ -85,12 +99,12 @@ pub(crate) fn paint_rows(lines: &mut [HyperlinkLine], width: usize) {
&& let Some(levels) = levels.get(source.range.clone())
&& let Some(visual) = project(line, levels, width)
{
*line = visual;
*line = visual.line;
}
}
}
fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<HyperlinkLine> {
pub(crate) fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<VisualRow> {
let source = line.source.as_ref()?;
// Never reorder across terminal autowrap boundaries (e.g. an oversized URL).
if line.width() > width || levels.len() != source.range.len() {
@@ -130,14 +144,15 @@ fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<Hyper
.peek()
.copied()
.filter(|link| link.columns.contains(&column));
graphemes.push((grapheme, cells, span.style, link));
graphemes.push((byte, grapheme, cells, span.style, link));
column += cells;
}
let mut visual = HyperlinkLine::new(Line::default().style(line.line.style));
visual.line.alignment = line.line.alignment;
let mut offsets = Vec::new();
column = 0;
for index in ParagraphBidiInfo::reorder_visual(&row_levels) {
let (grapheme, cells, style, link) = graphemes[index];
let (byte, grapheme, cells, style, link) = graphemes[index];
if cells == 0 {
continue;
}
@@ -156,7 +171,14 @@ fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<Hyper
if let Some(link) = link {
push_link_range(&mut visual, column..column + cells, link);
}
if (start..start + body.len()).contains(&byte) {
let offset = byte - start;
offsets.resize(offsets.len() + cells, offset..offset + grapheme.len());
}
column += cells;
}
Some(visual)
Some(VisualRow {
line: visual,
offsets,
})
}