X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Finteractive.py;h=76755e8c21adf85dfc2fddb4835f1889021081b1;hp=e27c17780e3cd7d1256bb5b52503d9c68f79e14b;hb=0e6d22d6fb2c54f5eb37028da1414279e2d1a625;hpb=b2b933a939fc0a10b2a6300089ac2a72d2cfb17b diff --git a/rainbowstream/interactive.py b/rainbowstream/interactive.py index e27c177..76755e8 100644 --- a/rainbowstream/interactive.py +++ b/rainbowstream/interactive.py @@ -1,4 +1,7 @@ import readline +import os.path + +from .config import * class RainbowCompleter(object): @@ -7,7 +10,8 @@ class RainbowCompleter(object): """ Init """ - self.options = sorted(options) + self.options = options + self.current_candidates = [] return def complete(self, text, state): @@ -16,24 +20,77 @@ 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([c for c in self.options]) else: - self.matches = self.options[:] + try: + if begin == 0: + candidates = [c for c in self.options] + elif words[-1] in self.options[words[0]]: + candidates = [] + 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): + 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 get_history_items(): + """ + Get all history item + """ + return [ + readline.get_history_item(i) + for i in xrange(1, readline.get_current_history_length() + 1) + ] + + +def read_history(): + """ + Read history file + """ + try: + readline.read_history_file(c['HISTORY_FILENAME']) + except: + pass + + +def save_history(): + """ + Save history to file + """ + try: + readline.write_history_file(c['HISTORY_FILENAME']) + except: + pass + + +def init_interactive_shell(d): """ Init the rainbow shell """ - readline.set_completer(RainbowCompleter(set).complete) - readline.parse_and_bind('tab: complete') - readline.parse_and_bind('set editing-mode vi') + readline.set_completer(RainbowCompleter(d).complete) + readline.parse_and_bind('set skip-completed-text on') + if 'libedit' in readline.__doc__: + readline.parse_and_bind("bind ^I rl_complete") + else: + readline.parse_and_bind("tab: complete")