X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Finteractive.py;h=b7fabbcba728ec21b62c884fbfbdfed5c9b53595;hp=2b61107ba691966a28bda728e262119955efec22;hb=60431c3bca32cf69f00f680dd5d4475d0a365ddc;hpb=94a5f62e9ae80a3e1c62a6754739f5ecc76aece6 diff --git a/rainbowstream/interactive.py b/rainbowstream/interactive.py index 2b61107..b7fabbc 100644 --- a/rainbowstream/interactive.py +++ b/rainbowstream/interactive.py @@ -1,12 +1,14 @@ import readline +import rlcompleter class RainbowCompleter(object): - + def __init__(self, options): """ Init """ - self.options = sorted(options) + self.options = options + self.current_candidates = [] return def complete(self, text, state): @@ -15,24 +17,49 @@ class RainbowCompleter(object): """ response = None if state == 0: - if text: - self.matches = [s - for s in self.options - if s and s.startswith(text)] + origline = readline.get_line_buffer() + begin = readline.get_begidx() + end = readline.get_endidx() + being_completed = origline[begin:end] + words = origline.split() + + if not words: + self.current_candidates = sorted(self.options.keys()) else: - self.matches = self.options[:] - + try: + if begin == 0: + candidates = self.options.keys() + else: + first = words[0] + candidates = self.options[first] + + if being_completed: + self.current_candidates = [ w for w in candidates + if w.startswith(being_completed) ] + else: + self.current_candidates = candidates + + except (KeyError, IndexError), err: + self.current_candidates = [] + try: - response = self.matches[state] + response = self.current_candidates[state] except IndexError: response = None return response -def init_interactive_shell(set): +def init_interactive_shell(d): """ Init the rainbow shell """ - readline.set_completer(RainbowCompleter(set).complete) - readline.parse_and_bind('tab: complete') + readline.set_completer(RainbowCompleter(d).complete) readline.parse_and_bind('set editing-mode vi') + readline.parse_and_bind("set input-meta on") + readline.parse_and_bind("set convert-meta off") + readline.parse_and_bind("set output-meta on") + if 'libedit' in readline.__doc__: + readline.parse_and_bind("bind ^I rl_complete") + else: + readline.parse_and_bind("tab: complete") +