Improved tab-completion

This commit is contained in:
David Bartley 2007-12-18 15:50:41 -05:00
parent 5a504220aa
commit a2a5e3771a
1 changed files with 16 additions and 13 deletions

View File

@ -54,7 +54,8 @@ class WordEdit(SingleEdit):
class LdapWordEdit(WordEdit): class LdapWordEdit(WordEdit):
ldap = None ldap = None
tabbing = False index = None
def __init__(self, uri, base, attr, *args): def __init__(self, uri, base, attr, *args):
try: try:
self.ldap = ldap.initialize(uri) self.ldap = ldap.initialize(uri)
@ -64,32 +65,34 @@ class LdapWordEdit(WordEdit):
self.base = base self.base = base
self.attr = ldapi.escape(attr) self.attr = ldapi.escape(attr)
return WordEdit.__init__(self, *args) return WordEdit.__init__(self, *args)
def keypress(self, size, key): def keypress(self, size, key):
if key == 'tab' and self.ldap != None: if (key == 'tab' or key == 'shift tab') and self.ldap != None:
if self.tabbing: if self.index != None:
self.index = (self.index + 1) % len(self.choices) if key == 'tab':
self.index = (self.index + 1) % len(self.choices)
elif key == 'shift tab':
self.index = (self.index - 1) % len(self.choices)
text = self.choices[self.index] text = self.choices[self.index]
self.set_edit_text(text) self.set_edit_text(text)
self.set_edit_pos(len(text)) self.set_edit_pos(len(text))
else: else:
try: try:
search = ldapi.escape(self.get_edit_text()) text = self.get_edit_text()
search = ldapi.escape(text)
matches = self.ldap.search_s(self.base, matches = self.ldap.search_s(self.base,
ldap.SCOPE_SUBTREE, '(%s=%s*)' % (self.attr, search)) ldap.SCOPE_SUBTREE, '(%s=%s*)' % (self.attr, search))
self.choices = [] self.choices = [ text ]
for match in matches: for match in matches:
(_, attrs) = match (_, attrs) = match
self.choices += attrs['uid'] self.choices += attrs['uid']
if len(self.choices) > 0: self.choices.sort()
self.index = 0 self.index = 0
self.tabbing = True self.keypress(size, key)
text = self.choices[self.index]
self.set_edit_text(text)
self.set_edit_pos(len(text))
except ldap.LDAPError, e: except ldap.LDAPError, e:
pass pass
else: else:
self.tabbing = False self.index = None
return WordEdit.keypress(self, size, key) return WordEdit.keypress(self, size, key)
class LdapFilterWordEdit(LdapWordEdit): class LdapFilterWordEdit(LdapWordEdit):