From c1863addfd33d3d3f3f03bcb036a0966097914cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Sch=C3=A4fer?= Date: Sun, 10 May 2020 14:38:43 +0200 Subject: Add function to find a tab by unique prefix --- poezio/core/tabs.py | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) (limited to 'poezio/core/tabs.py') 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""" -- cgit v1.2.3