-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompleter.py
More file actions
46 lines (35 loc) · 1.25 KB
/
Copy pathcompleter.py
File metadata and controls
46 lines (35 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import readline
from difflib import get_close_matches
class Completer:
def __init__(self, cutoff=0.6):
self.keywords = []
self.cutoff = cutoff
def set_keywords(self, keywords):
self.keywords = sorted(keywords)
def set_cutoff(self, cutoff):
self.cutoff = cutoff
def completer(self, text, state):
matches = get_close_matches(text, self.keywords, n=10, cutoff=self.cutoff)
try:
return matches[state]
except IndexError:
return None
def init_readline(self, delims=' ";:=', parse=('tab: menu-complete',)):
readline.set_completer(self.completer)
readline.set_completer_delims(delims)
if isinstance(parse, str):
readline.parse_and_bind(parse)
else:
for binding in parse:
readline.parse_and_bind(binding)
@staticmethod
def set_readline_hook(text):
readline.set_startup_hook(lambda: readline.insert_text(text))
if __name__ == '__main__':
print('Testing completer')
comp = Completer()
comp.set_keywords(['apple', 'banana', 'cherry'])
comp.init_readline()
Completer.set_readline_hook("fruit ['apple', 'banana', 'cherry']: ")
while True:
test = input()