Skip to content

Fix Segment._split_cells for non-unit-width characters (emoji, CJK)#4118

Open
ShiLiguo123 wants to merge 1 commit into
Textualize:masterfrom
ShiLiguo123:fix-split-cells-emoji
Open

Fix Segment._split_cells for non-unit-width characters (emoji, CJK)#4118
ShiLiguo123 wants to merge 1 commit into
Textualize:masterfrom
ShiLiguo123:fix-split-cells-emoji

Conversation

@ShiLiguo123
Copy link
Copy Markdown

Summary

Fix Segment._split_cells() to correctly handle strings containing double-cell-width characters (emoji, CJK characters, etc.).

Root Cause

The old algorithm used a heuristic initial position: pos = int((cut / cell_length) * len(text)). This assumes uniform character width, which fails badly for mixed-width strings.

For example:

s = Segment('🦊🦊🦊\n\n\n\n\n\n')
Segment._split_cells(s, 3)
# Wrong: (Segment('🦊🦊🦊\n '), Segment(' \n\n\n\n'))

Fix

Replaced the heuristic + while-loop with a character-by-character walk-through. The new algorithm:

  1. Tracks cumulative cell position as it iterates over characters
  2. When it finds the character that exceeds the cut point, it splits there
  3. Double-width characters are replaced with two spaces as before

Fixes #3299

The old algorithm used a heuristic initial position estimate
int((cut / cell_length) * len(text)) and then adjusted in a
while loop. This fails for strings with mixed single/double
cell-width characters (emoji, CJK).

Replace with a simple character-by-character walk-through
that correctly tracks cumulative cell positions.

Fixes Textualize#3299
@willmcgugan
Copy link
Copy Markdown
Member

Your PR has been closed due to a AI policy violation.

Please read the following before submitting further PRs.

https://github.com/Textualize/textual/blob/main/AI_POLICY.md

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Segment._split_cells doesn't handle non-unit characters well

2 participants