From d5a2a766df9c5c3d653608e3f695a0436a288dc8 Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 9 May 2023 16:32:38 +0100 Subject: [PATCH 1/4] chore: Add `argcomplete` dependency --- requirements.txt | 1 + setup.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index 0e1963a..38ca159 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +argcomplete==3.0.8 black==22.12.0 flake8==5.0.4 isort[colors]==5.12.0;python_version>="3.8" diff --git a/setup.py b/setup.py index 480be90..3be61b5 100644 --- a/setup.py +++ b/setup.py @@ -34,7 +34,7 @@ license="MIT", classifiers=classifiers, python_requires=">=3.7", - install_requires=["term-image>=0.6,<0.7", "urwid>=2.1,<3.0"], + install_requires=["argcomplete>=2,<4", "term-image>=0.6,<0.7", "urwid>=2.1,<3.0"], entry_points={ "console_scripts": ["termvisage=termvisage.__main__:main"], }, From ca610e7b9ccc2dbeab771ade684c0b05e18027e4 Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 9 May 2023 16:35:23 +0100 Subject: [PATCH 2/4] feat: Add `--completions` CL option - Add: `--completions` command-line option. - Add: `COMPLETIONS` and `CompletionsAction` in `.parsers`. --- src/termvisage/parsers.py | 56 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/termvisage/parsers.py b/src/termvisage/parsers.py index 304ccc3..c22b73c 100644 --- a/src/termvisage/parsers.py +++ b/src/termvisage/parsers.py @@ -15,12 +15,56 @@ from . import cli # noqa: F401; prevents circular import of `.config` (below) from . import __version__ +COMPLETIONS = """First, if `termvisage` was installed using `pipx`, +run the following (outside any virtual environment): + + pip install --user --upgrade argcomplete + +Then, follow the appropriate instructions for your shell: + +NOTE: If you add a command to your shell's config file, you will likely +have to restart the shell or re-login for autocompletion to start working. + +Bash or Zsh: + Add the following to your shell's config file: + + eval "$(register-python-argcomplete termvisage)" + +Tcsh: + Add the following to your shell's config file: + + eval "$(register-python-argcomplete --shell tcsh termvisage)" + +Fish: + Run the following once to create new completion file: + + register-python-argcomplete --shell fish termvisage \ +> ~/.config/fish/completions/termvisage.fish + + OR add the following to your shell's config file: + + register-python-argcomplete --shell fish termvisage | source + +Git Bash: + Add the following to your shell's config file: + + export ARGCOMPLETE_USE_TEMPFILES=1 + eval "$(register-python-argcomplete termvisage)" + +For other shells, see https://github.com/kislyuk/argcomplete/tree/develop/contrib +""" + class BasicHelpAction(Action): def __call__(self, *args): basic_parser.parse_args(["--help"]) +class CompletionsAction(Action): + def __call__(self, parser, *args): + parser.exit(message=COMPLETIONS) + + def rst_role_repl(match): if match.group(1) in {"option", "confval"}: return f"`{match.group(2)}`" @@ -87,6 +131,12 @@ def strip_markup(string: str) -> str: version=__version__, help="Show the program version and exit", ) +basic_parser.add_argument( + "--completions", + nargs=0, + action=CompletionsAction, + help="Show instructions to enable shell completions and exit", +) basic_parser.add_argument( "-S", "--style", @@ -180,6 +230,12 @@ def strip_markup(string: str) -> str: version=__version__, help="Show the program version and exit", ) +general.add_argument( + "--completions", + nargs=0, + action=CompletionsAction, + help="Show instructions to enable shell completions and exit", +) general.add_argument( "--query-timeout", type=float, From 59120965d33febbdf885c3a3784e54634ad07cd7 Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 9 May 2023 16:38:57 +0100 Subject: [PATCH 3/4] feat: Implement support for shell completion --- src/termvisage/__main__.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/termvisage/__main__.py b/src/termvisage/__main__.py index 714195b..ab32472 100644 --- a/src/termvisage/__main__.py +++ b/src/termvisage/__main__.py @@ -14,6 +14,12 @@ def main() -> int: """CLI execution entry-point""" + from argcomplete import autocomplete + + from .parsers import parser + + autocomplete(parser) + from . import cli, logging, notify from .tui import main From 9c0689b2347ac8ccd1ae596b824ca18986ce9f2e Mon Sep 17 00:00:00 2001 From: AnonymouX47 Date: Tue, 9 May 2023 16:52:38 +0100 Subject: [PATCH 4/4] chore: Update CHANGELOG for #4 --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d592b80..feaa5af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,11 +25,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [tui] File name labels to image grid cells ([e64edd4]). - [tui] Support for terminal synchronized output ([ad059bb]). - [cli] `--long-help` command-line option for full help message ([d5852e6]). +- [cli] Support for shell completions ([#4]). + - [argcomplete](https://github.com/kislyuk/argcomplete)>=2,<4 dependency + - `--completions` command-line option ### Removed - As much private API usage across the CLI and TUI code ([term-image#70]). [term-image#70]: https://github.com/AnonymouX47/term-image/pull/70 +[#4]: https://github.com/AnonymouX47/termvisage/pull/4 [term-image@b4533d5]: https://github.com/AnonymouX47/term-image/commit/b4533d5697d41fe0742c2ac895077da3b8d889dc [term-image@8b0af4c]: https://github.com/AnonymouX47/term-image/pull/70/commits/8b0af4cd76c96187b95237e7bcd74ab5b16b2c82 [1637a38]: https://github.com/AnonymouX47/termvisage/commit/1637a388affef84735805ac105b995cb2f25c005