rich icon indicating copy to clipboard operation
rich copied to clipboard

[BUG] Text.align exceeds requested width with a wide padding character

Open agammann opened this issue 1 month ago • 2 comments

  • [x] I've checked docs and closed issues for possible solutions.
  • [x] I can't find my issue in the FAQ.

Describe the bug

Text.align() can produce text much wider than its requested width when character occupies two terminal cells. With width=6, padding Text('x') using '界' gives a Text.cell_len of 11. The single-cell fill control gives 6.

from rich.text import Text

for fill in ('.', '\u754c'):  # U+754C, a two-cell CJK character
    text = Text('x')
    text.align('center', 6, character=fill)
    print(ascii(fill), text.cell_len)

Observed:

'.' 6
'\u754c' 11

Expected: aligning to a width of 6 should not produce 11 cells of text. If a wide fill character cannot satisfy the requested width exactly, the method could handle that case explicitly rather than exceeding the width.

Text.align calculates excess_space in cells but passes that number to pad_left / pad_right, which repeat character by character count. This doubles the added width for a two-cell fill.

Platform

  • Python 3.14.8 on Windows PowerShell
  • Rich 15.0.0, current main commit 9d8f9a372cc5916fd4781fec207ced7ddac2f08f
  • This is an internal Text.cell_len result; it does not depend on terminal rendering.

agammann avatar Nov 01 '26 01:11 agammann

Hi! I reproduced this against current main (9d8f9a3, Rich 15.0.0) on Windows with Python 3.12.1.

I added nine diagnostic regression cases locally: left/center/right alignment at widths 5, 6, and 7, padding Text("x") with U+754C. All nine exceed the requested cell width: the results are 9, 11, and 13 cells respectively. The 113 existing tests in tests/test_text.py still pass when those nine new cases are excluded. No production code has been changed.

Before choosing the behavior, would @willmcgugan prefer cell-aware padding, using spaces for any remaining single cell, or explicitly rejecting padding characters that do not occupy one cell?

My preference would be cell-aware padding within Text.align, preserving the character-count contract of pad_left/pad_right. Tests would also cover style-span offsets and unchanged behavior for ordinary one-cell padding. The diagnostic tests currently assume successful alignment; they would be adjusted if rejection is the preferred contract.

Following AI_POLICY.md, I am asking for approval of the approach before opening a PR. The investigation, diagnostic tests, and this comment were prepared with OpenAI Codex assistance; the reported checks were executed locally.

Hugo510 avatar Nov 03 '26 00:11 Hugo510

Original code

Amandalina670 avatar Nov 07 '26 14:11 Amandalina670