[BUG] Segment.split_cells doesn't handle zero width charachters correctly

Aperta
#4,264 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
1-2 giorni
Idoneità per principianti
42/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
python
Ambito
cli

Direzione di ricerca

Start with Segment.split_cells and the cell-width helpers in rich/segment.py, then read split_text in rich/cells.py to see how it cuts text by cell position. Write a test like the one in the issue, using repr checks on the two halves of a Hebrew string with combining marks, and run it to see the current split. Done means the marks stay attached to their base characters and the existing cell-splitting tests still pass; the proposed split_text swap needs checking against them.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Needs triage

Describe the bug

Segment.split_cells() mishandles zero-width characters (such as Hebrew diacritical marks) when splitting text across cell boundaries, resulting in malformed segments where zero-width combining marks are incorrectly detached or split from their base characters.

Minimal reproduction / Unit Test

def test_split_cells_zero_width():
    hebrew_segment = Segment("שָׁלוֹם")
    left, right = hebrew_segment.split_cells(3)

    assert repr(left) == "Segment('שָׁלוֹ')"
    assert repr(right) == "Segment('ם')"

Proposed Fix
cells.py already has a function called split_text that can be used to solve the problem as such:

    def _split_cells(cls, segment: "Segment", cut: int) -> tuple["Segment", "Segment"]:
        """Split a segment in to two at a given cell position.

        Note that splitting a double-width character, may result in that character turning
        into two spaces.

        Args:
            segment (Segment): A segment to split.
            cut (int): A cell position to cut on.

        Returns:
            A tuple of two segments.
        """
        text, style, control = segment
        _Segment = Segment
        cell_length = segment.cell_length
        if cut >= cell_length:
            return segment, _Segment("", style, control)

        before, after = split_text(text, cut)
        return (
            _Segment(before, style, control),
            _Segment(after, style, control),
        )

I tried to publish it but wasn't able to since the repository limints pull requests t collaborators only.

Lingua principale
Python
Stelle
57.8k
Fork
2.6k
Merge medio
20m
PR unite (30g)
3

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Textualize/rich

Tutte le issue di Textualize/rich

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.