fix(spotlight): return empty for unrelated caffeine queries

Add had_prefix flag so caffeine.search() returns [] for queries
without 'caffeine' or 'caff' prefix instead of always showing help.
Add debug logging.
This commit is contained in:
sankalp committed 2026-07-28 21:13:54 +05:45
1 parent 006a9dc97e
commit 36158ffbbb
1 file changed
+22 -9
+22 -9
View File
@@ -212,29 +212,38 @@ class CaffeinePlugin(SpotlightPlugin):
def search(self, query: str, token) -> list[SearchResult]: def search(self, query: str, token) -> list[SearchResult]:
q = query.strip().lower() q = query.strip().lower()
had_prefix = False
for kw in ("caffeine ", "caff "): for kw in ("caffeine ", "caff "):
if q.startswith(kw): if q.startswith(kw):
q = q[len(kw) :].strip() q = q[len(kw) :].strip()
had_prefix = True
break break
active = self.is_active() active = self.is_active()
logger.info(
f"[Caffeine] search query={query!r} q={q!r} had_prefix={had_prefix} active={active}"
)
if not q or q in ("caffeine", "caff"): if not q or q in ("caffeine", "caff"):
logger.info("[Caffeine] returning menu (keyword match)")
return self._menu(active) return self._menu(active)
if q == "off": if q == "off":
logger.info("[Caffeine] returning off")
return [ return [
self._mk( self._mk(
"off", "Disable Caffeine", "Allow idle and sleep again", active "off", "Disable Caffeine", "Allow idle and sleep again", active
) )
] ]
if q == "on": if q == "on":
logger.info("[Caffeine] returning on")
return [ return [
self._mk("on", "Enable Caffeine", "Stay awake until turned off", active) self._mk("on", "Enable Caffeine", "Stay awake until turned off", active)
] ]
secs = self._parse_duration(q) secs = self._parse_duration(q)
if secs is not None: if secs is not None:
logger.info(f"[Caffeine] returning duration secs={secs}")
return [ return [
self._mk( self._mk(
q, q,
@@ -244,15 +253,19 @@ class CaffeinePlugin(SpotlightPlugin):
) )
] ]
return [ if had_prefix:
self._result( logger.info("[Caffeine] returning help (had_prefix)")
"caffeine_help", return [
"Caffeine", self._result(
"Usage: caffeine on | off | <time> (e.g. 15m, 1hr)", "caffeine_help",
None, "Caffeine",
active, "Usage: caffeine on | off | <time> (e.g. 15m, 1hr)",
) None,
] active,
)
]
logger.info("[Caffeine] returning EMPTY (no match)")
return []
def detect(self, text: str) -> bool: def detect(self, text: str) -> bool:
t = text.strip().lower() t = text.strip().lower()