diff --git a/memos/cli.py b/memos/cli.py index 7827da7..b40795f 100644 --- a/memos/cli.py +++ b/memos/cli.py @@ -1,6 +1,6 @@ import click -from click_aliases import ClickAliasedGroup from memos import __VERSION__ +from memos.click_aliases import ClickAliasedGroup from memos.auth import auth_command from memos.echo import echo_command from memos.edit import edit_command @@ -22,9 +22,9 @@ def version_command(): cli.add_command(auth_command) cli.add_command(echo_command) cli.add_command(edit_command) -cli.add_command(list_command) +cli.add_command(list_command, aliases=['ls']) cli.add_command(new_command) -cli.add_command(remove_command) +cli.add_command(remove_command, aliases=['rm', 'delete', 'del']) cli.add_command(version_command) if __name__ == '__main__': diff --git a/memos/click_aliases.py b/memos/click_aliases.py new file mode 100644 index 0000000..730d351 --- /dev/null +++ b/memos/click_aliases.py @@ -0,0 +1,105 @@ +""" + This is a fork of the extension for the python + ``click`` module to provide a group or command + with aliases. + + Original project: https://github.com/click-contrib/click-aliases +""" + +import click + +_click7 = click.__version__[0] >= '7' + + +class ClickAliasedGroup(click.Group): + def __init__(self, *args, **kwargs): + super(ClickAliasedGroup, self).__init__(*args, **kwargs) + self._commands = {} + self._aliases = {} + + def add_command(self, *args, **kwargs): + aliases = kwargs.pop('aliases', []) + super(ClickAliasedGroup, self).add_command(*args, **kwargs) + if aliases: + cmd = args[0] + name = args[1] if len(args) > 1 else None + name = name or cmd.name + if name is None: + raise TypeError('Command has no name.') + + self._commands[name] = aliases + for alias in aliases: + self._aliases[alias] = cmd.name + + def command(self, *args, **kwargs): + aliases = kwargs.pop('aliases', []) + decorator = super(ClickAliasedGroup, self).command(*args, **kwargs) + if not aliases: + return decorator + + def _decorator(f): + cmd = decorator(f) + if aliases: + self._commands[cmd.name] = aliases + for alias in aliases: + self._aliases[alias] = cmd.name + return cmd + + return _decorator + + def group(self, *args, **kwargs): + aliases = kwargs.pop('aliases', []) + decorator = super(ClickAliasedGroup, self).group(*args, **kwargs) + if not aliases: + return decorator + + def _decorator(f): + cmd = decorator(f) + if aliases: + self._commands[cmd.name] = aliases + for alias in aliases: + self._aliases[alias] = cmd.name + return cmd + + return _decorator + + def resolve_alias(self, cmd_name): + if cmd_name in self._aliases: + return self._aliases[cmd_name] + return cmd_name + + def get_command(self, ctx, cmd_name): + cmd_name = self.resolve_alias(cmd_name) + command = super(ClickAliasedGroup, self).get_command(ctx, cmd_name) + if command: + return command + + def format_commands(self, ctx, formatter): + rows = [] + + sub_commands = self.list_commands(ctx) + + max_len = 0 + if len(sub_commands) > 0: + max_len = max(len(cmd) for cmd in sub_commands) + + limit = formatter.width - 6 - max_len + + for sub_command in sub_commands: + cmd = self.get_command(ctx, sub_command) + if cmd is None: + continue + if hasattr(cmd, 'hidden') and cmd.hidden: + continue + if sub_command in self._commands: + aliases = ','.join(sorted(self._commands[sub_command])) + sub_command = '{0} ({1})'.format(sub_command, aliases) + if _click7: + cmd_help = cmd.get_short_help_str(limit) + else: + cmd_help = cmd.short_help or '' + rows.append((sub_command, cmd_help)) + + if rows: + with formatter.section('Commands'): + formatter.write_dl(rows) diff --git a/poetry.lock b/poetry.lock index 7f95ef3..fe9e878 100644 --- a/poetry.lock +++ b/poetry.lock @@ -128,24 +128,6 @@ files = [ [package.dependencies] colorama = {version = "*", markers = "platform_system == \"Windows\""} -[[package]] -name = "click-aliases" -version = "1.0.1" -description = "Enable aliases for Click" -category = "main" -optional = false -python-versions = "*" -files = [ - {file = "click-aliases-1.0.1.tar.gz", hash = "sha256:f48012077e0788eb02f4f8ee458fef3601873fec6c998e9ea8b4554394e705a3"}, - {file = "click_aliases-1.0.1-py2.py3-none-any.whl", hash = "sha256:229ecab12a97d1d5ce3f1fd7ce16da0e4333a24ebe3b34d8b7a6d0a1d2cfab90"}, -] - -[package.dependencies] -click = "*" - -[package.extras] -dev = ["coveralls", "flake8", "flake8-import-order", "pytest", "pytest-cov", "tox-travis", "wheel"] - [[package]] name = "colorama" version = "0.4.6" @@ -472,4 +454,4 @@ files = [ [metadata] lock-version = "2.0" python-versions = "^3.11" -content-hash = "a43a4bcdb0e2d68bafdc807dc29580542303f669a34030a1e4e6691475061fe5" +content-hash = "cf6cdb8edb224ea506d64c7c05f5768d0d745c98182649f21f0ca227cc954cc1" diff --git a/pyproject.toml b/pyproject.toml index 34191ad..164e75e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "memos-cli" -version = "0.1.0" +version = "0.1.1" description = "A CLI frontend for memos self-hosted memo hub" authors = ["Giulio De Matteis "] readme = "README.md" @@ -14,7 +14,6 @@ packages = [ python = "^3.11" click = "^8.1.3" requests = "^2.30.0" -click-aliases = "^1.0.1" prettytable = "^3.7.0" [tool.poetry.scripts]