Skip to content

Commit c3e72ed

Browse files
committed
text selection test
1 parent 8bc1c65 commit c3e72ed

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1010
### Fixed
1111

1212
- Fixed type hint aliasing for App under TYPE_CHECKING https://github.com/Textualize/textual/pull/6152
13+
- Fixed for text selection with double width characters https://github.com/Textualize/textual/pull/6186
1314

1415
### Changed
1516

src/textual/screen.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1738,7 +1738,6 @@ def _watch__select_end(
17381738
Args:
17391739
select_end: The end selection.
17401740
"""
1741-
17421741
if select_end is None or self._select_start is None:
17431742
# Nothing to select
17441743
return

src/textual/selection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ def extract(self, text: str) -> str:
5656
return lines[start_line][start_offset:end_offset]
5757

5858
selection: list[str] = []
59-
selected_lines = lines[start_line:end_line]
59+
selected_lines = lines[start_line : end_line + 1]
6060
if len(selected_lines) >= 2:
6161
first_line, *mid_lines, last_line = selected_lines
6262
selection.append(first_line[start_offset:])
6363
selection.extend(mid_lines)
64-
selection.append(last_line[: end_offset + 1])
64+
selection.append(last_line[:end_offset])
6565
else:
6666
return lines[start_line][start_offset:end_offset]
6767
return "\n".join(selection)

tests/test_selection.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import pytest
22

3+
from textual.app import App, ComposeResult
34
from textual.geometry import Offset
45
from textual.selection import Selection
6+
from textual.widgets import Static
57

68

79
@pytest.mark.parametrize(
@@ -20,3 +22,24 @@
2022
def test_extract(text: str, selection: Selection, expected: str) -> None:
2123
"""Test Selection.extract"""
2224
assert selection.extract(text) == expected
25+
26+
27+
async def test_double_width():
28+
"""Test that selection works with double width characters."""
29+
30+
TEXT = """😂❤️👍Select😊🙏😍\nme🔥💯😭😂❤️👍"""
31+
32+
class TextSelectApp(App):
33+
def compose(self) -> ComposeResult:
34+
yield Static(TEXT)
35+
36+
app = TextSelectApp()
37+
async with app.run_test() as pilot:
38+
await pilot.pause()
39+
assert await pilot.mouse_down(offset=(2, 0))
40+
await pilot.pause()
41+
assert await pilot.mouse_up(offset=(7, 1))
42+
selected_text = app.screen.get_selected_text()
43+
expected = "❤️👍Select😊🙏😍\nme🔥💯😭"
44+
45+
assert selected_text == expected

0 commit comments

Comments
 (0)