Skip to content

Commit 9ff7cbc

Browse files
committed
Extra Input selection tests
1 parent 8e75f48 commit 9ff7cbc

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1616
- Added new keybinds (hold shift) for text selection in `Input` https://github.com/Textualize/textual/pull/5340
1717
- Added `Input.selection` reactive attribute for reading and updating the current selection https://github.com/Textualize/textual/pull/5340
1818
- Added `Input.select_on_focus` (default `True`) to enable/disable selecting all text in an `Input` on focus https://github.com/Textualize/textual/pull/5340
19+
- Added methods `Input.replace`, `Input.insert`, `Input.delete`, `Input.delete_selection` for editing text https://github.com/Textualize/textual/pull/5340
20+
- Added `Input.selected_text` property for getting the currently selected text https://github.com/Textualize/textual/pull/5340
1921
- `Input` can now be scrolled independently of cursor position (hold shift and scroll with the mouse wheel in supported environments) https://github.com/Textualize/textual/pull/5340
2022

2123
## Changed

src/textual/widgets/_input.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,12 @@ def clear(self) -> None:
743743
"""Clear the input."""
744744
self.value = ""
745745

746+
@property
747+
def selected_text(self) -> str:
748+
"""The text between the start and end points of the current selection."""
749+
start, end = sorted(self.selection)
750+
return self.value[start:end]
751+
746752
def action_cursor_left(self, select: bool = False) -> None:
747753
"""Move the cursor one position to the left.
748754

tests/input/test_input.py

Whitespace-only changes.

tests/input/test_input_properties.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from textual.app import App, ComposeResult
77
from textual.containers import Container
88
from textual.widgets import Input
9+
from textual.widgets.input import Selection
910

1011

1112
class InputApp(App[None]):
@@ -61,3 +62,34 @@ async def test_input_height():
6162
input_widget.styles.height.value == input_widget.parent.styles.height.value
6263
)
6364
assert input_widget.parent.styles.height.value == 1
65+
66+
67+
async def test_input_selected_text():
68+
async with InputApp().run_test() as pilot:
69+
input_widget = pilot.app.query_one(Input)
70+
input_widget.value = "Hello, world!"
71+
input_widget.selection = Selection(0, 4)
72+
assert input_widget.selected_text == "Hell"
73+
74+
# Reverse selection
75+
input_widget.selection = Selection(4, 0)
76+
assert input_widget.selected_text == "Hell"
77+
78+
# Empty selection
79+
input_widget.selection = Selection(4, 4)
80+
assert input_widget.selected_text == ""
81+
82+
83+
async def test_input_selection_deleted_programmatically():
84+
async with InputApp().run_test() as pilot:
85+
input_widget = pilot.app.query_one(Input)
86+
input_widget.value = "Hello, world!"
87+
input_widget.selection = Selection(0, 4)
88+
input_widget.delete_selection()
89+
assert input_widget.value == "o, world!"
90+
91+
# Reverse selection
92+
input_widget.value = "Hello, world!"
93+
input_widget.selection = Selection(4, 0)
94+
input_widget.delete_selection()
95+
assert input_widget.value == "o, world!"

0 commit comments

Comments
 (0)