summaryrefslogtreecommitdiff
path: root/poezio
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 /poezio
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
Diffstat (limited to 'poezio')
-rw-r--r--poezio/core/tabs.py33
1 files changed, 32 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"""