summaryrefslogtreecommitdiff
path: root/src/tab.py
diff options
context:
space:
mode:
authorlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-11-11 05:24:43 +0000
committerlouiz@4325f9fc-e183-4c21-96ce-0ab188b42d13 <louiz@4325f9fc-e183-4c21-96ce-0ab188b42d13>2010-11-11 05:24:43 +0000
commit8bdab491458d0aef2d7552ef826332db4096ab61 (patch)
tree3dbf783694c159bd2ca9ad61e431b94629f82eed /src/tab.py
parentd502f4a525bd2eabb6f6ef2c19128318f6423953 (diff)
downloadpoezio-8bdab491458d0aef2d7552ef826332db4096ab61.tar.gz
poezio-8bdab491458d0aef2d7552ef826332db4096ab61.tar.bz2
poezio-8bdab491458d0aef2d7552ef826332db4096ab61.tar.xz
poezio-8bdab491458d0aef2d7552ef826332db4096ab61.zip
roster search now uses difflib
Diffstat (limited to 'src/tab.py')
-rw-r--r--src/tab.py26
1 files changed, 23 insertions, 3 deletions
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