7 class RainbowCompleter(object):
9 def __init__(self
, options
):
13 self
.options
= options
14 self
.current_candidates
= []
17 def complete(self
, text
, state
):
23 origline
= readline
.get_line_buffer()
24 begin
= readline
.get_begidx()
25 end
= readline
.get_endidx()
26 being_completed
= origline
[begin
:end
]
27 words
= origline
.split()
30 self
.current_candidates
= sorted([c
for c
in self
.options
])
34 candidates
= [c
for c
in self
.options
]
35 elif words
[-1] in self
.options
[words
[0]]:
39 candidates
= self
.options
[first
]
42 self
.current_candidates
= [w
for w
in candidates
43 if w
.startswith(being_completed
)]
45 self
.current_candidates
= candidates
47 except (KeyError, IndexError):
48 self
.current_candidates
= []
51 response
= self
.current_candidates
[state
]
57 def get_history_items():
62 readline
.get_history_item(i
)
63 for i
in xrange(1, readline
.get_current_history_length() + 1)
71 if os
.path
.isfile(c
['HISTORY_FILENAME']):
72 readline
.read_history_file(c
['HISTORY_FILENAME'])
79 readline
.write_history_file(c
['HISTORY_FILENAME'])
82 def init_interactive_shell(d
):
84 Init the rainbow shell
86 readline
.set_completer(RainbowCompleter(d
).complete
)
87 readline
.parse_and_bind('set editing-mode vi')
88 readline
.parse_and_bind('set show-all-if-ambiguous on')
89 readline
.parse_and_bind('set show-all-if-unmodified on')
90 readline
.parse_and_bind('set skip-completed-text on')
91 if 'libedit' in readline
.__doc
__:
92 readline
.parse_and_bind("bind ^I rl_complete")
94 readline
.parse_and_bind("tab: complete")