From 8bdab491458d0aef2d7552ef826332db4096ab61 Mon Sep 17 00:00:00 2001 From: "louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13" Date: Thu, 11 Nov 2010 05:24:43 +0000 Subject: roster search now uses difflib --- src/tab.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/tab.py b/src/tab.py index 02ba4865..01ffeb42 100644 --- a/src/tab.py +++ b/src/tab.py @@ -31,6 +31,9 @@ log = logging.getLogger(__name__) import window import theme import curses +import difflib + +from sleekxmpp.xmlstream.stanzabase import JID from config import config from roster import RosterGroup, roster from contact import Contact, Resource @@ -481,7 +484,6 @@ class RosterInfoTab(Tab): self._color_state = theme.COLOR_TAB_NORMAL def on_gain_focus(self): - log.debug('on_gain_focus\n') self._color_state = theme.COLOR_TAB_CURRENT curses.curs_set(0) @@ -617,12 +619,30 @@ class ConversationTab(Tab): def on_close(self): return +def diffmatch(search, string): + """ + Use difflib and a loop to check if search_pattern can + be 'almost' found INSIDE a string. + 'almost' being defined by difflib + """ + l = len(search) + ratio = 0.7 + for i in range(len(string) - l + 1): + if difflib.SequenceMatcher(None, search, string[i:i+l]).ratio() >= ratio: + return True + return False + def jid_and_name_match(contact, txt): """ A function used to know if a contact in the roster should be shown in the roster """ - # TODO: search in nickname, and use libdiff - if txt in contact.get_bare_jid(): + ratio = 0.7 + if not txt: + return True # Everything matches when search is empty + user = JID(contact.get_bare_jid()).user + if diffmatch(txt, user): + return True + if contact.get_name() and diffmatch(txt, contact.get_name()): return True return False -- cgit v1.2.3