Skip to content

Core: Fix auto-fill in the text client when clicking on a hint suggestion #3267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions MultiServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1093,6 +1093,20 @@ def get_intended_text(input_text: str, possible_answers) -> typing.Tuple[str, bo
f"did you mean '{picks[0][0]}'? ({picks[0][1]}% sure)"


def get_input_text_from_response(text: str, command: str) -> typing.Optional[str]:
if "did you mean " in text:
for question in ("Didn't find something that closely matches",
"Too many close matches"):
if text.startswith(question):
name = Utils.get_text_between(text, "did you mean '",
"'? (")
return f"!{command} {name}"
break
elif text.startswith("Missing: "):
return text.replace("Missing: ", "!hint_location ")
return None


class CommandMeta(type):
def __new__(cls, name, bases, attrs):
commands = attrs["commands"] = {}
Expand Down
16 changes: 6 additions & 10 deletions kvui.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import typing
import re

from MultiServer import get_input_text_from_response

if sys.platform == "win32":
import ctypes

Expand Down Expand Up @@ -285,16 +287,10 @@ def on_touch_down(self, touch):
temp = MarkupLabel(text=self.text).markup
text = "".join(part for part in temp if not part.startswith(("[color", "[/color]", "[ref=", "[/ref]")))
cmdinput = App.get_running_app().textinput
if not cmdinput.text and " did you mean " in text:
for question in ("Didn't find something that closely matches, did you mean ",
"Too many close matches, did you mean "):
if text.startswith(question):
name = Utils.get_text_between(text, question,
"? (")
cmdinput.text = f"!{App.get_running_app().last_autofillable_command} {name}"
break
elif not cmdinput.text and text.startswith("Missing: "):
cmdinput.text = text.replace("Missing: ", "!hint_location ")
if not cmdinput.text:
input_text = get_input_text_from_response(text, App.get_running_app().last_autofillable_command)
if input_text is not None:
cmdinput.text = input_text

Clipboard.copy(text.replace("&", "&").replace("&bl;", "[").replace("&br;", "]"))
return self.parent.select_with_touch(self.index, touch)
Expand Down
21 changes: 21 additions & 0 deletions test/general/test_client_server_interaction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest

from MultiServer import get_intended_text, get_input_text_from_response


class TestClient(unittest.TestCase):
def test_autofill_hint_from_fuzzy_hint(self) -> None:
tests = (
("item", ["item1", "item2"]), # Multiple close matches
("itm", ["item1", "item21"]), # No close match, multiple option
("item", ["item1"]), # No close match, single option
("item", ["\"item\" 'item' (item)"]), # Testing different special characters
)

for input_text, possible_answers in tests:
item_name, usable, response = get_intended_text(input_text, possible_answers)
self.assertFalse(usable, "This test must be updated, it seems get_fuzzy_results behavior changed")

hint_command = get_input_text_from_response(response, "hint")
self.assertIsNotNone(hint_command, "The response to fuzzy hints is no longer recognized by the hint autofill")
self.assertEqual(hint_command, f"!hint {item_name}", "The hint command autofilled by the response is not correct")
Loading