This repository has been archived by the owner on Mar 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
1 parent
daac6ed
commit 03ecb79
Showing
6 changed files
with
174 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
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; | ||
|
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 _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)"; | ||
|
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 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; | ||
|
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,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}") |
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