Skip to content

Commit

Permalink
Fix issue when crop start == cell length of a Strip (#3998)
Browse files Browse the repository at this point in the history
* Fix issue when crop start was == cell length of a Strip

* Update changelog

* Update CHANGELOG.md

Co-authored-by: Rodrigo Girão Serrão <[email protected]>

---------

Co-authored-by: Rodrigo Girão Serrão <[email protected]>
  • Loading branch information
darrenburns and rodrigogiraoserrao authored Jan 10, 2024
1 parent 4da008d commit e646916
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 35 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Parameter `animate` from `DataTable.move_cursor` was being ignored https://github.com/Textualize/textual/issues/3840
- Fixed a crash if `DirectoryTree.show_root` was set before the DOM was fully available https://github.com/Textualize/textual/issues/2363
- Live reloading of TCSS wouldn't apply CSS changes to screens under the top screen of the stack https://github.com/Textualize/textual/issues/3931
- Fix issue with `Strip.crop` when crop window start aligned with strip end https://github.com/Textualize/textual/pull/3998


## [0.47.1] - 2023-01-05
Expand Down
2 changes: 1 addition & 1 deletion src/textual/strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ def crop(self, start: int, end: int | None = None) -> Strip:
add_segment = output_segments.append
iter_segments = iter(self._segments)
segment: Segment | None = None
if start > self.cell_length:
if start >= self.cell_length:
strip = Strip([], 0)
else:
for segment in iter_segments:
Expand Down
76 changes: 42 additions & 34 deletions tests/test_strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,22 @@ def test_eq():


def test_adjust_cell_length():
for repeat in range(3):
assert Strip([]).adjust_cell_length(3) == Strip([Segment(" ")])
assert Strip([Segment("f")]).adjust_cell_length(3) == Strip(
[Segment("f"), Segment(" ")]
)
assert Strip([Segment("💩")]).adjust_cell_length(3) == Strip(
[Segment("💩"), Segment(" ")]
)

assert Strip([Segment("💩💩")]).adjust_cell_length(3) == Strip([Segment("💩 ")])
assert Strip([Segment("💩💩")]).adjust_cell_length(4) == Strip([Segment("💩💩")])
assert Strip([Segment("💩"), Segment("💩💩")]).adjust_cell_length(2) == Strip(
[Segment("💩")]
)
assert Strip([Segment("💩"), Segment("💩💩")]).adjust_cell_length(4) == Strip(
[Segment("💩"), Segment("💩")]
)
assert Strip([]).adjust_cell_length(3) == Strip([Segment(" ")])
assert Strip([Segment("f")]).adjust_cell_length(3) == Strip(
[Segment("f"), Segment(" ")]
)
assert Strip([Segment("💩")]).adjust_cell_length(3) == Strip(
[Segment("💩"), Segment(" ")]
)

assert Strip([Segment("💩💩")]).adjust_cell_length(3) == Strip([Segment("💩 ")])
assert Strip([Segment("💩💩")]).adjust_cell_length(4) == Strip([Segment("💩💩")])
assert Strip([Segment("💩"), Segment("💩💩")]).adjust_cell_length(2) == Strip(
[Segment("💩")]
)
assert Strip([Segment("💩"), Segment("💩💩")]).adjust_cell_length(4) == Strip(
[Segment("💩"), Segment("💩")]
)


def test_extend_cell_length():
Expand All @@ -101,8 +100,6 @@ def test_simplify():
def test_apply_filter():
strip = Strip([Segment("foo", Style.parse("red"))])
expected = Strip([Segment("foo", Style.parse("#1b1b1b"))])
print(repr(strip))
print(repr(expected))
assert strip.apply_filter(Monochrome(), Color(0, 0, 0)) == expected


Expand All @@ -128,26 +125,37 @@ def test_style_links():


def test_crop():
for repeat in range(3):
assert Strip([Segment("foo")]).crop(0, 3) == Strip([Segment("foo")])
assert Strip([Segment("foo")]).crop(0, 2) == Strip([Segment("fo")])
assert Strip([Segment("foo")]).crop(0, 1) == Strip([Segment("f")])
assert Strip([Segment("foo")]).crop(0, 3) == Strip([Segment("foo")])
assert Strip([Segment("foo")]).crop(0, 2) == Strip([Segment("fo")])
assert Strip([Segment("foo")]).crop(0, 1) == Strip([Segment("f")])

assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([Segment("")])
assert Strip([Segment("foo")]).crop(1, 3) == Strip([Segment("oo")])
assert Strip([Segment("foo")]).crop(1, 2) == Strip([Segment("o")])
assert Strip([Segment("foo")]).crop(1, 1) == Strip([Segment("")])

assert Strip([Segment("foo💩"), Segment("b💩ar"), Segment("ba💩z")]).crop(
1, 6
) == Strip([Segment("oo💩"), Segment("b")])
assert Strip([Segment("foo💩"), Segment("b💩ar"), Segment("ba💩z")]).crop(
1, 6
) == Strip([Segment("oo💩"), Segment("b")])


@pytest.mark.parametrize(
"text,crop,output",
[
["foo", (0, 5), [Segment("foo")]],
["foo", (2, 5), [Segment("o")]],
["foo", (3, 5), []],
["foo", (4, 6), []],
],
)
def test_crop_out_of_bounds(text, crop, output):
assert Strip([Segment(text)]).crop(*crop) == Strip(output)


def test_divide():
for repeat in range(3):
assert Strip([Segment("foo")]).divide([1, 2]) == [
Strip([Segment("f")]),
Strip([Segment("o")]),
]
assert Strip([Segment("foo")]).divide([1, 2]) == [
Strip([Segment("f")]),
Strip([Segment("o")]),
]


@pytest.mark.parametrize(
Expand Down

0 comments on commit e646916

Please sign in to comment.