From 2f3522cf470819ef7b13766bee8f8ef8dc28087a Mon Sep 17 00:00:00 2001 From: Seblu Date: Fri, 7 Jan 2011 17:45:07 +0100 Subject: [PATCH] simple completion on command --- cccli/cli.py | 4 ++-- cccli/command.py | 5 +++++ cccli/printer.py | 28 +++++++++++++++++++++++----- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/cccli/cli.py b/cccli/cli.py index 2e87f1a..9889450 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 0ebffdc..8967853 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 c13ec49..0d9c220 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 -- GitLab