Skip to content

Commit

Permalink
Merge pull request #11 from gtalarico/autocomplete
Browse files Browse the repository at this point in the history
Added autocomplete helper, docs and gifs - Issue #10
  • Loading branch information
gtalarico committed May 15, 2018
2 parents 920c44b + ae7034c commit 710180f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 2 deletions.
63 changes: 63 additions & 0 deletions docs/completions.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
.. highlight:: console

================
Shell Completion
================

Pipes includes a helper ``--_completion`` flag which can be
used by terminals to provide autocomplete options.
Below are instructions for setting up autocompletion
for Bash, Zsh, and Fish.

If you have ideas for improving these please share them with use
over at the `Pipes repo <http://www.github.com/gtalarico/pipenv-pipes>`_

.. warning::

Pipes cannot activate an pipenv shell when one is already active,
thefore the autocompletion helper does not run either.
Make sure you are not inside a Pipenv Shell when trying to use
the autocomplete helper.

----------------------------

Fish
----

Add a new file ``pipes.fish`` to your Fish config folder
(eg. ``~/.config/fish/completions/pipes.fish``).

.. code:: console
complete --command pipes --arguments '(pipes --_completion (commandline -cp))' --no-files
.. image:: static/gif-autocomplete-fish.gif


----------------------------

Bash + Zsh
-----------

Original code kindly provided by `jonatasbaldin <https://github.com/jonatasbaldin>`_
`here <https://github.com/gtalarico/pipenv-pipes/issues/10>`_

Add the code below to your `.bashrc`:

.. code:: console
# .bashrc
export BASE_SHELL=$(basename $SHELL)
if [[ "$BASE_SHELL" == "zsh" ]] ; then
autoload bashcompinit && bashcompinit
fi
_pipenv-pipes_completions() {
COMPREPLY=($(compgen -W "$(pipes --_completion)" -- "${COMP_WORDS[1]}"))
}
complete -F _pipenv-pipes_completions pipes
.. image:: static/gif-autocomplete-bash.gif
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Pipenv Pipes Documentation
readme
installation
usage
completions
contributing
history

Expand Down
Binary file added docs/static/gif-autocomplete-bash.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/static/gif-autocomplete-fish.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 5 additions & 2 deletions pipenv_pipes/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@
help='Unlink Project Directory from this Environment')
@click.option('--verbose', '-v', is_flag=True, help='Verbose')
@click.option('--version', is_flag=True, help='Show Version')
@click.option('--_completion', is_flag=True)
@click.pass_context
def pipes(ctx, envname, list_, setlink, unlink, verbose, version):
def pipes(ctx, envname, list_, setlink, unlink, verbose, version, _completion):
"""
Pipes - PipEnv Environment Switcher
Expand Down Expand Up @@ -70,14 +71,16 @@ def pipes(ctx, envname, list_, setlink, unlink, verbose, version):

if env_vars.HAS_CURSES:
import curses # noqa flake8

ensure_env_vars_are_ok(env_vars)
environments = find_environments(env_vars.PIPENV_HOME)
if not environments:
click.echo(
'No pipenv environments found in {}'.format(env_vars.PIPENV_HOME))
sys.exit(1)

if _completion:
return [click.echo(e.envname) for e in environments]

if verbose:
click.echo('\nPIPENV_HOME: {}\n'.format(env_vars.PIPENV_HOME))

Expand Down
7 changes: 7 additions & 0 deletions tests/integration/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ def test_do_link_no_assoc_env(runner, temp_folder):
assert 'no virtualenv has been created' in result.output.lower()


def test_completions(runner):
result = runner.invoke(
pipes, args=['--_completion'], catch_exceptions=False)
assert result.exit_code == 0
assert 'proj1' in result.output


class TestEnsureVars():
""" Check for errors raised if Certain conditions are not met """

Expand Down

0 comments on commit 710180f

Please sign in to comment.