diff --git a/codex-rs/tui/src/insert_history.rs b/codex-rs/tui/src/insert_history.rs index 5065a0aef2..f02e311225 100644 --- a/codex-rs/tui/src/insert_history.rs +++ b/codex-rs/tui/src/insert_history.rs @@ -14,6 +14,7 @@ use crate::terminal_hyperlinks::HyperlinkLine; use crate::terminal_hyperlinks::decorate_spans; use crate::terminal_hyperlinks::plain_hyperlink_lines; use crate::terminal_hyperlinks::remap_wrapped_line; +use crate::width::physical_row_count; use crate::wrapping::RtOptions; use crate::wrapping::adaptive_wrap_line; use crate::wrapping::line_contains_url_like; @@ -260,7 +261,7 @@ pub(crate) fn wrap_history_hyperlink_lines( }; wrapped_rows += line_wrapped .iter() - .map(|wrapped_line| wrapped_line.width().max(/*other*/ 1).div_ceil(wrap_width)) + .map(|wrapped_line| physical_row_count(&wrapped_line.line, wrap_width)) .sum::(); wrapped.extend(line_wrapped); } @@ -297,7 +298,7 @@ fn write_history_line( line: &HyperlinkLine, wrap_width: usize, ) -> io::Result<()> { - let physical_rows = line.width().max(1).div_ceil(wrap_width) as u16; + let physical_rows = physical_row_count(&line.line, wrap_width) as u16; if physical_rows > 1 { queue!(writer, SavePosition)?; for _ in 1..physical_rows { @@ -1282,37 +1283,34 @@ mod tests { } #[test] - fn vt100_long_unwrapped_url_does_not_insert_extra_blank_gap_before_content() { - let width: u16 = 56; - let height: u16 = 24; + fn vt100_long_unwrapped_url_preserves_adjacent_history() { + let width: u16 = 20; + let height: u16 = 8; let backend = VT100Backend::new(width, height); let mut term = crate::custom_terminal::Terminal::with_options(backend).expect("terminal"); - let viewport = Rect::new(0, height - 1, width, 1); - term.set_viewport_area(viewport); + term.set_viewport_area(Rect::new(0, 0, width, 1)); let prompt = "Write a long URL as output for testing"; insert_history_lines(&mut term, vec![Line::from(prompt)]).expect("insert prompt line"); - let long_url = format!( - "https://example.test/api/v1/projects/alpha-team/releases/2026-02-17/builds/1234567890/{}", - "very-long-segment-".repeat(16), - ); + // The first 界 cannot fit in the last column, so the final x needs a third row. + let long_url = "https://test.org/界界界界界界界界界界x"; let url_line: Line<'static> = Line::from(vec!["• ".into(), long_url.into()]); insert_history_lines(&mut term, vec![url_line]).expect("insert long url line"); - let rows: Vec = term.backend().vt100().screen().rows(0, width).collect(); - let prompt_row = rows - .iter() - .position(|row| row.contains("Write a long URL as output for testing")) - .unwrap_or_else(|| panic!("expected prompt row in screen rows: {rows:?}")); - let url_row = rows - .iter() - .position(|row| row.contains("• https://example.test/api")) - .unwrap_or_else(|| panic!("expected URL first row in screen rows: {rows:?}")); - - assert!( - url_row <= prompt_row + 2, - "expected URL content to appear immediately after prompt (allowing at most one spacer row), got prompt_row={prompt_row}, url_row={url_row}, rows={rows:?}", - ); + let rows: Vec = term + .backend() + .vt100() + .screen() + .rows(0, width) + .take(usize::from(term.viewport_area.y)) + .collect(); + insta::assert_snapshot!(rows.join("\n"), @r" + Write a long URL as + output for testing + • https://test.org/ + 界界界界界界界界界界 + x + "); } } diff --git a/codex-rs/tui/src/width.rs b/codex-rs/tui/src/width.rs index ba46928faa..d31b82b945 100644 --- a/codex-rs/tui/src/width.rs +++ b/codex-rs/tui/src/width.rs @@ -12,6 +12,9 @@ //! fallback" rather than attempting wrapped rendering at zero width, which //! would produce empty or unstable output. +use ratatui::buffer::CellWidth; +use ratatui::text::Line; +use unicode_width::UnicodeWidthChar; use unicode_width::UnicodeWidthStr; /// Returns the display width Ratatui uses for terminal text without its `u16` limit. @@ -23,6 +26,31 @@ pub(crate) fn display_width(text: &str) -> usize { .count() } +/// Counts rows occupied by a line soft-wrapped at a nonzero terminal width. +/// A wide character can leave the last column unused when the terminal wraps. +pub(crate) fn physical_row_count(line: &Line<'_>, wrap_width: usize) -> usize { + let mut cells = 0; + for grapheme in line.styled_graphemes(line.style) { + let width = usize::from(grapheme.symbol.cell_width()); + // A cluster such as ガ can straddle the edge. Count a gap only when + // its first character is itself wide, retaining Ratatui's cluster width. + if width > 1 + && wrap_width > 1 + && cells % wrap_width == wrap_width - 1 + && grapheme + .symbol + .chars() + .next() + .and_then(UnicodeWidthChar::width) + == Some(2) + { + cells += 1; + } + cells += width; + } + cells.max(1).div_ceil(wrap_width) +} + /// Returns usable content width after reserving fixed columns. /// /// Guarantees a strict positive width (`Some(n)` where `n > 0`) or `None` when @@ -50,7 +78,6 @@ mod tests { use super::*; use crate::line_truncation::line_width; use pretty_assertions::assert_eq; - use ratatui::text::Line; #[test] fn display_width_matches_ratatui_halfwidth_sound_marks_without_overflow() { @@ -63,6 +90,14 @@ mod tests { assert_eq!(line_width(&Line::from(text)), 65_536); } + #[test] + fn narrow_characters_in_a_cluster_can_straddle_the_right_edge() { + for cluster in ["ガ", "क्ष"] { + let line = Line::from(format!("https://example.co/{cluster}1234567890123456789")); + assert_eq!(physical_row_count(&line, /*wrap_width*/ 20), 2); + } + } + #[test] fn usable_content_width_returns_none_when_reserved_exhausts_width() { assert_eq!(