Skip to content
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
6 changes: 6 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ jobs:
run: |
pytest tests/test_activate_shells.py -v

- name: Run shell completion tests
env:
NODEENV_REQUIRE_SHELLS: '1'
run: |
pytest tests/test_completion.py -v

# The only job that builds real node environments: the tests marked
# `integration` are deselected everywhere else. Deliberately ubuntu
# only - the install path is also platform specific, so macOS and
Expand Down
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ Nodeenv changelog
Version [unreleased]
--------------------

- `--print-completion SHELL` prints a completion script for bash, zsh or
fish. It needs shtab, installed with `pip install "nodeenv[completion]"`
`#351 <https://github.com/ekalinin/nodeenv/issues/351>`_
- `--prebuilt` installs no longer fail when the environment path or the
node.js version contains `[`, `*` or `?`
`#441 <https://github.com/ekalinin/nodeenv/issues/441>`_
Expand Down
44 changes: 44 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,46 @@ In an unactivated PowerShell environment, use
.. _pip: https://pypi.org/project/pip/
.. _pipx: https://pipx.pypa.io/

Shell completion
^^^^^^^^^^^^^^^^

``nodeenv --print-completion SHELL`` prints a completion script for bash,
zsh or fish. It needs `shtab`_, which the ``completion`` extra installs::

$ python -m pip install "nodeenv[completion]"

With pipx, add shtab to nodeenv's environment instead::

$ pipx inject nodeenv shtab

Then save the script where your shell loads it from.

bash::

$ mkdir -p ~/.local/share/bash_completion
$ nodeenv --print-completion bash > ~/.local/share/bash_completion/nodeenv
$ echo 'source ~/.local/share/bash_completion/nodeenv' >> ~/.bashrc

zsh, as ``_nodeenv`` in a directory on ``fpath``::

$ mkdir -p ~/.zfunc
$ nodeenv --print-completion zsh > ~/.zfunc/_nodeenv

and in ``~/.zshrc``, before ``compinit`` runs::

fpath=(~/.zfunc $fpath)

Instead of the file, ``~/.zshrc`` can run
``eval "$(nodeenv --print-completion zsh)"`` after ``compinit``, at the
cost of starting nodeenv in every new shell. Sourcing the script does not
work in zsh.

fish::

$ nodeenv --print-completion fish > ~/.config/fish/completions/nodeenv.fish

.. _shtab: https://pypi.org/project/shtab/


Dependency
----------
Expand Down Expand Up @@ -478,6 +518,10 @@ Other options

$ SSL_CERT_FILE=$(python -c 'import certifi; print(certifi.where())') nodeenv env

``--print-completion=SHELL``
Print the completion script for ``bash``, ``zsh`` or ``fish`` and
exit. Needs shtab, see `Shell completion`_.

``--version``
Show program version and exit.

Expand Down
47 changes: 47 additions & 0 deletions nodeenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,47 @@ def emit(self, record):
logger = create_logger()


class PrintCompletionAction(argparse.Action):
"""
Print the shell completion script made by shtab and exit.

shtab is imported only here, so nodeenv works without it.
"""
# options completed with file and with directory names
file_dests = ('requirements', 'local_requirements', 'config_file',
'make_path')
dir_dests = ('python_virtualenv', 'env_dir')

def __call__(self, parser, namespace, values, option_string=None):
try:
import shtab
except ImportError:
parser.error('shtab is not installed, run: '
'pip install "nodeenv[completion]"')
if values not in shtab.SUPPORTED_SHELLS or not hasattr(shtab, 'cmd'):
parser.error('the installed shtab cannot complete %s, run: '
'pip install -U "nodeenv[completion]"' % values)

node_versions = 'echo latest; echo lts'
if not is_WIN:
# system node.js is not supported on Windows
node_versions += '; echo system'
for action in parser._actions:
if action.dest in self.file_dests:
action.complete = shtab.FILE
elif action.dest in self.dir_dests:
action.complete = shtab.DIRECTORY
elif action.dest == 'node':
action.complete = shtab.cmd(node_versions)
elif action.dest == 'npm':
action.complete = shtab.cmd('echo latest')

# complete `nodeenv`, whatever command line started this run
parser.prog = 'nodeenv'
print(shtab.complete(parser, values))
parser.exit()


def make_parser():
"""
Make a command line argument parser.
Expand All @@ -432,6 +473,12 @@ def make_parser():
parser.add_argument(
'--version', action='version', version=nodeenv_version)

parser.add_argument(
'--print-completion', action=PrintCompletionAction,
choices=('bash', 'zsh', 'fish'), metavar='SHELL',
help='Print the completion script for SHELL (bash, zsh or fish) '
'and exit. Needs shtab: pip install "nodeenv[completion]"')

parser.add_argument(
'-n', '--node', dest='node', metavar='NODE_VER', default=Config.node,
help='The node.js version to use, e.g., '
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-e .
-e .[completion]

coverage
flake8
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def read_file(file_name):
author='Eugene Kalinin',
author_email='e.v.kalinin@gmail.com',
install_requires=[],
extras_require={'certifi': ['certifi']},
extras_require={'certifi': ['certifi'], 'completion': ['shtab>=1.11']},
python_requires='>=3.10',
description="Node.js virtual environment builder",
long_description=ldesc,
Expand Down
Loading
Loading