@@ -200,7 +200,7 @@ def insert_separators(self, value: str, cursor_position: int) -> tuple[str, int]
200
200
cursor_position += 1
201
201
return value , cursor_position
202
202
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 :
204
204
"""Inserts `text` at current cursor position. If not present in `text`, any expected separator is automatically
205
205
inserted at the correct position.
206
206
@@ -211,6 +211,8 @@ def insert_text_at_cursor(self, text: str, value: str, cursor_position: int) ->
211
211
A tuple in the form `(value, cursor_position)` with the new control value and current cursor position if
212
212
`text` matches the template, None otherwise.
213
213
"""
214
+ value = self .input .value
215
+ cursor_position = self .input .cursor_position
214
216
separators = set (
215
217
[
216
218
char_definition .char
@@ -610,7 +612,7 @@ def insert_text_at_cursor(self, text: str) -> None:
610
612
text: New text to insert.
611
613
"""
612
614
613
- new_value = self ._template .insert_text_at_cursor (text , self . value , self . cursor_position )
615
+ new_value = self ._template .insert_text_at_cursor (text )
614
616
if new_value is not None :
615
617
self .value , self .cursor_position = new_value
616
618
else :
@@ -625,33 +627,24 @@ def replace(self, text: str, start: int, end: int):
625
627
end: End index to replace (inclusive).
626
628
"""
627
629
628
- previous_cursor_position = self .cursor_position
629
- value = self .value
630
- cursor_position = start
630
+ self .cursor_position = start
631
631
for char in text :
632
- new_value_cursor_position = self ._template .insert_text_at_cursor (char , value , cursor_position )
632
+ if self .cursor_position >= end :
633
+ return
634
+ new_value_cursor_position = self ._template .insert_text_at_cursor (char )
633
635
if new_value_cursor_position is None :
634
- self .value = value
635
- self .cursor_position = previous_cursor_position
636
636
self .restricted ()
637
637
return
638
638
639
- new_value , new_cursor_position = new_value_cursor_position
640
- if new_cursor_position >= end :
641
- self .value = new_value [:end ] + value [end :]
642
- self .cursor_position = new_cursor_position
643
- return
639
+ self .value , self .cursor_position = new_value_cursor_position
644
640
645
- value = new_value
646
- cursor_position = new_cursor_position
641
+ last_cursor_position = self .cursor_position
647
642
648
- self .value = value
649
- self .cursor_position = end
650
- while self .cursor_position > cursor_position :
651
- self ._template .move_cursor (- 1 )
643
+ while self .cursor_position < end :
652
644
self ._template .delete_at_position ()
645
+ self ._template .move_cursor (1 )
653
646
654
- self .cursor_position = cursor_position
647
+ self .cursor_position = last_cursor_position
655
648
656
649
def clear (self ) -> None :
657
650
"""Clear the masked input."""
0 commit comments