Skip to content

chop_cells() emits a spurious empty first line when the first character is wider than max_cells #4202

Description

@nyxst4ck

Description

rich.cells.chop_cells() emits a spurious empty first line when the very first character of the text is wider than max_cells.

Reproduction

from rich.cells import chop_cells

chop_cells("文", 1)
# actual:   ['', '文']
# expected: ['文']

Reproduced on rich 15.0.0 (and current master, rich/cells.py).

The root cause: the loop flushes the current (still empty) line whenever the next character does not fit. When the first character is a double-width CJK glyph and max_cells is 1, the buffer is flushed while empty, producing a leading "" entry. Any consumer that renders line-by-line (e.g. text wrapping into width-1 or narrow columns containing CJK text) shows a blank line before the content.

Non-degenerate case for contrast — this one is already correct:

chop_cells("ab文cd", 2)  # ['ab', '文', 'cd'] ✅

Proposed solution

In the flush branch, only append the buffered line if it is non-empty (one-line guard). A character that can never fit still gets emitted on its own line, matching the behavior of the "ab文cd" case above. I have this fix ready with two regression tests (red on current master, green with the fix; full test suite passes, black/mypy clean) and can open a PR if the approach is approved, per AI_POLICY.md.

Platform

Not platform-specific (pure Python, reproduced on Windows 11 / Python 3.11 / rich 15.0.0 and master).


Disclosure per the spirit of AI_POLICY.md: this bug was found and the fix prepared with the assistance of an AI agent (Claude Code), operated and reviewed by me. If the solution is approved by a maintainer, the follow-up PR will identify itself as AI-generated as the policy requires.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions