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

Abierto
#4,264 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
42/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
python
Área
cli

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Python
Estrellas
57.8k
Forks
2.6k
Merge medio
20 min
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Textualize/rich

Todos los issues de Textualize/rich

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.