Skip to content

Commit

Permalink
Fixed behavior of blank character
Browse files Browse the repository at this point in the history
Placeholder characters are only a visual thing and do not affect the field value; missing or deleted characters within the value
are always replaced with space.
  • Loading branch information
amottola committed Jun 5, 2024
1 parent aab1215 commit 824717f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
20 changes: 9 additions & 11 deletions src/textual/widgets/_input.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def __rich_console__(
style,
)
for index, (c, char_def) in enumerate(zip(value, template.template)):
if c == template.blank:
if c == " ":
result.stylize(style, index, index + 1)

if self.cursor_visible and input.has_focus:
Expand Down Expand Up @@ -204,7 +204,7 @@ def __init__(self, input: Input, template: str) -> None:
self.update_mask(input.placeholder)

def validate(self, value) -> ValidationResult:
full_value = value.ljust(len(self.template), self.blank)
full_value = value.ljust(len(self.template), chr(0))
for c, char_def in zip(full_value, self.template):
if (char_def.flags & _CharFlags.REQUIRED) and not char_def.pattern.match(c):
return self.failure("Value does not match template!", value)
Expand Down Expand Up @@ -245,7 +245,7 @@ def insert_text_at_cursor(self, text: str) -> str | None:
):
char = self.template[cursor_position].char
else:
char = self.blank
char = " "
value = (
value[:cursor_position]
+ char
Expand Down Expand Up @@ -293,14 +293,12 @@ def delete_at_position(self, position: int | None = None) -> None:
if cursor_position == len(value) - 1:
value = value[:cursor_position]
else:
value = (
value[:cursor_position] + self.blank + value[cursor_position + 1 :]
)
value = value[:cursor_position] + " " + value[cursor_position + 1 :]
pos = len(value)
while pos > 0:
char_def = self.template[pos - 1]
if ((char_def.flags & _CharFlags.SEPARATOR) == 0) and (
value[pos - 1] != self.blank
value[pos - 1] != " "
):
break
pos -= 1
Expand Down Expand Up @@ -347,7 +345,7 @@ def next_separator(self, position: int | None = None) -> str | None:
def display(self, value: str) -> str:
result = []
for c, char_def in zip(value, self.template):
if c == self.blank:
if c == " ":
c = char_def.char
result.append(c)
return "".join(result)
Expand All @@ -365,10 +363,10 @@ def mask(self) -> str:
return "".join([c.char for c in self.template])

@property
def blank_mask(self) -> str:
def empty_mask(self) -> str:
return "".join(
[
self.blank if (c.flags & _CharFlags.SEPARATOR) == 0 else c.char
" " if (c.flags & _CharFlags.SEPARATOR) == 0 else c.char
for c in self.template
]
)
Expand Down Expand Up @@ -1133,7 +1131,7 @@ def action_delete_left_all(self) -> None:
self.value = ""
else:
self.value = (
self._template.blank_mask[:cursor_position]
self._template.empty_mask[:cursor_position]
+ self.value[cursor_position:]
)
else:
Expand Down
20 changes: 10 additions & 10 deletions tests/input/test_input_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,30 +112,30 @@ async def test_key_modification_actions():
input.value = serial
assert input.is_valid
input.action_delete_right()
assert input.value == "_BCDE-FGHIJ-KLMNO-PQRST"
assert input.value == " BCDE-FGHIJ-KLMNO-PQRST"
input.cursor_position = 3
input.action_delete_left()
assert input.value == "_B_DE-FGHIJ-KLMNO-PQRST"
assert input.value == " B DE-FGHIJ-KLMNO-PQRST"
input.cursor_position = 6
input.action_delete_left()
assert input.value == "_B_D_-FGHIJ-KLMNO-PQRST"
assert input.value == " B D -FGHIJ-KLMNO-PQRST"
input.cursor_position = 9
input.action_delete_left_word()
assert input.value == "_B_D_-___IJ-KLMNO-PQRST"
assert input.value == " B D - IJ-KLMNO-PQRST"
input.action_delete_left_word()
assert input.value == "_____-___IJ-KLMNO-PQRST"
assert input.value == " - IJ-KLMNO-PQRST"
input.cursor_position = 15
input.action_delete_right_word()
assert input.value == "_____-___IJ-KLM__-PQRST"
assert input.value == " - IJ-KLM -PQRST"
input.action_delete_right_word()
assert input.value == "_____-___IJ-KLM"
assert input.value == " - IJ-KLM"
input.cursor_position = 10
input.action_delete_right_all()
assert input.value == "_____-___I"
assert input.value == " - I"
await pilot.press("J")
assert input.value == "_____-___IJ-"
assert input.value == " - IJ-"
input.action_cursor_left()
input.action_delete_left_all()
assert input.value == "_____-____J-"
assert input.value == " - J-"
input.clear()
assert input.value == ""

0 comments on commit 824717f

Please sign in to comment.