diff --git a/meshterm/ui/atlas_screen.py b/meshterm/ui/atlas_screen.py index 185de7e..911b5f3 100644 --- a/meshterm/ui/atlas_screen.py +++ b/meshterm/ui/atlas_screen.py @@ -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 diff --git a/meshterm/ui/map_screen.py b/meshterm/ui/map_screen.py index aa5fc74..62e2e92 100644 --- a/meshterm/ui/map_screen.py +++ b/meshterm/ui/map_screen.py @@ -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": diff --git a/meshterm/ui/path_composer.py b/meshterm/ui/path_composer.py index 91adb7f..a5c16f0 100644 --- a/meshterm/ui/path_composer.py +++ b/meshterm/ui/path_composer.py @@ -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") diff --git a/meshterm/ui/tui/select.py b/meshterm/ui/tui/select.py index dcb2951..34476f4 100644 --- a/meshterm/ui/tui/select.py +++ b/meshterm/ui/tui/select.py @@ -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): diff --git a/tests/test_tui.py b/tests/test_tui.py index f377f6e..eae805f 100644 --- a/tests/test_tui.py +++ b/tests/test_tui.py @@ -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)