fix rst
[rainbowstream.git] / build / lib / rainbowstream / interactive.py
1 import readline
2 import rlcompleter
3
4 class RainbowCompleter(object):
5
6 def __init__(self, options):
7 """
8 Init
9 """
10 self.options = options
11 self.current_candidates = []
12 return
13
14 def complete(self, text, state):
15 """
16 Complete
17 """
18 response = None
19 if state == 0:
20 origline = readline.get_line_buffer()
21 begin = readline.get_begidx()
22 end = readline.get_endidx()
23 being_completed = origline[begin:end]
24 words = origline.split()
25
26 if not words:
27 self.current_candidates = sorted(self.options.keys())
28 else:
29 try:
30 if begin == 0:
31 candidates = self.options.keys()
32 else:
33 first = words[0]
34 candidates = self.options[first]
35
36 if being_completed:
37 self.current_candidates = [ w for w in candidates
38 if w.startswith(being_completed) ]
39 else:
40 self.current_candidates = candidates
41
42 except (KeyError, IndexError), err:
43 self.current_candidates = []
44
45 try:
46 response = self.current_candidates[state]
47 except IndexError:
48 response = None
49 return response
50
51
52 def init_interactive_shell(d):
53 """
54 Init the rainbow shell
55 """
56 readline.set_completer(RainbowCompleter(d).complete)
57 readline.parse_and_bind('set editing-mode vi')
58 readline.parse_and_bind("set input-meta on")
59 readline.parse_and_bind("set convert-meta off")
60 readline.parse_and_bind("set output-meta on")
61 if 'libedit' in readline.__doc__:
62 readline.parse_and_bind("bind ^I rl_complete")
63 else:
64 readline.parse_and_bind("tab: complete")
65