From 03ecb7970e29e7637cc7d6d15a97268535d09641 Mon Sep 17 00:00:00 2001 From: Gabriele Girelli Date: Wed, 6 Oct 2021 16:48:47 +0200 Subject: [PATCH] Added autocompletion. (#73) * 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> --- ifpd2/autocomplete/.ifpd2-complete.bash | 29 +++++++++ ifpd2/autocomplete/.ifpd2-complete.fish | 22 +++++++ ifpd2/autocomplete/.ifpd2-complete.zsh | 35 +++++++++++ ifpd2/scripts/__init__.py | 5 +- ifpd2/scripts/autocomplete.py | 84 +++++++++++++++++++++++++ pyproject.toml | 1 + 6 files changed, 174 insertions(+), 2 deletions(-) create mode 100644 ifpd2/autocomplete/.ifpd2-complete.bash create mode 100644 ifpd2/autocomplete/.ifpd2-complete.fish create mode 100644 ifpd2/autocomplete/.ifpd2-complete.zsh create mode 100644 ifpd2/scripts/autocomplete.py diff --git a/ifpd2/autocomplete/.ifpd2-complete.bash b/ifpd2/autocomplete/.ifpd2-complete.bash new file mode 100644 index 00000000..f045cfc9 --- /dev/null +++ b/ifpd2/autocomplete/.ifpd2-complete.bash @@ -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; + diff --git a/ifpd2/autocomplete/.ifpd2-complete.fish b/ifpd2/autocomplete/.ifpd2-complete.fish new file mode 100644 index 00000000..075e65ff --- /dev/null +++ b/ifpd2/autocomplete/.ifpd2-complete.fish @@ -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)"; + diff --git a/ifpd2/autocomplete/.ifpd2-complete.zsh b/ifpd2/autocomplete/.ifpd2-complete.zsh new file mode 100644 index 00000000..b68c1464 --- /dev/null +++ b/ifpd2/autocomplete/.ifpd2-complete.zsh @@ -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; + diff --git a/ifpd2/scripts/__init__.py b/ifpd2/scripts/__init__.py index 7cb98673..0a89b35e 100644 --- a/ifpd2/scripts/__init__.py +++ b/ifpd2/scripts/__init__.py @@ -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 @@ -17,9 +17,10 @@ ) __all__ = [ + "autocomplete", + "ifpd2", "db", "extract_kmers", - "ifpd2", "query", "query2", ] diff --git a/ifpd2/scripts/autocomplete.py b/ifpd2/scripts/autocomplete.py new file mode 100644 index 00000000..1fc5bdc1 --- /dev/null +++ b/ifpd2/scripts/autocomplete.py @@ -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}") diff --git a/pyproject.toml b/pyproject.toml index 5539c742..c05daca6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,3 +45,4 @@ build-backend = "poetry.masonry.api" [tool.poetry.scripts] "ifpd2" = "ifpd2.scripts.ifpd2:main" +"ifpd2-autocomplete" = "ifpd2.scripts.autocomplete:main"