Skip to content

Switch to shtab for shell completion #135

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

Merged
merged 10 commits into from
Oct 9, 2021
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
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:

- name: Install developer dependencies
run: |
python3 -m pip install -U pip
python3 -m pip install -U pip setuptools
python3 -m pip install -U pytest pytest-runner flake8

- name: Install sphinx dependencies
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ If you are experiencing issues with *tldr*, consider deleting the cache files be

#### Autocomplete

`argcomplete` is required for autocompletion. See the `argcomplete` [docs](https://pypi.org/project/argcomplete/) for how to enable `argcomplete`. Cache will also need to be enabled and downloaded.
`shtab` is required for autocompletion using the `--print-completion` argument.

```bash
# bash
tldr --print-completion bash | sudo tee "$BASH_COMPLETION_COMPAT_DIR"/tldr
# zsh
tldr --print-completion zsh | sudo tee /usr/local/share/zsh/site-functions/_tldr
```

See the `shtab` [docs](https://pypi.org/project/shtab/#usage) for other installation methods.

### SSL Inspection

Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
termcolor
colorama
argcomplete
shtab>=1.3.10
14 changes: 11 additions & 3 deletions tldr.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from urllib.error import HTTPError, URLError
from termcolor import colored
import colorama # Required for Windows
import argcomplete
import shtab

__version__ = "2.0.0"
__client_specification__ = "1.4"
Expand Down Expand Up @@ -435,15 +435,23 @@ def create_parser():

parser.add_argument(
'command', type=str, nargs='*', help="command to lookup", metavar='command'
).completer = argcomplete.completers.ChoicesCompleter(get_commands())
).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"}

shtab.add_argument_to(parser, preamble={
'bash': r'''shtab_tldr_cmd_list(){{
compgen -W "$("{py}" -m tldr --list | sed 's/\W/ /g')" -- "$1"
}}'''.format(py=sys.executable),
'zsh': r'''shtab_tldr_cmd_list(){{
_describe 'command' "($("{py}" -m tldr --list | sed 's/\W/ /g'))"
}}'''.format(py=sys.executable)
})

return parser


def main():
parser = create_parser()

argcomplete.autocomplete(parser)
options = parser.parse_args()

colorama.init(strip=options.color)
Expand Down