Refactor handling search in command.py

This commit is contained in:
Florian Bruhin 2018-05-03 13:58:59 +02:00
parent 49bdcd5a97
commit 6eb8284fe0

View File

@ -71,13 +71,25 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
self.textChanged.connect(self.updateGeometry)
self.textChanged.connect(self._incremental_search)
dispatcher = objreg.get('command-dispatcher',
scope='window', window=self._win_id)
search_fn = dispatcher.search
self.search_prefixes = {
'/': search_fn,
'?': functools.partial(search_fn, reverse=True)
self._command_dispatcher = objreg.get(
'command-dispatcher', scope='window', window=self._win_id)
def _handle_search(self):
"""Check if the currently entered text is a search, and if so, run it.
Return:
True if a search was executed, False otherwise.
"""
search_prefixes = {
'/': self._command_dispatcher.search,
'?': functools.partial(
self._command_dispatcher.search, reverse=True)
}
if self.prefix() in search_prefixes:
search_fn = search_prefixes[self.prefix()]
search_fn(self.text()[1:])
return True
return False
def prefix(self):
"""Get the currently entered command prefix."""
@ -178,10 +190,8 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
modeman.leave(self._win_id, usertypes.KeyMode.command,
'cmd accept')
prefix = text[0]
if prefix in self.search_prefixes:
self.search_prefixes[prefix](text[1:])
else:
was_search = self._handle_search()
if not was_search:
self.got_cmd[str].emit(text[1:])
@cmdutils.register(instance='status-command', scope='window')
@ -268,5 +278,4 @@ class Command(misc.MinimalLineEditMixin, misc.CommandLineEdit):
if not config.val.search.incremental:
return
if self.prefix() in self.search_prefixes:
self.search_prefixes[self.prefix()](text[1:])
self._handle_search()