8 class RainbowCompleter(object):
10 def __init__(self
, options
):
14 self
.options
= options
15 self
.current_candidates
= []
18 def complete(self
, text
, state
):
24 origline
= readline
.get_line_buffer()
25 begin
= readline
.get_begidx()
26 end
= readline
.get_endidx()
27 being_completed
= origline
[begin
:end
]
28 words
= origline
.split()
31 self
.current_candidates
= sorted(self
.options
.keys())
35 candidates
= self
.options
.keys()
38 candidates
= self
.options
[first
]
41 self
.current_candidates
= [ w
for w
in candidates
42 if w
.startswith(being_completed
) ]
44 self
.current_candidates
= candidates
46 except (KeyError, IndexError), err
:
47 self
.current_candidates
= []
50 response
= self
.current_candidates
[state
]
56 def get_history_items():
61 readline
.get_history_item(i
)
62 for i
in xrange(1, readline
.get_current_history_length() + 1)
70 if os
.path
.isfile(HISTORY_FILENAME
):
71 readline
.read_history_file(HISTORY_FILENAME
)
78 readline
.write_history_file(HISTORY_FILENAME
)
81 def init_interactive_shell(d
):
83 Init the rainbow shell
85 readline
.set_completer(RainbowCompleter(d
).complete
)
86 readline
.parse_and_bind('set editing-mode vi')
87 readline
.parse_and_bind("set input-meta on")
88 readline
.parse_and_bind("set convert-meta off")
89 readline
.parse_and_bind("set output-meta on")
90 if 'libedit' in readline
.__doc
__:
91 readline
.parse_and_bind("bind ^I rl_complete")
93 readline
.parse_and_bind("tab: complete")