Skip to content

Commit

Permalink
Support tab completion on first run
Browse files Browse the repository at this point in the history
  • Loading branch information
amalthundiyil committed Jun 9, 2022
1 parent 74530c6 commit ab1047c
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 20 deletions.
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ vendor-install:
-$(RM) $(vendor-install)
$(MAKE) $(vendor-install)

# Tab completion scripts

completer-script = $(VIRTUAL_ENV)/bin/completer

.PHONY: completer
completer:
_KART_COMPLETE=bash_source $(kart-app-any) > $(completer-script).bash
_KART_COMPLETE=zsh_source $(kart-app-any) > $(completer-script).zsh
_KART_COMPLETE=fish_source $(kart-app-any) > $(completer-script).fish

# Install Python (just release) dependencies
.PHONY: py-deps
py-deps: $(vendor-install) $(py-install-main) | $(VIRTUAL_ENV)
Expand All @@ -155,9 +165,11 @@ $(kart-app-release): py-deps setup.py kart | $(VIRTUAL_ENV)
-$(RM) dist/*
python3 setup.py sdist
pip install --force-reinstall --no-deps dist/*.tar.gz
$(MAKE) completer

$(kart-app-dev): py-deps-dev setup.py | $(VIRTUAL_ENV)
pip install --force-reinstall --no-deps -e .
$(MAKE) completer

$(kart-app-any):
$(MAKE) $(kart-app-release)
Expand Down
26 changes: 26 additions & 0 deletions kart/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import subprocess
import sys
import traceback
from os.path import expanduser

import click
import pygit2
Expand All @@ -17,6 +18,7 @@
from .cli_util import add_help_subcommand, call_and_exit_flag, tool_environment
from .context import Context
from .exec import execvp
from kart import prefix

MODULE_COMMANDS = {
"annotations.cli": {"build-annotations"},
Expand Down Expand Up @@ -372,6 +374,30 @@ def _format_exc_tb():
return module.main(ctx=ctx, args=args)


@cli.command()
@click.option(
"--file",
"file",
type=click.Path(file_okay=True, dir_okay=False),
help="File for shell configuration",
)
@click.argument("shell", type=click.Choice(["zsh", "bash", "fish"]))
def completer(file, shell):
"""Get autocompletion script for kart"""
script = f"{prefix}/bin/completer.{shell}"
shell_conf = {
"zsh": f"{expanduser('~')}/.zshrc",
"bash": f"{expanduser('~')}/.bashrc",
"fish": f"{expanduser('~')}/.config/fish/config.fish",
}
shell_conf[shell] = file if file else shell_conf[shell]

with open(shell_conf[shell], "a") as f:
_ = f.write(f"\n# kart autocompletion\nsource {script}\n")

click.echo(f"Successfully added autocompletion for {shell}.")


def _hackily_parse_command(args, skip_first_arg=True):
ignore_next = skip_first_arg
for arg in args:
Expand Down
9 changes: 5 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ attrs==21.4.0
# via jsonschema
cached-property==1.5.2
# via pygit2
certifi==2021.10.8
certifi==2022.5.18.1
# via -r requirements/requirements.in
cffi==1.15.0
# via
# cryptography
# pygit2
click==7.1.2
click==8.1.3
# via -r requirements/requirements.in
cryptography==37.0.2
# via -r requirements/requirements.in
greenlet==1.1.2
# via sqlalchemy
importlib-metadata==4.11.3
importlib-metadata==4.11.4
# via
# click
# jsonschema
# sqlalchemy
jsonschema==4.1.2
Expand All @@ -44,7 +45,7 @@ pyrsistent==0.18.1
# via jsonschema
rtree==0.9.7
# via -r requirements/requirements.in
sqlalchemy==1.4.36
sqlalchemy==1.4.37
# via -r requirements/requirements.in
typing-extensions==4.2.0
# via importlib-metadata
Expand Down
14 changes: 7 additions & 7 deletions requirements/dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ decorator==5.1.1
# ipython
execnet==1.9.0
# via pytest-xdist
importlib-metadata==4.11.3
importlib-metadata==4.11.4
# via
# -c requirements/../requirements.txt
# -c requirements/docs.txt
Expand Down Expand Up @@ -76,27 +76,27 @@ pygments==2.12.0
# -c requirements/../requirements.txt
# -c requirements/docs.txt
# ipython
pyparsing==3.0.8
pyparsing==3.0.9
# via
# -c requirements/docs.txt
# -c requirements/test.txt
# packaging
pytest-forked==1.4.0
# via pytest-xdist
pytest-xdist==2.5.0
# via -r requirements/dev.in
pytest==7.1.2
# via
# -c requirements/test.txt
# pytest-forked
# pytest-xdist
pytest-forked==1.4.0
# via pytest-xdist
pytest-xdist==2.5.0
# via -r requirements/dev.in
toml==0.10.2
# via ipdb
tomli==2.0.1
# via
# -c requirements/test.txt
# pytest
traitlets==5.1.1
traitlets==5.2.2.post1
# via
# ipython
# matplotlib-inline
Expand Down
10 changes: 5 additions & 5 deletions requirements/docs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ alabaster==0.7.12
# via sphinx
babel==2.10.1
# via sphinx
certifi==2021.10.8
certifi==2022.5.18.1
# via
# -c requirements/../requirements.txt
# requests
Expand All @@ -26,7 +26,7 @@ idna==3.3
# via requests
imagesize==1.3.0
# via sphinx
importlib-metadata==4.11.3
importlib-metadata==4.11.4
# via
# -c requirements/../requirements.txt
# -c requirements/test.txt
Expand All @@ -45,13 +45,13 @@ pygments==2.12.0
# via
# -c requirements/../requirements.txt
# sphinx
pyparsing==3.0.8
pyparsing==3.0.9
# via
# -c requirements/test.txt
# packaging
pytz==2022.1
# via babel
requests==2.27.1
requests==2.28.0
# via sphinx
six==1.16.0
# via
Expand All @@ -63,7 +63,7 @@ sphinx-autobuild==2021.3.14
# via -r requirements/docs.in
sphinx-rtd-theme==1.0.0
# via -r requirements/docs.in
sphinx==4.5.0
sphinx==5.0.1
# via
# sphinx-autobuild
# sphinx-rtd-theme
Expand Down
2 changes: 1 addition & 1 deletion requirements/requirements.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
certifi
Click~=7.0
Click~=8.1
cryptography
msgpack~=0.6.1
pymysql
Expand Down
6 changes: 3 additions & 3 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ attrs==21.4.0
# pytest
colorama==0.4.4
# via -r requirements/test.in
coverage[toml]==6.3.2
coverage[toml]==6.4.1
# via pytest-cov
fields==5.0.0
# via aspectlib
gprof2dot==2021.2.21
# via pytest-profiling
html5lib==1.1
# via -r requirements/test.in
importlib-metadata==4.11.3
importlib-metadata==4.11.4
# via
# -c requirements/../requirements.txt
# pluggy
Expand All @@ -39,7 +39,7 @@ py-cpuinfo==8.0.0
# via pytest-benchmark
py==1.11.0
# via pytest
pyparsing==3.0.8
pyparsing==3.0.9
# via packaging
pytest-benchmark[aspect]==3.4.1
# via -r requirements/test.in
Expand Down

0 comments on commit ab1047c

Please sign in to comment.