Commit | Line | Data |
---|---|---|
94a5f62e | 1 | import readline |
4ac5fe72 | 2 | import rlcompleter |
b2b933a9 | 3 | |
94a5f62e | 4 | class RainbowCompleter(object): |
b2b933a9 | 5 | |
94a5f62e | 6 | def __init__(self, options): |
7 | """ | |
8 | Init | |
9 | """ | |
d51b4107 O |
10 | self.options = options |
11 | self.current_candidates = [] | |
94a5f62e | 12 | return |
13 | ||
14 | def complete(self, text, state): | |
15 | """ | |
16 | Complete | |
17 | """ | |
18 | response = None | |
19 | if state == 0: | |
d51b4107 O |
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()) | |
94a5f62e | 28 | else: |
d51b4107 O |
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 = [] | |
b2b933a9 | 44 | |
94a5f62e | 45 | try: |
d51b4107 | 46 | response = self.current_candidates[state] |
94a5f62e | 47 | except IndexError: |
48 | response = None | |
49 | return response | |
50 | ||
51 | ||
d51b4107 | 52 | def init_interactive_shell(d): |
94a5f62e | 53 | """ |
54 | Init the rainbow shell | |
55 | """ | |
d51b4107 | 56 | readline.set_completer(RainbowCompleter(d).complete) |
94a5f62e | 57 | readline.parse_and_bind('set editing-mode vi') |
7b896eac O |
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") | |
4ac5fe72 O |
61 | if 'libedit' in readline.__doc__: |
62 | readline.parse_and_bind("bind ^I rl_complete") | |
63 | else: | |
c91f75f2 | 64 | readline.parse_and_bind("tab: complete") |
65 |