summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonas Schäfer <j.wielicki@sotecware.net>2020-05-10 14:38:43 +0200
committerJonas Schäfer <j.wielicki@sotecware.net>2020-05-12 22:26:51 +0200
commitc1863addfd33d3d3f3f03bcb036a0966097914cb (patch)
treed62d28e9b396b1d65a64e00ed5628e4c21493f64
parentd4d0c1a19f5fc5df0f082df1fb7323141175a310 (diff)
downloadpoezio-c1863addfd33d3d3f3f03bcb036a0966097914cb.tar.gz
poezio-c1863addfd33d3d3f3f03bcb036a0966097914cb.tar.bz2
poezio-c1863addfd33d3d3f3f03bcb036a0966097914cb.tar.xz
poezio-c1863addfd33d3d3f3f03bcb036a0966097914cb.zip
Add function to find a tab by unique prefix
-rw-r--r--poezio/core/tabs.py33
-rw-r--r--test/test_tabs.py22
2 files changed, 54 insertions, 1 deletions
diff --git a/poezio/core/tabs.py b/poezio/core/tabs.py
index abea7313..c5ecb206 100644
--- a/poezio/core/tabs.py
+++ b/poezio/core/tabs.py
@@ -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"""
diff --git a/test/test_tabs.py b/test/test_tabs.py
index 0a6930d4..6989bd67 100644
--- a/test/test_tabs.py
+++ b/test/test_tabs.py
@@ -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)