summaryrefslogtreecommitdiff
path: root/ql/completer.py
diff options
context:
space:
mode:
authorMichal Zimniewicz <michzimny@gmail.com>2016-10-09 11:27:06 +0200
committerMichal Zimniewicz <michzimny@gmail.com>2016-10-09 11:27:06 +0200
commit0a27bbf9ce1f5b1a52bc62ff4d9e5c2f960b030c (patch)
treedf4a562b9e2452bc288264156c1e47bf6fcedeb3 /ql/completer.py
Initial commit
Diffstat (limited to 'ql/completer.py')
-rw-r--r--ql/completer.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/ql/completer.py b/ql/completer.py
new file mode 100644
index 0000000..a9b7d8d
--- /dev/null
+++ b/ql/completer.py
@@ -0,0 +1,29 @@
+import readline
+
+
+class Completer(object):
+
+ @classmethod
+ def install_new_completer(cls, options):
+ completer = cls(options)
+ readline.set_completer(completer.complete)
+ readline.set_completer_delims('') # allow options with whitespace
+ readline.parse_and_bind('tab: complete')
+
+ def __init__(self, options):
+ self.options = options
+
+ def complete(self, text, state):
+ text = text.lower()
+ if state == 0: # on first trigger, build possible matches
+ if text: # cache matches (entries that start with entered text)
+ self.matches = [s for s in self.options
+ if s and s.lower().startswith(text)]
+ else: # no text entered, all matches possible
+ self.matches = self.options[:]
+
+ # return match indexed by state
+ try:
+ return self.matches[state]
+ except IndexError:
+ return None