diff --git a/kvui.py b/kvui.py index 12cc9319a02..c92b53520f0 100644 --- a/kvui.py +++ b/kvui.py @@ -113,6 +113,10 @@ else: remove_between_brackets = re.compile(r"\[.*?]") +def kv_unescape(text: str) -> str: + return text.replace("&", "&").replace("&bl;", "[").replace("&br;", "]") + + class ThemedApp(MDApp): def set_colors(self): text_colors = KivyJSONtoTextParser.TextColors() @@ -440,14 +444,14 @@ class SelectableLabel(RecycleDataViewBehavior, TooltipLabel): else: # Not a fan of the following few lines, but they work. temp = MarkupLabel(text=self.text).markup - text = "".join(part for part in temp if not part.startswith("[")) + text = kv_unescape("".join(part for part in temp if not part.startswith("["))) cmdinput = MDApp.get_running_app().textinput if not cmdinput.text: input_text = get_input_text_from_response(text, MDApp.get_running_app().last_autofillable_command) if input_text is not None: cmdinput.text = input_text - Clipboard.copy(text.replace("&", "&").replace("&bl;", "[").replace("&br;", "]")) + Clipboard.copy(text) return self.parent.select_with_touch(self.index, touch) def apply_selection(self, rv, index, is_selected): @@ -560,9 +564,7 @@ class AutocompleteHintInput(ResizableTextField): item_names = ctx.item_names._game_store[ctx.game].values() def on_press(text): - split_text = MarkupLabel(text=text).markup - self.set_text(self, "".join(text_frag for text_frag in split_text - if not text_frag.startswith("["))) + self.set_text(self, text) self.dropdown.dismiss() self.focus = True @@ -573,11 +575,13 @@ class AutocompleteHintInput(ResizableTextField): except ValueError: pass # substring not found else: - text = escape_markup(item_name) - text = text[:index] + "[b]" + text[index:index+len(value)]+"[/b]"+text[index+len(value):] + prefix = escape_markup(item_name[:index]) + matching = escape_markup(item_name[index:index+len(value)]) + postfix = escape_markup(item_name[index+len(value):]) + text = f"{prefix}[b]{matching}[/b]{postfix}" self.dropdown.items.append({ "text": text, - "on_release": lambda txt=text: on_press(txt), + "on_release": lambda txt=item_name: on_press(txt), "markup": True }) if not self.dropdown.parent: @@ -669,7 +673,7 @@ class HintLabel(RecycleDataViewBehavior, MDBoxLayout): else "", ". (", self.status_text.lower(), ")")) temp = MarkupLabel(text).markup text = "".join(part for part in temp if not part.startswith("[")) - Clipboard.copy(escape_markup(text).replace("&", "&").replace("&bl;", "[").replace("&br;", "]")) + Clipboard.copy(kv_unescape(text)) return self.parent.select_with_touch(self.index, touch) else: parent = self.parent