-
Notifications
You must be signed in to change notification settings - Fork 0
/
autocomplete.py
25 lines (20 loc) · 932 Bytes
/
autocomplete.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
# reference: http://blog.doughellmann.com/2008/11/pymotw-readline.html
import readline
class MyCompleter(object): # Custom completer
def __init__(self, options):
self.options = sorted(options)
def complete(self, text, state):
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.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
completer = MyCompleter(['netstat','whoami','dir','ls','df','con','clear','serall'])
readline.set_completer(completer.complete)
readline.parse_and_bind('tab: complete')