Skip to content

Commit

Permalink
fix(masked input): fix selection with shift + arrow keys
Browse files Browse the repository at this point in the history
  • Loading branch information
TomJGooding committed Feb 14, 2025
1 parent 8ad251a commit c66e80e
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/textual/widgets/_masked_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from textual import events
from textual.strip import Strip
from textual.widgets.input import Selection

if TYPE_CHECKING:
pass
Expand Down Expand Up @@ -256,15 +257,17 @@ def insert_text_at_cursor(self, text: str) -> tuple[str, int] | None:
value, cursor_position = self.insert_separators(value, cursor_position)
return value, cursor_position

def move_cursor(self, delta: int) -> None:
def move_cursor(self, delta: int, select: bool = False) -> None:
"""Moves the cursor position by `delta` characters, skipping separators if
running over them.
Args:
delta: The number of characters to move; positive moves right, negative
moves left.
select: If `True`, select the text between the old and new cursor positions.
"""
cursor_position = self.input.cursor_position
start, end = self.input.selection
if delta < 0 and all(
[
(_CharFlags.SEPARATOR in char_definition.flags)
Expand All @@ -279,7 +282,11 @@ def move_cursor(self, delta: int) -> None:
and (_CharFlags.SEPARATOR in self.template[cursor_position].flags)
):
cursor_position += delta
self.input.cursor_position = cursor_position

if select:
self.input.selection = Selection(start, cursor_position)
else:
self.input.cursor_position = cursor_position

def delete_at_position(self, position: int | None = None) -> None:
"""Deletes character at `position`.
Expand Down Expand Up @@ -623,16 +630,28 @@ def clear(self) -> None:
self.value, self.cursor_position = self._template.insert_separators("", 0)

def action_cursor_left(self, select: bool = False) -> None:
"""Move the cursor one position to the left; separators are skipped."""
self._template.move_cursor(-1)
"""Move the cursor one position to the left; separators are skipped.
Args:
select: If `True`, select the text to the left of the cursor.
"""
self._template.move_cursor(-1, select=select)

def action_cursor_right(self, select: bool = False) -> None:
"""Move the cursor one position to the right; separators are skipped."""
self._template.move_cursor(1)
"""Move the cursor one position to the right; separators are skipped.
Args:
select: If `True`, select the text to the right of the cursor.
"""
self._template.move_cursor(1, select=select)

def action_home(self, select: bool = False) -> None:
"""Move the cursor to the start of the input."""
self._template.move_cursor(-len(self.template))
"""Move the cursor to the start of the input.
Args:
select: If `True`, select the text between the old and new cursor positions.
"""
self._template.move_cursor(-len(self.template), select=select)

def action_cursor_left_word(self, select: bool = False) -> None:
"""Move the cursor left next to the previous separator. If no previous
Expand Down

0 comments on commit c66e80e

Please sign in to comment.