Commit 2f3522cf authored by Seblu's avatar Seblu
Browse files

simple completion on command

parent 69702fa8
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -13,9 +13,9 @@ import shlex
import StringIO

import cccli
from cccli.command import Command, Alias
from cccli.printer import Printer, color
from cccli.exception import *
from cccli.printer import Printer, color
from cccli.command import Command, Alias

from sjrpc.client import SimpleRpcClient
from sjrpc.utils import RpcHandler, pure
+5 −0
Original line number Diff line number Diff line
@@ -43,6 +43,11 @@ class Command(object):
        self.cli = cli
        self.printer = cli.printer

    @classmethod
    def list(cls):
        '''Return a list of command name'''
        return [ x[4:] for x in dir(cls) if x.startswith("cmd_") ]

    @classmethod
    def usage(cls, cmdname):
        '''Return usage of a command'''
+23 −5
Original line number Diff line number Diff line
@@ -63,14 +63,19 @@ class Printer(object):
    def setinteractive(self):
        if self.readline is not None:
            return
        try:
            import readline
        except Exception as e:
            raise cliError("Unable to start readline")
        self.readline = readline
        if self.history is None:
            self.history = History(self.readline)
        else:
        # enable history
        self.history.readline = readline
        # load history
        self.history.read(self.historyfile)
        self.history.maxsize(self.historysize)
        # start prompt completer
        self.readline.set_completer(self.completer)
        self.readline.parse_and_bind("tab: complete")

    def out(self, message="", fd=sys.stdout, nl=os.linesep, flush=True):
        '''Print a message in fd ended by nl'''
@@ -152,6 +157,15 @@ class Printer(object):
        rows, cols, px_x, px_y = struct.unpack("HHHH", resp)
        return rows, cols

    def completer(self, texte, state):
        r = self.readline
        if state == 0 and texte == "" and r.get_line_buffer()[:r.get_begidx() + 1].lstrip() != "":
            return None
        cl = [ c for c in  Command.list() if c.startswith(texte) ]
        if state < len(cl):
            return cl[state]
        return None


class History(object):
    '''History class'''
@@ -214,3 +228,7 @@ class History(object):
    def clear(self):
        '''Clear history'''
        self.readline.clear_history()

# Need to be at end of file to fix cross loading of printer by command and command by printer
# So i maybe should change the design of import
from cccli.command import Command