Add function to find a tab by unique prefix
This commit is contained in:
parent
d4d0c1a19f
commit
c1863addfd
2 changed files with 54 additions and 1 deletions
|
@ -24,7 +24,7 @@ have become [0|1|2|3], with the tab "4" renumbered to "3" if gap tabs are
|
|||
disabled.
|
||||
"""
|
||||
|
||||
from typing import List, Dict, Type, Optional, Union
|
||||
from typing import List, Dict, Type, Optional, Union, Tuple
|
||||
from collections import defaultdict
|
||||
from slixmpp import JID
|
||||
from poezio import tabs
|
||||
|
@ -139,6 +139,37 @@ class Tabs:
|
|||
return self._tabs[i]
|
||||
return None
|
||||
|
||||
def find_by_unique_prefix(self, prefix: str) -> Tuple[bool, Optional[tabs.Tab]]:
|
||||
"""
|
||||
Get a tab by its unique name prefix, ignoring case.
|
||||
|
||||
:return: A tuple indicating the presence of any match, as well as the
|
||||
uniquely matched tab (if any).
|
||||
|
||||
The first element, a boolean, in the returned tuple indicates whether
|
||||
at least one tab matched.
|
||||
|
||||
The second element (a Tab) in the returned tuple is the uniquely
|
||||
matched tab, if any. If multiple or no tabs match the prefix, the
|
||||
second element in the tuple is :data:`None`.
|
||||
"""
|
||||
|
||||
# TODO: should this maybe use something smarter than .lower()?
|
||||
# something something stringprep?
|
||||
prefix = prefix.lower()
|
||||
candidate = None
|
||||
any_matched = False
|
||||
for tab in self._tabs:
|
||||
if not tab.name.lower().startswith(prefix):
|
||||
continue
|
||||
any_matched = True
|
||||
if candidate is not None:
|
||||
# multiple tabs match -> return None
|
||||
return True, None
|
||||
candidate = tab
|
||||
|
||||
return any_matched, candidate
|
||||
|
||||
def by_name_and_class(self, name: str,
|
||||
cls: Type[tabs.Tab]) -> Optional[tabs.Tab]:
|
||||
"""Get a tab with its name and class"""
|
||||
|
|
|
@ -183,3 +183,25 @@ def test_slice():
|
|||
tabs.append(dummy3)
|
||||
|
||||
assert tabs[1:2][0] is dummy2
|
||||
|
||||
def test_find_by_unique_prefix():
|
||||
DummyTab.reset()
|
||||
tabs = Tabs(h)
|
||||
t1 = DummyTab()
|
||||
t2 = DummyTab()
|
||||
t3 = DummyTab()
|
||||
tabs.append(t1)
|
||||
tabs.append(t2)
|
||||
tabs.append(t3)
|
||||
|
||||
t1.name = "foo"
|
||||
t2.name = "bar"
|
||||
t3.name = "fnord"
|
||||
|
||||
assert tabs.find_by_unique_prefix("f") == (True, None)
|
||||
assert tabs.find_by_unique_prefix("b") == (True, t2)
|
||||
assert tabs.find_by_unique_prefix("fo") == (True, t1)
|
||||
assert tabs.find_by_unique_prefix("fn") == (True, t3)
|
||||
assert tabs.find_by_unique_prefix("fx") == (False, None)
|
||||
assert tabs.find_by_unique_prefix("x") == (False, None)
|
||||
assert tabs.find_by_unique_prefix("") == (True, None)
|
||||
|
|
Loading…
Reference in a new issue