-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathAutoCompleter.py
49 lines (37 loc) · 1.32 KB
/
AutoCompleter.py
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
47
48
49
from __future__ import print_function
import sys
import readline
from os import environ
class AutoCompleter(object): # Custom completer
def __init__(self, options, prefix):
self.options = sorted(options)
self.prefix = prefix
def complete(self, text, state):
if state == 0: # on first trigger, build possible matches
if not text:
self.matches = self.options[:]
else:
self.matches = [s for s in self.options
if s and s.startswith(text)]
# return match indexed by state
try:
return self.matches[state]
except IndexError:
return None
def display_matches(self, substitution, matches, longest_match_length):
line_buffer = readline.get_line_buffer()
columns = environ.get("COLUMNS", 80)
print('\n')
tpl = "{:<" + str(int(max(map(len, matches)) * 1.2)) + "}"
buffer = ""
for match in matches:
match = tpl.format(match[len(substitution):])
if len(buffer + match) > columns:
print(buffer)
buffer = ""
buffer += match
if buffer:
print(buffer)
print(self.prefix, end="")
print(line_buffer, end="")
sys.stdout.flush()