Skip to content

Commit 9537a9d

Browse files
author
Jonathan Plasse
committed
Clean up the code
1 parent 346ec6f commit 9537a9d

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

src/textual/widgets/_masked_input.py

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ def insert_separators(self, value: str, cursor_position: int) -> tuple[str, int]
200200
cursor_position += 1
201201
return value, cursor_position
202202

203-
def insert_text_at_cursor(self, text: str, value: str, cursor_position: int) -> tuple[str, int] | None:
203+
def insert_text_at_cursor(self, text: str) -> tuple[str, int] | None:
204204
"""Inserts `text` at current cursor position. If not present in `text`, any expected separator is automatically
205205
inserted at the correct position.
206206
@@ -211,6 +211,8 @@ def insert_text_at_cursor(self, text: str, value: str, cursor_position: int) ->
211211
A tuple in the form `(value, cursor_position)` with the new control value and current cursor position if
212212
`text` matches the template, None otherwise.
213213
"""
214+
value = self.input.value
215+
cursor_position = self.input.cursor_position
214216
separators = set(
215217
[
216218
char_definition.char
@@ -602,7 +604,7 @@ def insert_text_at_cursor(self, text: str) -> None:
602604
text: New text to insert.
603605
"""
604606

605-
new_value = self._template.insert_text_at_cursor(text, self.value, self.cursor_position)
607+
new_value = self._template.insert_text_at_cursor(text)
606608
if new_value is not None:
607609
self.value, self.cursor_position = new_value
608610
else:
@@ -617,33 +619,24 @@ def replace(self, text: str, start: int, end: int):
617619
end: End index to replace (inclusive).
618620
"""
619621

620-
previous_cursor_position = self.cursor_position
621-
value = self.value
622-
cursor_position = start
622+
self.cursor_position = start
623623
for char in text:
624-
new_value_cursor_position = self._template.insert_text_at_cursor(char, value, cursor_position)
624+
if self.cursor_position >= end:
625+
return
626+
new_value_cursor_position = self._template.insert_text_at_cursor(char)
625627
if new_value_cursor_position is None:
626-
self.value = value
627-
self.cursor_position = previous_cursor_position
628628
self.restricted()
629629
return
630630

631-
new_value, new_cursor_position = new_value_cursor_position
632-
if new_cursor_position >= end:
633-
self.value = new_value[:end] + value[end:]
634-
self.cursor_position = new_cursor_position
635-
return
631+
self.value, self.cursor_position = new_value_cursor_position
636632

637-
value = new_value
638-
cursor_position = new_cursor_position
633+
last_cursor_position = self.cursor_position
639634

640-
self.value = value
641-
self.cursor_position = end
642-
while self.cursor_position > cursor_position:
643-
self._template.move_cursor(-1)
635+
while self.cursor_position < end:
644636
self._template.delete_at_position()
637+
self._template.move_cursor(1)
645638

646-
self.cursor_position = cursor_position
639+
self.cursor_position = last_cursor_position
647640

648641
def clear(self) -> None:
649642
"""Clear the masked input."""

0 commit comments

Comments
 (0)