Skip to content

Commit

Permalink
Merge pull request #33 from sloria/completion
Browse files Browse the repository at this point in the history
Add completion using click-completion
  • Loading branch information
sloria committed May 9, 2018
2 parents bcf2bff + 7130ebc commit 5eea895
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 6 deletions.
15 changes: 10 additions & 5 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -227,18 +227,23 @@ If you have `IPython <https://ipython.org/>`_ installed, you can run doitlive in
Only IPython>=5.0 is supported.
Bash completion
---------------
Shell completion
----------------
To enable bash completion, add the following to your ``.bashrc`` or ``.bash_profile``.
Shell completion is available for ``bash``, ``zsh``, and ``fish``.
For bash or zsh, add the following to your ``.bashrc`` or ``.zshrc``:
.. code-block:: bash
eval "$(_DOITLIVE_COMPLETE=source doitlive)"
eval "$(doitlive completion)"
For fish, add the following to ``~/.config/fish/completions/doitlive.fish``:
Completion is currently only supported for ``bash``.
.. code-block:: bash
eval (doitlive completion)
More
----
Expand Down
31 changes: 30 additions & 1 deletion doitlive/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from click import style, secho, getchar
from click.termui import strip_ansi
import click
import click_completion

from doitlive.termutils import raw_mode
from doitlive.version_control import (
Expand All @@ -42,6 +43,8 @@
__license__ = 'MIT'

env = os.environ
click_completion.init()

PY2 = int(sys.version[0]) == 2
if not PY2:
unicode = str
Expand Down Expand Up @@ -628,7 +631,6 @@ def cli():
"""
pass


def preview_themes():
secho('Theme previews:', bold=True)
echo()
Expand Down Expand Up @@ -656,6 +658,33 @@ def themes(preview, list):
else:
list_themes()

@cli.command()
def completion():
"""Output completion (to be eval'd).
For bash or zsh, add the following to your .bashrc or .zshrc:
eval "$(doitlive completion)"
For fish, add the following to ~/.config/fish/completions/doitlive.fish:
eval (doitlive completion)
"""
shell = env.get('SHELL', None)
if env.get('SHELL', None):
echo(
click_completion.get_code(
shell=shell.split(os.sep)[-1],
prog_name='doitlive'
)
)
else:
echo(
'Please ensure that the {SHELL} environment '
'variable is set.'.format(SHELL=style('SHELL', bold=True))
)
sys.exit(1)


QUIET_OPTION = click.option('--quiet', '-q', help='Suppress startup and ending message.',
is_flag=True, default=False, show_default=False)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

REQUIRES = [
'click>=4.0',
'click-completion>=0.3.1',
]

if 'win32' in str(sys.platform).lower():
Expand Down
13 changes: 13 additions & 0 deletions tests/test_doitlive.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,19 @@ def test_themes_preview(runner):
assert result1.output == result2.output


def test_completion(runner, monkeypatch):
monkeypatch.setitem(os.environ, 'SHELL', '/usr/local/bin/zsh')
result = runner.invoke(cli, ['completion'])
assert result.exit_code == 0


def test_completion_fails_if_SHELL_is_unset(runner, monkeypatch):
monkeypatch.delitem(os.environ, 'SHELL')
result = runner.invoke(cli, ['completion'])
assert result.exit_code > 0
msg = 'Please ensure that the SHELL environment variable is set.'
assert msg in result.output

def test_version(runner):
result = runner.invoke(cli, ['--version'])
assert doitlive.__version__ in result.output
Expand Down

0 comments on commit 5eea895

Please sign in to comment.