X-Git-Url: https://vcs.fsf.org/?p=rainbowstream.git;a=blobdiff_plain;f=rainbowstream%2Finteractive.py;h=47a257633a68a8685e68d18014d5ae5bd9ce5f10;hp=abf45b7fc1f67b1974dfac1220c2d7c65e130316;hb=1e9794859fb00052587ab17a4f472bd62aea7f65;hpb=c91f75f269a8d1fff06701ae745103548e5ea974 diff --git a/rainbowstream/interactive.py b/rainbowstream/interactive.py index abf45b7..47a2576 100644 --- a/rainbowstream/interactive.py +++ b/rainbowstream/interactive.py @@ -1,5 +1,9 @@ import readline import rlcompleter +import os.path + +from .config import * + class RainbowCompleter(object): @@ -7,7 +11,8 @@ class RainbowCompleter(object): """ Init """ - self.options = sorted(options) + self.options = options + self.current_candidates = [] return def complete(self, text, state): @@ -16,25 +21,68 @@ 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 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 + """ + if os.path.isfile(HISTORY_FILENAME): + readline.read_history_file(HISTORY_FILENAME) + + +def save_history(): + """ + Save history to file + """ + readline.write_history_file(HISTORY_FILENAME) + + +def init_interactive_shell(d): """ Init the rainbow shell """ - readline.set_completer(RainbowCompleter(set).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")