Skip to content

Commit 86985ed

Browse files
committed
new file: autocomplete.py
1 parent 07a92f5 commit 86985ed

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

autocomplete.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# reference: http://blog.doughellmann.com/2008/11/pymotw-readline.html
2+
import readline
3+
4+
class MyCompleter(object): # Custom completer
5+
6+
def __init__(self, options):
7+
self.options = sorted(options)
8+
9+
def complete(self, text, state):
10+
if state == 0: # on first trigger, build possible matches
11+
if text: # cache matches (entries that start with entered text)
12+
self.matches = [s for s in self.options
13+
if s and s.startswith(text)]
14+
else: # no text entered, all matches possible
15+
self.matches = self.options[:]
16+
17+
# return match indexed by state
18+
try:
19+
return self.matches[state]
20+
except IndexError:
21+
return None
22+
23+
completer = MyCompleter(['netstat','whoami','dir','ls','df','con','clear','serall'])
24+
readline.set_completer(completer.complete)
25+
readline.parse_and_bind('tab: complete')

0 commit comments

Comments
 (0)