From 4da970f703d65efd7b26f4b4b772e19d02b425f9 Mon Sep 17 00:00:00 2001 From: Seblu Date: Sat, 8 Jan 2011 03:42:14 +0100 Subject: [PATCH] Abstract readline completer by a simple completer which take a list of choice --- cccli/cli.py | 12 ++++-------- cccli/printer.py | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cccli/cli.py b/cccli/cli.py index 8d09414..54639fa 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -51,7 +51,7 @@ class Cli(object): self.printer.history.maxsize(self.settings.get("hsize", None)) self.printer.debug("Loaded history: %s"%len(self.printer.history)) # enable completion - self.printer.completion.set_completer(self._completer) + self.printer.completion.set_completer(self._command_completer) # set prompt # load alias self.alias.load(self.settings.get("alias", "")) @@ -155,17 +155,13 @@ class Cli(object): self.printer.error("%s: %s"%(type(e), str(e))) self.printer.warn("This is a not expected error, please report it!") - def _completer(self, texte, state): + def _command_completer(self, texte): '''Return the list of completion''' comp = self.printer.completion stripped = comp.get_buf()[:comp.get_begin() + 1].lstrip() - if state == 0 and texte == "" and stripped != "": + if texte == "" and stripped != "": return None - cl = [ c for c in Command.list() if c.startswith(texte) ] - if state < len(cl): - return cl[state] - return None - + return [ c for c in Command.list() if c.startswith(texte) ] class CliHandler(RpcHandler): '''Handle RPC incoming request''' diff --git a/cccli/printer.py b/cccli/printer.py index c70221f..d0b0a73 100644 --- a/cccli/printer.py +++ b/cccli/printer.py @@ -220,6 +220,8 @@ class Completion(object): '''Handle completion functions''' def __init__(self): self.readline = None + self.compfunc = None + self.complist = list() def __getattribute__(self, name): r = object.__getattribute__(self, "readline") @@ -241,7 +243,19 @@ class Completion(object): '''Get the ending index of the readline tab-completion scope''' return self.readline.get_begidx() + def _completer(self, text, state): + '''Readline real completer''' + if state == 0: + if self.compfunc is not None: + self.complist = list(self.compfunc(text)) + try: + return self.complist[state] + except IndexError: + self.complist = list() + return None + def set_completer(self, func): - '''Set completer function''' - self.readline.set_completer(func) + '''Set completer custom function which return a list of possibilities''' + self.compfunc = func + self.readline.set_completer(self._completer) self.readline.parse_and_bind("tab: complete") -- GitLab