Skip to content

Commit

Permalink
ENH: easier tab completion activation: source tab-qiime (#84)
Browse files Browse the repository at this point in the history
Removes the ugly and hard-to-remember `eval "$(register-qiime-completion 2> /dev/null)"` command in favor of `source tab-qiime`. Also enables tab completion out of the box for a fresh q2cli install once users run this command.

Thanks @thermokarst and @gregcaporaso for your input!
  • Loading branch information
jairideout authored and thermokarst committed Oct 7, 2016
1 parent d0bf53b commit aad9d67
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ install:
script:
- QIIMETEST= nosetests
- flake8 q2cli setup.py
- eval "$(register-qiime-completion 2> /dev/null)"
- source tab-qiime
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ To get help with QIIME 2, visit http://2.qiime.org.
To enable tab completion in Bash, run the following command or add it to your `.bashrc`/`.bash_profile`:

```bash
eval "$(register-qiime-completion 2> /dev/null)"
source tab-qiime
```
45 changes: 0 additions & 45 deletions bin/register-qiime-completion

This file was deleted.

54 changes: 54 additions & 0 deletions bin/tab-qiime
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash

# Bash completion script that defers to a cached completion script representing
# the state of the current QIIME 2 deployment.
#
# This script is intended to be executed on the command-line or in
# .bashrc/.bash_profile:
#
# source tab-qiime
#

_qiime_completion()
{
# Attempt to find the cached completion script. If q2cli isn't installed, or
# is an incompatible version, don't attempt completion.
local path="$(python -c "import q2cli.util; print(q2cli.util.get_completion_path())" 2> /dev/null)"

if [[ $? != 0 ]]; then
unset COMPREPLY
return 0
fi

# If the completion script exists, attempt completion by invoking the script
# in a subshell, supplying COMP_WORDS and COMP_CWORD. Capture the output as
# the completion reply. If the completion script failed, don't attempt
# completion.
if [[ -f "$path" ]] ; then
COMPREPLY=( $(COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD="${COMP_CWORD}" "$path" 2> /dev/null) )

if [[ $? != 0 ]]; then
unset COMPREPLY
return 0
fi
else
unset COMPREPLY
return 0
fi

return 0
}

# Enable default readline and bash completion behavior when `_qiime_completion`
# doesn't have a reply.
complete -F _qiime_completion -o default -o bashdefault qiime

# Execute a `qiime` command (any command will do) so that tab-completion will
# work out-of-the-box (e.g. with a fresh installation of q2cli). Running a
# command will create or refresh the cache if necessary, which contains the
# actual completion script.
#
# Ignore stdout to avoid displaying help text to users enabling tab-completion.
# stderr displays the note about cache refreshing, as that can take a few
# moments to complete.
qiime > /dev/null
2 changes: 1 addition & 1 deletion q2cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
To enable tab completion in Bash, run the following command or add it to your \
.bashrc/.bash_profile:
eval "$(register-qiime-completion 2> /dev/null)"
source tab-qiime
"""

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
packages=find_packages(),
include_package_data=True,
install_requires=['click', 'qiime >= 2.0.2', 'pip'],
scripts=['bin/register-qiime-completion'],
scripts=['bin/tab-qiime'],
entry_points='''
[console_scripts]
qiime=q2cli.__main__:qiime
Expand Down

0 comments on commit aad9d67

Please sign in to comment.