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 authored and copyberry committed 2026-07-29 14:01:31 +00:00
1 parent cef3910ea4
commit 3725f02cf3
38 files changed
+1335 -245

No files matched your search

+27 -11
View File
@@ -1,5 +1,5 @@
use unicode_width::UnicodeWidthChar;
use unicode_width::UnicodeWidthStr;
use crate::width::display_width;
use unicode_segmentation::UnicodeSegmentation;
/// A single visual row produced by RowBuilder.
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -11,7 +11,7 @@ pub struct Row {
impl Row {
pub fn width(&self) -> usize {
self.text.width()
display_width(&self.text)
}
}
@@ -140,9 +140,9 @@ impl RowBuilder {
let (prefix, suffix, taken) =
take_prefix_by_width(&self.current_line, self.target_width);
if taken == 0 {
// Avoid infinite loop on pathological inputs; take one scalar and continue.
if let Some((i, ch)) = self.current_line.char_indices().next() {
let len = i + ch.len_utf8();
// Avoid an infinite loop on an indivisible grapheme wider than the target.
if let Some(grapheme) = self.current_line.graphemes(/*is_extended*/ true).next() {
let len = grapheme.len();
let p = self.current_line[..len].to_string();
self.rows.push(Row {
text: p,
@@ -176,13 +176,13 @@ pub fn take_prefix_by_width(text: &str, max_cols: usize) -> (String, &str, usize
}
let mut cols = 0usize;
let mut end_idx = 0usize;
for (i, ch) in text.char_indices() {
let ch_width = UnicodeWidthChar::width(ch).unwrap_or(0);
if cols.saturating_add(ch_width) > max_cols {
for (i, grapheme) in text.grapheme_indices(/*is_extended*/ true) {
let grapheme_width = display_width(grapheme);
if cols.saturating_add(grapheme_width) > max_cols {
break;
}
cols += ch_width;
end_idx = i + ch.len_utf8();
cols += grapheme_width;
end_idx = i + grapheme.len();
if cols == max_cols {
break;
}
@@ -235,6 +235,22 @@ mod tests {
);
}
#[test]
fn halfwidth_sound_marks_stay_with_their_grapheme_when_wrapping() {
assert_eq!(
take_prefix_by_width("abガc", /*max_cols*/ 3),
("ab".to_string(), "ガc", 2)
);
assert_eq!(
take_prefix_by_width("abガc", /*max_cols*/ 4),
("abガ".to_string(), "c", 4)
);
let mut row_builder = RowBuilder::new(/*target_width*/ 1);
row_builder.push_fragment("ガx");
assert_eq!(row_builder.rows()[0].text, "ガ");
}
#[test]
fn fragmentation_invariance_long_token() {
let s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; // 26 chars