textual icon indicating copy to clipboard operation
textual copied to clipboard

Content.center() returns output wider than the requested width

Open Timotty opened this issue 1 month ago • 3 comments

Have you checked closed issues? Yes. I searched open and closed issues and pull requests for Content.center, centered content width, padding, and the documented "Desired width of output" contract; no matching report was found.

Have you checked against the most recent version of Textual? Yes. PyPI currently lists 8.2.8. This also reproduces with Textual installed editable from current main, commit 5e5b7ef58b8c5572c13a3ff9027d756623661495.

The bug

Content.center(width) returns content wider than the requested width. The method documents width as the "Desired width of output", but its right-padding calculation does not subtract the content width.

Complete minimal example

from textual.content import Content

for text, width in (("ab", 10), ("hello", 11)):
    result = Content(text).center(width)
    print(
        f"{text!r}, width={width}: "
        f"plain={result.plain!r}, cells={result.cell_length}"
    )

Actual output:

'ab', width=10: plain='    ab      ', cells=12
'hello', width=11: plain='   hello        ', cells=16

Expected output:

'ab', width=10: plain='    ab    ', cells=10
'hello', width=11: plain='   hello   ', cells=11

Investigation and proposed direction

In src/textual/content.py, Content.center() currently calculates:

left = (width - content.cell_length) // 2
right = width - left
content = content.pad(left, right)

Since pad() adds both padding values to the content, the final cell length is width + content.cell_length. The neighboring formatted-line center alignment instead divides only the excess space between the two sides.

The focused correction appears to be:

right = width - left - content.cell_length

with regression tests covering even and uneven padding. Could @willmcgugan confirm that this matches the intended Content.center() contract? No PR has been opened pending approval under the project's AI policy.

Environment

  • Windows
  • CPython 3.14.6
  • Textual 8.2.8 (editable from the main commit above)
  • Rich 15.0.0
  • Pure API reproduction; no terminal rendering required

Disclosure: this report and its reproduction were prepared and verified with AI assistance (OpenCode, GPT-5.6 Sol).

Timotty avatar Oct 13 '26 00:10 Timotty

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This project is developed and maintained by Will McGugan. Consider sponsoring Will's work on this project (and others).

This is an automated reply, generated by FAQtory

github-actions[bot] avatar Oct 13 '26 00:10 github-actions[bot]

Thanks for the detailed report and reproduction.

I reproduced on current main and confirmed the root cause: right = width - left does not subtract the content width, so the right padding is too large by content.cell_length.

Proposed fix (one line):

content = self.rstrip().truncate(width, ellipsis=ellipsis)
left = (width - content.cell_length) // 2
right = width - left - content.cell_length
content = content.pad(left, right)
return content

Verified locally:

  • Content('ab').center(10) -> ' ab ' (10 cells)
  • Content('hello').center(11) -> ' hello ' (11 cells)
  • tests/test_content.py: 74 passed (1 pre-existing failure, test_split_and_tabs, unrelated to this change — fails on unmodified main too, appears to be a pytest-asyncio config issue).

This comment and the investigation were prepared with AI assistance. Per the repo's AI policy, I'm not opening a PR until a maintainer approves the approach. Happy to open a PR with the template and regression tests once approved.

@willmcgugan Would this one-line correction be acceptable?

Sudhakar7J avatar Nov 04 '26 05:11 Sudhakar7J

Hi @willmcgugan,

I investigated and implemented a fix for this on my fork: [https://github.com/Satyam-git-dotcom/textual/tree/fix-content-center-width][Satyam-git-dotcom/textual@fix-content-center-width](commit 77db4f5).

Summary of Changes: Bug fix: Corrected right-padding calculation in src/textual/content.py to right = width - content.cell_length - left. Tests: Added regression tests in tests/test_content.py covering even/odd widths, exact width match, truncation with ellipsis, styled spans, and double-width characters. All 84 tests pass. Changelog: Updated CHANGELOG.md under ## Unreleased.

Since PR creation is currently restricted to collaborators, please let me know if you would like me to open a PR or if you'd like to pull the branch directly!

Image

Satyam-git-dotcom avatar Nov 07 '26 14:11 Satyam-git-dotcom