Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Added autocompletion. (#73)
Browse files Browse the repository at this point in the history
* Added autocompletion.

* 'Refactored by Sourcery' (#74)

Co-authored-by: Sourcery AI <>

Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
ggirelli and sourcery-ai[bot] committed Oct 6, 2021
1 parent daac6ed commit 03ecb79
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 2 deletions.
29 changes: 29 additions & 0 deletions ifpd2/autocomplete/.ifpd2-complete.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
_ifpd2_completion() {
local IFS=$'\n'
local response

response=$(env COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD=$COMP_CWORD _IFPD2_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
}

_ifpd2_completion_setup() {
complete -o nosort -F _ifpd2_completion ifpd2
}

_ifpd2_completion_setup;

22 changes: 22 additions & 0 deletions ifpd2/autocomplete/.ifpd2-complete.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function _ifpd2_completion;
set -l response;

for value in (env _IFPD2_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) ifpd2);
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 ifpd2 --arguments "(_ifpd2_completion)";

35 changes: 35 additions & 0 deletions ifpd2/autocomplete/.ifpd2-complete.zsh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#compdef ifpd2

_ifpd2_completion() {
local -a completions
local -a completions_with_descriptions
local -a response
(( ! $+commands[ifpd2] )) && return 1

response=("${(@f)$(env COMP_WORDS="${words[*]}" COMP_CWORD=$((CURRENT-1)) _IFPD2_COMPLETE=zsh_complete ifpd2)}")

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 _ifpd2_completion ifpd2;

5 changes: 3 additions & 2 deletions ifpd2/scripts/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
@contact: gigi.ga90@gmail.com
"""

from ifpd2.scripts import autocomplete, ifpd2
from ifpd2.scripts import db, extract_kmers
from ifpd2.scripts import ifpd2
from ifpd2.scripts import query, query2

import logging
Expand All @@ -17,9 +17,10 @@
)

__all__ = [
"autocomplete",
"ifpd2",
"db",
"extract_kmers",
"ifpd2",
"query",
"query2",
]
84 changes: 84 additions & 0 deletions ifpd2/scripts/autocomplete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
@author: Gabriele Girelli
@contact: gigi.ga90@gmail.com
"""

import click # type: ignore
import os
from rich import print # type: ignore
from shutil import copyfile
import sys

from ifpd2 import __path__, __version__
from ifpd2.const import CONTEXT_SETTINGS


@click.command(context_settings=CONTEXT_SETTINGS)
@click.option(
"--shell-type",
"-s",
help="shell type for which to activate autocompletion",
type=click.Choice(["bash", "zsh", "fish"], case_sensitive=False),
default="bash",
show_default=True,
)
@click.option(
"--regenerate",
help="to regenerate autocompletion file, mainly for developers",
type=click.BOOL,
default=False,
is_flag=True,
)
@click.version_option(__version__)
def main(shell_type: str, regenerate: bool) -> None:
user_home_path = os.path.expanduser("~")
autocomplete_path = os.path.join(
__path__[0], "autocomplete", f".ifpd2-complete.{shell_type}"
)

if regenerate:
regenerate_autocompletion_files(shell_type, autocomplete_path)

if shell_type in {"bash", "zsh"}:
autocomplete_bash_or_zsh(user_home_path, autocomplete_path, shell_type)
elif shell_type == "fish":
autocomplete_fish(user_home_path, autocomplete_path)

print("Done. :thumbs_up: :smiley:")


def regenerate_autocompletion_files(shell_type: str, autocomplete_path: str) -> None:
os.system(f"_IFPD2_COMPLETE={shell_type}_source ifpd2 > {autocomplete_path}")
print(f"Regenerated {shell_type} completion file: {autocomplete_path}")


def autocomplete_fish(user_home_path: str, autocomplete_path: str) -> None:
destination_path = os.path.join(
user_home_path, ".config/fish/completions/ifpd2.fish"
)

if os.path.isfile(destination_path):
print("Autocompletion was previously set up. Skipping.")
sys.exit()

copyfile(
autocomplete_path,
destination_path,
)


def autocomplete_bash_or_zsh(
user_home_path: str, autocomplete_path: str, shell_type: str = "bash"
) -> None:
assert shell_type in {"bash", "zsh"}

autocompletion_string = f". {autocomplete_path} # IFPD2-AUTOCOMPLETE\n"
run_command_path = os.path.join(user_home_path, f".{shell_type}rc")

with open(run_command_path, "r") as OH:
if autocompletion_string in OH.readlines():
print("Autocompletion was previously set up. Skipping.")
sys.exit()

with open(run_command_path, "a+") as OH:
OH.write(f"\n{autocompletion_string}")
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,4 @@ build-backend = "poetry.masonry.api"

[tool.poetry.scripts]
"ifpd2" = "ifpd2.scripts.ifpd2:main"
"ifpd2-autocomplete" = "ifpd2.scripts.autocomplete:main"

0 comments on commit 03ecb79

Please sign in to comment.