diff --git a/cccli/cli.py b/cccli/cli.py index 2e87f1ad096e7303869979bd67aabbe64b80582c..9889450c4ee030557605700efc48cebc78a9265e 100644 --- a/cccli/cli.py +++ b/cccli/cli.py @@ -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 diff --git a/cccli/command.py b/cccli/command.py index 0ebffdcfd26f64103d8994d01207475c46dff6bf..896785375f74e4639fa2dc95cd2bcfe4e4f017c3 100644 --- a/cccli/command.py +++ b/cccli/command.py @@ -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''' diff --git a/cccli/printer.py b/cccli/printer.py index c13ec49c5c19a9e84a1c6195e9264c85c9518987..0d9c2206bbdf279de8bb0378a59364eefa621ca8 100644 --- a/cccli/printer.py +++ b/cccli/printer.py @@ -63,14 +63,19 @@ class Printer(object): def setinteractive(self): if self.readline is not None: return - import readline + 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: - self.history.readline = readline + # 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