Skip to content

Commit ebd0765

Browse files
etraut-openaicopyberry
authored andcommitted
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
1 parent f0c0f0a commit ebd0765

19 files changed

Lines changed: 457 additions & 37 deletions

‎codex-rs/tui/src/app.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -923,6 +923,7 @@ impl App {
923923
self.agents_overview.workspace.divider_drag_viewport = None;
924924
}
925925
}
926+
self.transcript_view.bidi_layout = tui.terminal.bidi_layout;
926927
self.transcript_view.agent_links = self
927928
.side_retention_enabled(tui)
928929
.then(|| app_server.backend_id());

‎codex-rs/tui/src/app/owned_transcript.rs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::motion::MotionMode;
2626
use crate::pager_overlay::TranscriptHistoryState;
2727
use crate::sidebar_tab_strip::TabState;
2828
use crate::terminal_hyperlinks::HyperlinkLine;
29+
use crate::terminal_hyperlinks::bidi;
2930
use crate::transcript_image::ImagePlacement;
3031
use crate::transcript_view::TranscriptView;
3132
use crate::transcript_view::ViewAction;
@@ -234,8 +235,11 @@ impl App {
234235
chat_widget.active_cell_transcript_hyperlink_lines(width)
235236
})
236237
} else if let Some(row) = live_summary {
238+
let bidi_layout = view.bidi_layout;
237239
view.sync_live_layout(transcript_width, active_key, |width| {
238-
Some(row.layout(width, expanded, chat_widget.active_auxiliary_lines(width)))
240+
let lines =
241+
bidi::session_lines(chat_widget.active_auxiliary_lines(width), bidi_layout);
242+
Some(row.layout(width, expanded, lines))
239243
})
240244
} else {
241245
view.sync_live_activity_tail(transcript_width, active_key, expanded, |width| {
@@ -394,6 +398,7 @@ impl App {
394398
id == panes.passive_thread && self.agent_navigation.is_parent_owned(id)
395399
});
396400
if let Some(companion) = self.side_companions.get_mut(&panes.passive_thread) {
401+
companion.view.bidi_layout = self.transcript_view.bidi_layout;
397402
companion.view.agent_links = self.transcript_view.agent_links;
398403
companion.view.image_protocol = self.transcript_view.image_protocol;
399404
panes.paint_companion(

‎codex-rs/tui/src/app/tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13231,6 +13231,7 @@ async fn clear_only_ui_reset_preserves_chat_session_state() {
1323113231
crate::keymap::RuntimeKeymap::defaults().pager,
1323213232
/*copy_on_select*/ false,
1323313233
/*mouse_scroll_speed*/ 1.0,
13234+
/*bidi_layout*/ false,
1323413235
));
1323513236
app.deferred_history_lines = vec![Line::from("stale buffered line").into()];
1323613237
app.has_emitted_history_lines = true;

‎codex-rs/tui/src/app_backtrack.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ impl App {
215215
.tui
216216
.mouse_scroll_speed
217217
.unwrap_or(/*default*/ 1.0),
218+
tui.terminal.bidi_layout,
218219
)
219220
.with_copy_on_select_clear_selection(
220221
self.local_settings.tui.copy_on_select_clear_selection,

‎codex-rs/tui/src/clipboard_copy/worker_tests.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ async fn blocked_copy_allows_overlay_exit_rejects_backlog_and_wakes_completion()
131131
RuntimeKeymap::defaults().pager,
132132
/*copy_on_select*/ false,
133133
/*mouse_scroll_speed*/ 1.0,
134+
/*bidi_layout*/ false,
134135
);
135136
overlay
136137
.handle_event(

‎codex-rs/tui/src/pager_overlay.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@ impl Overlay {
6161
keymap: PagerKeymap,
6262
copy_on_select: bool,
6363
mouse_scroll_speed: f64,
64+
bidi_layout: bool,
6465
) -> Self {
6566
let mut overlay = TranscriptOverlay::new(cells, keymap);
67+
overlay.view.bidi_layout = bidi_layout;
6668
overlay.view.copy_on_select = copy_on_select;
6769
overlay.view.mouse_scroll_speed = mouse_scroll_speed;
6870
overlay.view.primary_selection = crate::clipboard_copy::primary::available();

‎codex-rs/tui/src/resume_picker.rs‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,7 @@ async fn run_session_picker_event_loop(
781781
state.copy_on_select = options.copy_on_select;
782782
state.copy_on_select_clear_selection = options.copy_on_select_clear_selection;
783783
state.mouse_scroll_speed = options.mouse_scroll_speed;
784+
state.bidi_layout = alt.tui.terminal.bidi_layout;
784785
state.density = options.initial_density;
785786
state.view_persistence = options.view_persistence;
786787
state.keymap = options.keymap;
@@ -1148,6 +1149,7 @@ struct PickerState {
11481149
copy_on_select: bool,
11491150
copy_on_select_clear_selection: bool,
11501151
mouse_scroll_speed: f64,
1152+
bidi_layout: bool,
11511153
// Resolve local filesystem membership once per cwd for each page-loading cycle.
11521154
local_cwd_matches: HashMap<PathBuf, bool>,
11531155
requester: FrameRequester,
@@ -1372,6 +1374,7 @@ impl PickerState {
13721374
copy_on_select: false,
13731375
copy_on_select_clear_selection: false,
13741376
mouse_scroll_speed: 1.0,
1377+
bidi_layout: false,
13751378
requester,
13761379
relative_time_reference: None,
13771380
pagination: PaginationState::new(),
@@ -1524,6 +1527,7 @@ impl PickerState {
15241527
self.keymap.pager.clone(),
15251528
self.copy_on_select,
15261529
self.mouse_scroll_speed,
1530+
self.bidi_layout,
15271531
)
15281532
.with_copy_on_select_clear_selection(self.copy_on_select_clear_selection);
15291533
if let Overlay::Transcript(view) = &mut overlay {
@@ -5982,8 +5986,6 @@ mod tests {
59825986

59835987
#[tokio::test]
59845988
async fn loaded_transcript_waits_for_loading_frame_before_opening_overlay() {
5985-
use crate::history_cell::PlainHistoryCell;
5986-
59875989
let thread_id = ThreadId::new();
59885990
let (tx, mut receipts) = tokio::sync::mpsc::unbounded_channel();
59895991
let mut state = PickerState::new(
@@ -6006,8 +6008,13 @@ mod tests {
60066008
let owner = BackendId::new();
60076009
let key = (owner, thread_id);
60086010
state.pending_transcript_open = Some(key);
6009-
let cells: TranscriptCells =
6010-
vec![Arc::new(PlainHistoryCell::new(vec!["transcript".into()]))];
6011+
state.bidi_layout = true;
6012+
let cells: TranscriptCells = vec![Arc::new(crate::history_cell::new_user_prompt(
6013+
"transcript שלום".into(),
6014+
vec![],
6015+
vec![],
6016+
vec![],
6017+
))];
60116018

60126019
state
60136020
.transcript_cells
@@ -6110,6 +6117,7 @@ mod tests {
61106117
.iter()
61116118
.map(ratatui::buffer::Cell::symbol)
61126119
.collect::<String>();
6120+
assert!(text.contains("transcript םולש"), "{text}");
61136121
assert!(text.contains("BAtail"), "Find query: {text}");
61146122

61156123
state.overlay = None;
@@ -6152,6 +6160,7 @@ mod tests {
61526160
state.keymap.pager.clone(),
61536161
state.copy_on_select,
61546162
state.mouse_scroll_speed,
6163+
state.bidi_layout,
61556164
));
61566165
let mut tui = crate::tui::test_support::make_test_tui().expect("tui");
61576166
state

‎codex-rs/tui/src/terminal_hyperlinks/bidi.rs‎

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use icu_properties::CodePointMapData;
1212
use icu_properties::props::BidiMirroringGlyph;
1313
use ratatui::text::Line;
1414
use std::borrow::Cow;
15+
use std::ops::Range;
1516
use std::sync::Arc;
1617
use unicode_bidi::BidiClass;
1718
use unicode_bidi::Level;
@@ -26,6 +27,13 @@ pub(crate) fn prepare_message(lines: &mut [HyperlinkLine]) {
2627
}
2728
}
2829

30+
pub(crate) fn session_lines(mut lines: Vec<HyperlinkLine>, enabled: bool) -> Vec<HyperlinkLine> {
31+
if enabled {
32+
resolve_paragraphs(&mut lines);
33+
}
34+
lines
35+
}
36+
2937
pub(crate) fn resolve_paragraphs(lines: &mut [HyperlinkLine]) {
3038
for group in lines.chunk_by_mut(|a, b| {
3139
matches!(
@@ -77,6 +85,12 @@ pub(crate) fn resolve_paragraphs(lines: &mut [HyperlinkLine]) {
7785
}
7886
}
7987

88+
/// A projected row and the logical body bytes under each of its visible body cells.
89+
pub(crate) struct VisualRow {
90+
pub(crate) line: HyperlinkLine,
91+
pub(crate) offsets: Vec<Range<usize>>,
92+
}
93+
8094
pub(crate) fn paint_rows(lines: &mut [HyperlinkLine], width: usize) {
8195
resolve_paragraphs(lines);
8296
for line in lines {
@@ -85,12 +99,12 @@ pub(crate) fn paint_rows(lines: &mut [HyperlinkLine], width: usize) {
8599
&& let Some(levels) = levels.get(source.range.clone())
86100
&& let Some(visual) = project(line, levels, width)
87101
{
88-
*line = visual;
102+
*line = visual.line;
89103
}
90104
}
91105
}
92106

93-
fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<HyperlinkLine> {
107+
pub(crate) fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<VisualRow> {
94108
let source = line.source.as_ref()?;
95109
// Never reorder across terminal autowrap boundaries (e.g. an oversized URL).
96110
if line.width() > width || levels.len() != source.range.len() {
@@ -130,14 +144,15 @@ fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<Hyper
130144
.peek()
131145
.copied()
132146
.filter(|link| link.columns.contains(&column));
133-
graphemes.push((grapheme, cells, span.style, link));
147+
graphemes.push((byte, grapheme, cells, span.style, link));
134148
column += cells;
135149
}
136150
let mut visual = HyperlinkLine::new(Line::default().style(line.line.style));
137151
visual.line.alignment = line.line.alignment;
152+
let mut offsets = Vec::new();
138153
column = 0;
139154
for index in ParagraphBidiInfo::reorder_visual(&row_levels) {
140-
let (grapheme, cells, style, link) = graphemes[index];
155+
let (byte, grapheme, cells, style, link) = graphemes[index];
141156
if cells == 0 {
142157
continue;
143158
}
@@ -156,7 +171,14 @@ fn project(line: &HyperlinkLine, levels: &[Level], width: usize) -> Option<Hyper
156171
if let Some(link) = link {
157172
push_link_range(&mut visual, column..column + cells, link);
158173
}
174+
if (start..start + body.len()).contains(&byte) {
175+
let offset = byte - start;
176+
offsets.resize(offsets.len() + cells, offset..offset + grapheme.len());
177+
}
159178
column += cells;
160179
}
161-
Some(visual)
180+
Some(VisualRow {
181+
line: visual,
182+
offsets,
183+
})
162184
}

‎codex-rs/tui/src/transcript_view.rs‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ mod text;
2626
mod turn_details;
2727
mod turn_tip;
2828

29+
use crate::terminal_hyperlinks::bidi;
2930
use std::sync::Arc;
3031

3132
use crate::chatwidget::ActiveCellTranscriptKey;
@@ -95,6 +96,7 @@ struct VisibleRow {
9596
/// Shared scrolling and interaction state for compact and detailed transcript presentations.
9697
pub(crate) struct TranscriptView {
9798
pub(crate) image_protocol: Option<crate::pets::ImageProtocol>,
99+
pub(crate) bidi_layout: bool,
98100
pub(crate) agent_links: Option<crate::backend_id::BackendId>,
99101
pub(crate) document_links: bool,
100102
pub(crate) mouse_scroll_speed: f64,
@@ -140,6 +142,7 @@ impl Default for TranscriptView {
140142
fn default() -> Self {
141143
Self {
142144
image_protocol: None,
145+
bidi_layout: false,
143146
agent_links: None,
144147
document_links: false,
145148
mouse_scroll_speed: 1.0,
@@ -523,8 +526,10 @@ impl TranscriptView {
523526
if self.detailed {
524527
self.sync_live_tail(width, /*key*/ None, lines);
525528
} else if let Some(row) = summary {
529+
let bidi_layout = self.bidi_layout;
526530
self.sync_live_layout(width, /*key*/ None, |width| {
527-
Some(row.layout(width, expanded, auxiliary.to_vec()))
531+
let lines = bidi::session_lines(auxiliary.to_vec(), bidi_layout);
532+
Some(row.layout(width, expanded, lines))
528533
});
529534
} else {
530535
self.sync_live_activity_tail(width, /*key*/ None, expanded && disclose, |width| {
@@ -569,8 +574,10 @@ impl TranscriptView {
569574
key: Option<ActiveCellTranscriptKey>,
570575
lines: impl FnOnce(u16) -> Option<Vec<HyperlinkLine>>,
571576
) -> bool {
577+
let bidi_layout = self.bidi_layout;
572578
self.sync_live_layout(width, key, |width| {
573-
lines(width).map(|lines| TextLayout::new(lines, width))
579+
lines(width)
580+
.map(|lines| TextLayout::new(bidi::session_lines(lines, bidi_layout), width))
574581
})
575582
}
576583

@@ -585,8 +592,10 @@ impl TranscriptView {
585592
.disclosure
586593
.keymap
587594
.primary_hint(crate::keymap::KeymapContext::Global, "open_transcript");
595+
let bidi_layout = self.bidi_layout;
588596
self.sync_live_layout(width, key, |width| {
589-
lines(width).map(|lines| layout::activity_layout(lines, width, expanded, shortcut))
597+
lines(width)
598+
.map(|lines| layout::activity_layout(lines, width, expanded, bidi_layout, shortcut))
590599
})
591600
}
592601

‎codex-rs/tui/src/transcript_view/input.rs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,15 @@ impl TranscriptView {
326326
if modifiers == KeyModifiers::CONTROL && code == KeyCode::Char(' ') {
327327
self.begin_selection(cells, self.area.x, self.area.y, /*clicks*/ 1);
328328
if let Some(selection) = &mut self.selection {
329+
// Ctrl+Space extends a half-open source range with the arrow keys.
330+
// Start at the logical boundary even if the leftmost glyph is RTL.
331+
if let Some(first) = self.visible.first()
332+
&& first.layout.visual_hit(first.row, /*column*/ 0).is_some()
333+
{
334+
let mut anchor = selection.start;
335+
anchor.offset = first.layout.position_at(first.row, /*column*/ 0);
336+
selection.set_keyboard_range(anchor, anchor);
337+
}
329338
selection.dragging = false;
330339
selection.resume_on_empty = false;
331340
}
@@ -367,6 +376,7 @@ impl TranscriptView {
367376
selection.end,
368377
selection.unit(),
369378
selection.has_table_scope(),
379+
selection.mouse_endpoints,
370380
)
371381
});
372382
let action =
@@ -377,6 +387,7 @@ impl TranscriptView {
377387
selection.end,
378388
selection.unit(),
379389
selection.has_table_scope(),
390+
selection.mouse_endpoints,
380391
)
381392
});
382393
if endpoint_changed && before != after {

0 commit comments

Comments
 (0)