diff --git a/rich/segment.py b/rich/segment.py index 34f84b9dc..0f0676876 100644 --- a/rich/segment.py +++ b/rich/segment.py @@ -142,12 +142,12 @@ def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment ) if out_by == -1 and cell_size(text[pos]) == 2: return ( - _Segment(before[:pos] + " ", style, control), + _Segment(text[:pos] + " ", style, control), _Segment(" " + text[pos + 1 :], style, control), ) - if out_by == +1 and cell_size(text[pos]) == 2: + if out_by == +1 and cell_size(text[pos - 1]) == 2: return ( - _Segment(before[: pos - 1] + " ", style, control), + _Segment(text[: pos - 1] + " ", style, control), _Segment(" " + text[pos:], style, control), ) if cell_pos < cut: diff --git a/tests/test_segment.py b/tests/test_segment.py index 4bdb01453..3673f129b 100644 --- a/tests/test_segment.py +++ b/tests/test_segment.py @@ -285,19 +285,27 @@ def test_split_cells_emoji(text, split, result): assert Segment(text).split_cells(split) == result -def test_split_cells_mixed() -> None: - """Check that split cells splits on cell positions.""" - # Caused https://github.com/Textualize/textual/issues/4996 in Textual - tests = [ +@pytest.mark.parametrize( + "segment", + [ Segment("早乙女リリエル (CV: 徳井青)"), - Segment("メイド・イン・きゅんクチュアリ☆"), + Segment("メイド・イン・きゅんクチュアリ☆ "), Segment("TVアニメ「メルクストーリア -無気力少年と瓶の中の少女-」 主題歌CD"), - ] - for test in tests: - for position in range(1, test.cell_length): - left, right = Segment.split_cells(test, position) - assert cell_len(left.text) == position - assert cell_len(right.text) == test.cell_length - position + Segment("南無阿弥JKうらめしや?! "), + Segment("メルク (CV: 水瀬いのり) "), + ], +) +def test_split_cells_mixed(segment: Segment) -> None: + """Check that split cells splits on cell positions.""" + # Caused https://github.com/Textualize/textual/issues/4996 in Textual + + for position in range(0, segment.cell_length + 1): + left, right = Segment.split_cells(segment, position) + assert all( + cell_len(c) > 0 for c in segment.text + ) # Sanity check there aren't any sneaky control codes + assert cell_len(left.text) == position + assert cell_len(right.text) == segment.cell_length - position def test_split_cells_doubles() -> None: