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

Đang mở
#4,264 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
42/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
cli

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
57.8k
Fork
2.6k
Merge trung bình
20 phút
Pull request đã merge (30 ngày)
3

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Textualize/rich

Tất cả issue của Textualize/rich

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.