diff options
author | Raúl Benencia <rul@kalgan.cc> | 2012-10-11 15:30:31 -0300 |
---|---|---|
committer | Raúl Benencia <rul@kalgan.cc> | 2012-10-11 15:30:31 -0300 |
commit | 3f9dd1f5c5c563ddaf0ea18614667edaa7e1feaa (patch) | |
tree | 370261673be2e5a949ab12bb60037fadedcb6650 /lib/completionmanager.py | |
parent | 7ef52c68574b54333c44c5be739049deac3a28ec (diff) |
Diffstat (limited to 'lib/completionmanager.py')
-rw-r--r-- | lib/completionmanager.py | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/lib/completionmanager.py b/lib/completionmanager.py index fe6f099..72458f8 100644 --- a/lib/completionmanager.py +++ b/lib/completionmanager.py @@ -1,9 +1,81 @@ +# Almost all code is based on themole completion. http://themole.nasel.com.ar + +import readline, re + +import sys + class CompletionManager: - def __init__(self): - pass + def __init__(self, cmd_manager, loader): + self.manager = cmd_manager + self.loader = loader + + readline.parse_and_bind("tab: complete") + readline.set_completer(self.completer) + readline.set_completer_delims(' \t\n`~!@#$%^&*()=+[{]}\\|;:\'",<>/?') + + self.parse_regex = re.compile('("[^"]*"|\'[^\']+\')') + + def completer(self, text, state): + if readline.get_begidx() == 0: + return self.generate_commands(text, state) + else: + return self.generate_parameters(text, state) + + def get_completion(self, text, state): + if self.current == len(self.available): + return None + else: + tmp = self.available[self.current] + self.current += 1 + + return tmp + ' ' + + def generate_commands(self, text, state): + if state == 0: + self.available = [] + self.current = 0 + + for c in self.manager.commands(): + if c[0:len(text)] == text: + self.available.append(c) + + self.available.sort() + if len(self.available) == 1 and self.available[0] == text: + self.available = [] + self.current = 0 + + return text + ' ' + + return self.get_completion(text, state) + + def generate_parameters(self, text, state): + if state == 0: + self.available = [] + self.current = 0 + + try: + line = readline.get_line_buffer()[:readline.get_endidx()].split(' ') + cmd = self.manager.find(line[0]) + except: + return 0 + + current_params = list(filter(lambda x: len(x.strip()) > 0, line[1:-1] if len(line) > 2 else [])) + if ',' in text: + text = text.split(',')[-1] + + for i in cmd.parameters(self.loader, current_params): + if i[:len(text)] == text: + self.available.append(i) + + self.available.sort() + if len(self.available) == 1: + self.available = [] + self.current = 0 + + return text + cmd.parameter_separator(current_params) + + return self.get_completion(text, state) + - def generate_commands(self): - pass + - def generate_parameters(self): - pass |