2b61107ba691966a28bda728e262119955efec22
[rainbowstream.git] / rainbowstream / interactive.py
1 import readline
2
3 class RainbowCompleter(object):
4
5 def __init__(self, options):
6 """
7 Init
8 """
9 self.options = sorted(options)
10 return
11
12 def complete(self, text, state):
13 """
14 Complete
15 """
16 response = None
17 if state == 0:
18 if text:
19 self.matches = [s
20 for s in self.options
21 if s and s.startswith(text)]
22 else:
23 self.matches = self.options[:]
24
25 try:
26 response = self.matches[state]
27 except IndexError:
28 response = None
29 return response
30
31
32 def init_interactive_shell(set):
33 """
34 Init the rainbow shell
35 """
36 readline.set_completer(RainbowCompleter(set).complete)
37 readline.parse_and_bind('tab: complete')
38 readline.parse_and_bind('set editing-mode vi')