Fix TUI layout for halfwidth Japanese sound marks (#35962)

## What changed

- Use Ratatui-compatible terminal widths throughout TUI layout, preserving
  full-width counters for long lines.
- Wrap and truncate on grapheme boundaries so halfwidth kana sound marks and
  emoji sequences stay intact.
- Apply the corrected sizing to text input, selection popups, status and
  history cells, Markdown, hyperlinks, and scrollable cloud diffs.

## Testing

- Add focused unit and snapshot coverage for wrapping, truncation, cursor and
  mask alignment, popup columns, status output, session headers, Markdown
  tables, process output, and scrollable diffs.

GitOrigin-RevId: 294ec0bcff743c6bb16d55ec26308524ce741879
This commit is contained in:
Charlie Marsh
2026-07-29 14:01:31 +00:00
committed by copyberry
parent cef3910ea4
commit 3725f02cf3
38 changed files with 1335 additions and 245 deletions
@@ -7,8 +7,7 @@ use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;
use std::borrow::Cow;
use std::time::Instant;
use unicode_width::UnicodeWidthChar;
use unicode_width::UnicodeWidthStr;
use unicode_segmentation::UnicodeSegmentation;
use crate::bottom_pane::popup_consts::standard_popup_hint_line;
use crate::bottom_pane::scroll_state::ScrollState;
@@ -18,7 +17,9 @@ use crate::bottom_pane::selection_popup_common::menu_surface_padding_height;
use crate::bottom_pane::selection_popup_common::render_menu_surface;
use crate::bottom_pane::selection_popup_common::render_rows;
use crate::bottom_pane::selection_popup_common::wrap_styled_line;
use crate::line_truncation::line_width;
use crate::render::renderable::Renderable;
use crate::width::display_width;
use super::DESIRED_SPACERS_BETWEEN_SECTIONS;
use super::RequestUserInputOverlay;
@@ -421,12 +422,6 @@ impl RequestUserInputOverlay {
}
}
fn line_width(line: &Line<'_>) -> usize {
line.iter()
.map(|span| UnicodeWidthStr::width(span.content.as_ref()))
.sum()
}
/// Render rows into `area`, bottom-aligning the visible rows when fewer than
/// `area.height` lines are produced.
///
@@ -489,7 +484,7 @@ fn truncate_line_word_boundary_with_ellipsis(
}
let ellipsis = "…";
let ellipsis_width = UnicodeWidthStr::width(ellipsis);
let ellipsis_width = display_width(ellipsis);
if ellipsis_width >= max_width {
return Line::from(ellipsis);
}
@@ -509,19 +504,19 @@ fn truncate_line_word_boundary_with_ellipsis(
'outer: for (span_idx, span) in line.spans.iter().enumerate() {
let text = span.content.as_ref();
for (byte_idx, ch) in text.char_indices() {
let ch_width = UnicodeWidthChar::width(ch).unwrap_or(0);
if used.saturating_add(ch_width) > limit {
for (byte_idx, grapheme) in text.grapheme_indices(/*is_extended*/ true) {
let grapheme_width = display_width(grapheme);
if used.saturating_add(grapheme_width) > limit {
overflowed = true;
break 'outer;
}
used = used.saturating_add(ch_width);
used = used.saturating_add(grapheme_width);
let bp = BreakPoint {
span_idx,
byte_end: byte_idx + ch.len_utf8(),
byte_end: byte_idx + grapheme.len(),
};
last_fit = Some(bp);
if ch.is_whitespace() {
if grapheme.chars().all(char::is_whitespace) {
last_word_break = Some(bp);
}
}
@@ -576,3 +571,7 @@ fn truncate_line_word_boundary_with_ellipsis(
Line::from(spans_out).style(line_style)
}
#[cfg(test)]
#[path = "render_tests.rs"]
mod tests;
@@ -0,0 +1,37 @@
use super::truncate_line_word_boundary_with_ellipsis;
use pretty_assertions::assert_eq;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::style::Stylize;
use ratatui::text::Line;
use ratatui::text::Span;
use ratatui::widgets::Paragraph;
use ratatui::widgets::Widget;
#[test]
fn halfwidth_sound_marks_are_truncated_at_a_grapheme_boundary() {
let line = Line::from("abガ tail");
assert_eq!(
truncate_line_word_boundary_with_ellipsis(line, /*max_width*/ 4),
Line::from(vec![Span::raw("ab"), Span::raw("…")])
);
}
#[test]
fn halfwidth_sound_marks_are_truncated_and_rendered_at_a_grapheme_boundary() {
let lines = [
Line::from(vec!["ab".bold(), "ガ".cyan(), " tail".dim()]),
Line::from(vec!["xy".bold(), "パ".magenta(), " tail".dim()]),
]
.map(|line| truncate_line_word_boundary_with_ellipsis(line, /*max_width*/ 5));
let area = Rect::new(0, 0, 5, 2);
let mut buf = Buffer::empty(area);
Paragraph::new(lines.to_vec()).render(area, &mut buf);
insta::assert_snapshot!(
"request_user_input_halfwidth_sound_marks_truncation",
format!("{buf:?}\n{lines:#?}")
);
}
@@ -0,0 +1,33 @@
---
source: tui/src/bottom_pane/request_user_input/render_tests.rs
expression: "format!(\"{buf:?}\\n{lines:#?}\")"
---
Buffer {
area: Rect { x: 0, y: 0, width: 5, height: 2 },
content: [
"abガ…", // hidden by multi-width symbols: [(3, " ")]
"xyパ…", // hidden by multi-width symbols: [(3, " ")]
],
styles: [
x: 0, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: BOLD,
x: 2, y: 0, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE,
x: 3, y: 0, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 4, y: 0, fg: Cyan, bg: Reset, underline: Reset, modifier: NONE,
x: 0, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: BOLD,
x: 2, y: 1, fg: Magenta, bg: Reset, underline: Reset, modifier: NONE,
x: 3, y: 1, fg: Reset, bg: Reset, underline: Reset, modifier: NONE,
x: 4, y: 1, fg: Magenta, bg: Reset, underline: Reset, modifier: NONE,
]
}
[
Line::from_iter([
Span::from("ab").bold(),
Span::from("カ\u{ff9e}").cyan(),
Span::from("…").cyan(),
]),
Line::from_iter([
Span::from("xy").bold(),
Span::from("ハ\u{ff9f}").magenta(),
Span::from("…").magenta(),
]),
]