fix split cells

This commit is contained in:
Will McGugan 2024-09-28 20:19:10 +01:00
parent 4101991898
commit a8c3b8700b
3 changed files with 21 additions and 2 deletions

View file

@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Rich will display tracebacks with finely grained error locations on python 3.11+ https://github.com/Textualize/rich/pull/3486
### Fixed
- Fixed issue with Segment._split_cells
## [13.8.1] - 2024-09-10
### Fixed

View file

@ -109,16 +109,22 @@ class Segment(NamedTuple):
@classmethod
@lru_cache(1024 * 16)
def _split_cells(cls, segment: "Segment", cut: int) -> Tuple["Segment", "Segment"]:
"""Split a segment in to two at a given cell position.
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)
cell_size = get_character_cell_size
pos = int((cut / cell_length) * (len(text) - 1))
pos = int((cut / cell_length) * (len(text) - 1)) - 1
if pos < 0:
pos = 0
before = text[:pos]
cell_pos = cell_len(before)

View file

@ -2,6 +2,7 @@ from io import StringIO
import pytest
from rich.cells import cell_len
from rich.segment import ControlType, Segment, SegmentLines, Segments
from rich.style import Style
@ -284,6 +285,13 @@ def test_split_cells_emoji(text, split, result):
assert Segment(text).split_cells(split) == result
def test_split_cells_mixed() -> None:
test = Segment("早乙女リリエル (CV: 徳井青)")
for position in range(1, test.cell_length):
left, right = Segment.split_cells(test, position)
assert cell_len(left.text) == position
def test_segment_lines_renderable():
lines = [[Segment("hello"), Segment(" "), Segment("world")], [Segment("foo")]]
segment_lines = SegmentLines(lines)