Filters: ignore leading and trailing spaces
Every type-to-filter surface — the shared select list, the map's node find, the atlas find, and the path composer's hop entry — now refuses a leading space (the query never begins with whitespace) and strips the query when matching (so a trailing space is ignored). Spaces still count mid-query, so a multi-word name like "Homestead R&D" filters fine; only " Homestead" and "Homestead " are prevented from mattering. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
ee8731e4d4
commit
1b20989e6c
5 files changed
+34
-15
No files matched your search
@@ -271,7 +271,7 @@ class AtlasScreen(Screen):
|
||||
|
||||
def _matches(self) -> list[str]:
|
||||
"""Nodes the find filter matches: nearest first, then by display name."""
|
||||
needle = self._filter.casefold()
|
||||
needle = self._filter.strip().casefold()
|
||||
if not needle:
|
||||
return []
|
||||
depths = self._hops_out()
|
||||
@@ -331,8 +331,9 @@ class AtlasScreen(Screen):
|
||||
self._filter = ""
|
||||
self._index = 0
|
||||
elif action == "text":
|
||||
self._filter += data
|
||||
self._index = 0
|
||||
if not data.isspace() or self._filter: # never begin the filter with a space
|
||||
self._filter += data
|
||||
self._index = 0
|
||||
elif action == "space" and self._filter:
|
||||
self._filter += " " # node names carry spaces; only meaningful mid-query
|
||||
self._needs_scrub = True
|
||||
|
||||
@@ -200,9 +200,9 @@ class MapScreen(Screen):
|
||||
|
||||
def _matches(self) -> list[MapMarker]:
|
||||
"""The markers the live find filter currently matches (all of them when off)."""
|
||||
if not self._filter:
|
||||
needle = self._filter.strip().casefold()
|
||||
if not needle:
|
||||
return self._markers
|
||||
needle = self._filter.casefold()
|
||||
return [m for m in self._markers if needle in m.label.casefold()]
|
||||
|
||||
def _title(self, vp: Viewport) -> str:
|
||||
@@ -275,7 +275,8 @@ class MapScreen(Screen):
|
||||
elif action in ("home", "ctrl_home"):
|
||||
self._reset_view(vp)
|
||||
elif action == "text" and self.find_enabled:
|
||||
self._filter += data
|
||||
if not data.isspace() or self._filter: # never begin the filter with a space
|
||||
self._filter += data
|
||||
elif action == "space" and self._filter:
|
||||
self._filter += " " # node names carry spaces; only meaningful mid-query
|
||||
elif action == "backspace":
|
||||
|
||||
@@ -239,7 +239,7 @@ class PathComposerScreen(Screen):
|
||||
{self._topology.self_id, self._anchor()} | ({after} if after else set())
|
||||
)
|
||||
suggestions = self._topology.next_hops(self._anchor(), exclude=exclude)
|
||||
needle = self._entry.lower()
|
||||
needle = self._entry.strip().lower()
|
||||
if needle:
|
||||
suggestions = [
|
||||
s
|
||||
@@ -251,7 +251,7 @@ class PathComposerScreen(Screen):
|
||||
|
||||
def _custom_hex(self) -> Optional[str]:
|
||||
"""The typed entry as an addable hex hop, or ``None`` when it isn't one."""
|
||||
needle = self._entry.lower().removeprefix("0x")
|
||||
needle = self._entry.strip().lower().removeprefix("0x")
|
||||
return needle if _is_hex(needle) else None
|
||||
|
||||
def _rows(self) -> list[tuple[str, object]]:
|
||||
@@ -542,7 +542,8 @@ class PathComposerScreen(Screen):
|
||||
self._cursor -= 1
|
||||
self._index = 0
|
||||
elif action == "text" and data.isprintable() and data not in ("/",):
|
||||
self._entry += data
|
||||
self._index = 0
|
||||
if not data.isspace() or self._entry: # never begin the entry with a space
|
||||
self._entry += data
|
||||
self._index = 0
|
||||
elif action == "escape":
|
||||
super().handle("escape")
|
||||
@@ -213,9 +213,9 @@ class SelectScreen(Screen):
|
||||
matching choices are shown — but every separator stays, so the list keeps its
|
||||
section landmarks (and column headers) while it narrows.
|
||||
"""
|
||||
if not self._filter:
|
||||
needle = self._filter.strip().lower()
|
||||
if not needle:
|
||||
return self._items
|
||||
needle = self._filter.lower()
|
||||
return [
|
||||
it
|
||||
for it in self._items
|
||||
@@ -469,9 +469,13 @@ class SelectScreen(Screen):
|
||||
self._index = 0
|
||||
self._hshift = 0
|
||||
elif action == "text" and self._filterable and data.isprintable():
|
||||
self._filter += data
|
||||
self._index = 0
|
||||
self._hshift = 0
|
||||
# A leading space is ignored (the filter never begins with whitespace); a
|
||||
# trailing one is dropped when matching (see _rows), so spaces count only
|
||||
# mid-query — inside a multi-word name like "Homestead R&D".
|
||||
if not data.isspace() or self._filter:
|
||||
self._filter += data
|
||||
self._index = 0
|
||||
self._hshift = 0
|
||||
|
||||
|
||||
class ReorderScreen(Screen):
|
||||
|
||||
@@ -116,6 +116,18 @@ def test_select_filter_narrows_but_keeps_separators() -> None:
|
||||
assert _run(screen, "enter") == 1
|
||||
|
||||
|
||||
def test_select_filter_ignores_leading_and_trailing_spaces() -> None:
|
||||
"""A leading space never begins the filter; a trailing one is dropped when matching."""
|
||||
screen = _menu()
|
||||
screen.handle("text", " ") # ignored — the filter never starts with whitespace
|
||||
assert screen._filter == ""
|
||||
for ch in "alpha ": # "alpha", then a trailing space
|
||||
screen.handle("text", ch)
|
||||
assert screen._filter == "alpha " # the space stays in the buffer…
|
||||
# …but matching strips it, so the trailing space doesn't stop "alpha" from matching.
|
||||
assert [r.title for r in screen._rows() if isinstance(r, Choice)] == ["alpha"]
|
||||
|
||||
|
||||
def test_select_non_filterable_ignores_typing() -> None:
|
||||
"""With filtering off, typed keys neither narrow the list nor add a filter line."""
|
||||
screen = SelectScreen("pick", [Choice("alpha", 1), Choice("beta", 2)], filterable=False)
|
||||
|
||||
Reference in new issue
Block a user