Skip to content

Commit fcf9612

Browse files
committed
Favour match at start of string
1 parent 2705b78 commit fcf9612

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/textual/widgets/_select.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def on_mount(self) -> None:
6969
def reset_query() -> None:
7070
self._search_query = ""
7171

72-
self._search_reset_timer = Timer(self, 1.0, callback=reset_query)
72+
self._search_reset_timer = Timer(self, 0.7, callback=reset_query)
7373

7474
def watch_has_focus(self, value: bool) -> None:
7575
self._search_query = ""
@@ -113,16 +113,27 @@ def select(self, index: int | None) -> None:
113113
self.scroll_to_highlight()
114114

115115
def _find_search_match(self, query: str) -> int | None:
116-
"""Find the first index"""
116+
best_match: int | None = None
117+
minimum_index: int | None = None
118+
119+
query = query.lower()
117120
for index, option in enumerate(self._options):
118121
prompt = option.prompt
119122
if isinstance(prompt, Text):
120-
if query in prompt.plain:
121-
return index
123+
lower_prompt = prompt.plain.lower()
122124
elif isinstance(prompt, str):
123-
if query in prompt:
124-
return index
125-
return None
125+
lower_prompt = prompt.lower()
126+
else:
127+
continue
128+
129+
match_index = lower_prompt.find(query)
130+
if match_index != -1 and (
131+
minimum_index is None or match_index < minimum_index
132+
):
133+
best_match = index
134+
minimum_index = match_index
135+
136+
return best_match
126137

127138
def action_dismiss(self) -> None:
128139
"""Dismiss the overlay."""

0 commit comments

Comments
 (0)