Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for shell completions #4

Merged
merged 4 commits into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
},
Expand Down
6 changes: 6 additions & 0 deletions src/termvisage/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
56 changes: 56 additions & 0 deletions src/termvisage/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}`"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down