-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add shell completion script + entrypoint
- Loading branch information
1 parent
a62ca57
commit 7ec403d
Showing
7 changed files
with
175 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
_judo_completion() { | ||
local IFS=$'\n' | ||
local response | ||
|
||
response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _JUDO_COMPLETE=bash_complete $1) | ||
|
||
for completion in $response; do | ||
IFS=',' read type value <<< "$completion" | ||
|
||
if [[ $type == 'dir' ]]; then | ||
COMREPLY=() | ||
compopt -o dirnames | ||
elif [[ $type == 'file' ]]; then | ||
COMREPLY=() | ||
compopt -o default | ||
elif [[ $type == 'plain' ]]; then | ||
COMPREPLY+=($value) | ||
fi | ||
done | ||
|
||
return 0 | ||
} | ||
|
||
_judo_completion_setup() { | ||
complete -o nosort -F _judo_completion judo | ||
} | ||
|
||
_judo_completion_setup; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
function _judo_completion; | ||
set -l response; | ||
|
||
for value in (env _JUDO_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) judo); | ||
set response $response $value; | ||
end; | ||
|
||
for completion in $response; | ||
set -l metadata (string split "," $completion); | ||
|
||
if test $metadata[1] = "dir"; | ||
__fish_complete_directories $metadata[2]; | ||
else if test $metadata[1] = "file"; | ||
__fish_complete_path $metadata[2]; | ||
else if test $metadata[1] = "plain"; | ||
echo $metadata[2]; | ||
end; | ||
end; | ||
end; | ||
|
||
complete --no-files --command judo --arguments "(_judo_completion)"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#compdef judo | ||
|
||
_judo_completion() { | ||
local -a completions | ||
local -a completions_with_descriptions | ||
local -a response | ||
(( ! $+commands[judo] )) && return 1 | ||
|
||
response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _JUDO_COMPLETE=zsh_complete judo)}") | ||
|
||
for type key descr in ${response}; do | ||
if [[ "$type" == "plain" ]]; then | ||
if [[ "$descr" == "_" ]]; then | ||
completions+=("$key") | ||
else | ||
completions_with_descriptions+=("$key":"$descr") | ||
fi | ||
elif [[ "$type" == "dir" ]]; then | ||
_path_files -/ | ||
elif [[ "$type" == "file" ]]; then | ||
_path_files -f | ||
fi | ||
done | ||
|
||
if [ -n "$completions_with_descriptions" ]; then | ||
_describe -V unsorted completions_with_descriptions -U | ||
fi | ||
|
||
if [ -n "$completions" ]; then | ||
compadd -U -V unsorted -a completions | ||
fi | ||
} | ||
|
||
compdef _judo_completion judo; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,4 @@ | ||
from os.path import dirname, abspath | ||
|
||
package_path = abspath(dirname(__file__)) | ||
completions_path = dirname(dirname(package_path)) + "/completions" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import pathlib | ||
import subprocess | ||
from functools import partial | ||
from shutil import copy | ||
|
||
import click | ||
|
||
from judoka import completions_path | ||
|
||
|
||
def rc_installer(rc_file, completion_source): | ||
judoka_homedir = pathlib.Path("~/.judo").expanduser() | ||
judoka_homedir.mkdir(exist_ok=True) | ||
|
||
completion_target = copy( | ||
pathlib.Path(completions_path) / completion_source, | ||
judoka_homedir, | ||
) | ||
|
||
return f"echo 'source {completion_target}' >> {rc_file}" | ||
|
||
|
||
def path_installer(install_path, completion_source): | ||
install_path.mkdir(exist_ok=True) | ||
copy( | ||
pathlib.Path(completions_path) / completion_source, | ||
install_path, | ||
) | ||
|
||
|
||
installers = { | ||
"bash": partial( | ||
rc_installer, | ||
rc_file=pathlib.Path("~/.bashrc").expanduser(), | ||
completion_source="judo.bash", | ||
), | ||
"zsh": partial( | ||
rc_installer, | ||
rc_file=pathlib.Path("~/.zshrc").expanduser(), | ||
completion_source="judo.zsh", | ||
), | ||
"fish": partial( | ||
path_installer, | ||
install_path=pathlib.Path("~/.config/fish/completions/").expanduser(), | ||
completion_source="judo.fish", | ||
), | ||
} | ||
|
||
|
||
@click.command() | ||
@click.argument("shell", type=click.Choice(["bash", "fish", "zsh"])) | ||
@click.option("--apply/--no-apply", default=False) | ||
def install(shell, apply=False): | ||
installer = installers.get(shell) | ||
|
||
if not installer: | ||
click.echo(f"No installer found for shell '{shell}'") | ||
exit(1) | ||
|
||
instruction = installer() | ||
if instruction: | ||
if not apply: | ||
click.echo("\nTo complete installation, execute the following command:\n") | ||
click.echo(instruction) | ||
click.echo("\nAlternatively, run with --force to automate installation.\n") | ||
else: | ||
subprocess.run(instruction, shell=True) | ||
click.echo(f"Shell completion was installed for you via: \n{instruction}") | ||
else: | ||
click.echo("Shell completion was installed") | ||
|
||
|
||
if __name__ == "__main__": | ||
install() |