From 2f9051c6e1cc0f85b8f724215fcaa1d99e895583 Mon Sep 17 00:00:00 2001 From: Felix Van der Jeugt Date: Thu, 10 Dec 2015 18:17:13 +0100 Subject: [PATCH] shorten unique word hints --- qutebrowser/browser/hints.py | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/qutebrowser/browser/hints.py b/qutebrowser/browser/hints.py index 8128f7131..171b09dde 100644 --- a/qutebrowser/browser/hints.py +++ b/qutebrowser/browser/hints.py @@ -246,12 +246,26 @@ class HintManager(QObject): Return: A list of hint strings, in the same order as the elements. """ + + def html_text_to_hint(text): + if not text: return None + hint = text.split()[0].lower() + if hint.isalpha(): + return hint + return None + + def is_prefix(hint, existing): + return set(hint[:i+1] for i in range(len(hint))) & set(existing) + hints = [] hintss = set() words = iter(self._words) for elem in elems: - hint = _html_text_to_hint(str(elem)) or next(words) - while set(hint[:i+1] for i in range(len(hint))) & set(hintss): + hint = html_text_to_hint(str(elem)) + if hint and len(hint) >= 3 and not is_prefix(hint, hintss): + hint = next(hint[:i] for i in range(3, len(hint) + 1) + if not is_prefix(hint[:i], hintss)) + while not hint or is_prefix(hint, hintss): hint = next(words) hintss.add(hint) hints.append(hint) @@ -1010,9 +1024,3 @@ class HintManager(QObject): return self._cleanup() -def _html_text_to_hint(text): - if not text: return None - hint = text.split()[0].lower() - if hint.isalpha(): - return hint - return None