Commit 4da970f7 authored by Seblu's avatar Seblu
Browse files

Abstract readline completer by a simple completer which take a list of choice

parent 14791a44
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -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'''
+16 −2
Original line number Diff line number Diff line
@@ -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")