Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

re-enable autocomplete on click v7 #87

Merged
merged 1 commit into from
Feb 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions dapp_manager/autocomplete/scripts/.dapp-manager.bash
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@

_dapp_manager_completion() {
local IFS=$'\n'
local response

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

local IFS=$'
'
COMPREPLY=( $( env COMP_WORDS="${COMP_WORDS[*]}" \
COMP_CWORD=$COMP_CWORD \
_DAPP_MANAGER_COMPLETE=complete $1 ) )
return 0
}

complete -o nosort -F _dapp_manager_completion dapp-manager
_dapp_manager_completionsetup() {
local COMPLETION_OPTIONS=""
local BASH_VERSION_ARR=(${BASH_VERSION//./ })
# Only BASH version 4.4 and later have the nosort option.
if [ ${BASH_VERSION_ARR[0]} -gt 4 ] || ([ ${BASH_VERSION_ARR[0]} -eq 4 ] && [ ${BASH_VERSION_ARR[1]} -ge 4 ]); then
COMPLETION_OPTIONS="-o nosort"
fi

complete $COMPLETION_OPTIONS -F _dapp_manager_completion dapp-manager
}

_dapp_manager_completionsetup;
24 changes: 1 addition & 23 deletions dapp_manager/autocomplete/scripts/.dapp-manager.fish
Original file line number Diff line number Diff line change
@@ -1,23 +1 @@

function _dapp_manager_completion;
set -l response;

for value in (env _DAPP_MANAGER_COMPLETE=fish_complete COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) dapp-manager);
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 dapp-manager --arguments "(_dapp_manager_completion)";

complete --no-files --command dapp-manager --arguments "(env _DAPP_MANAGER_COMPLETE=complete_fish COMP_WORDS=(commandline -cp) COMP_CWORD=(commandline -t) dapp-manager)";
28 changes: 12 additions & 16 deletions dapp_manager/autocomplete/scripts/.dapp-manager.zsh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#compdef dapp-manager

_dapp_manager_completion() {
Expand All @@ -7,20 +6,17 @@ _dapp_manager_completion() {
local -a response
(( ! $+commands[dapp-manager] )) && return 1

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

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
response=("${(@f)$( env COMP_WORDS="${words[*]}" \
COMP_CWORD=$((CURRENT-1)) \
_DAPP_MANAGER_COMPLETE="complete_zsh" \
dapp-manager )}")

for key descr in ${(kv)response}; do
if [[ "$descr" == "_" ]]; then
completions+=("$key")
else
completions_with_descriptions+=("$key":"$descr")
fi
done

if [ -n "$completions_with_descriptions" ]; then
Expand All @@ -30,7 +26,7 @@ _dapp_manager_completion() {
if [ -n "$completions" ]; then
compadd -U -V unsorted -a completions
fi
compstate[insert]="automenu"
}

compdef _dapp_manager_completion dapp-manager;

55 changes: 27 additions & 28 deletions dapp_manager/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
from dapp_runner.log import LOG_CHOICES

from dapp_manager import DappManager
from dapp_manager.autocomplete import install_autocomplete
from dapp_manager.exceptions import DappManagerException
from dapp_manager.storage import RunnerReadFileType

# from dapp_manager.autocomplete import install_autocomplete


def _app_id_autocomplete(_ctx, _param, incomplete):
def _app_id_autocomplete(ctx, args, incomplete): # noqa
return [app_id for app_id in DappManager.list() if app_id.startswith(incomplete)]


def _with_app_id(wrapped_func):
wrapped_func = click.argument("app-id", type=str)( # shell_complete=_app_id_autocomplete)(
wrapped_func = click.argument("app-id", type=click.STRING, autocompletion=_app_id_autocomplete)(
wrapped_func
)
return wrapped_func
Expand Down Expand Up @@ -171,27 +170,27 @@ def read(app_id: str, file_type: RunnerReadFileType, ensure_alive: bool, follow:
print(dapp.read_file(file_type, ensure_alive=ensure_alive))


# @cli.command()
# @click.argument("shell", type=click.Choice(["bash", "fish", "zsh"]))
# @click.option(
# "--path",
# "-p",
# type=Path,
# default=None,
# help="Path to the file to which the shell completion function should be added.",
# )
# def autocomplete(shell: str, path: Path):
# """Enable CLI shell completion for the given shell.
#
# This command works by appending a pre-defined piece of shell code to the user's shell
# configuration file.
#
# The default target file will depend on the selected shell type (bash, fish or zsh):
# - bash: `~/.bashrc`
# - fish: `~/.config/fish/completions/{script_name}.fish`
# - zsh: `~/.zshrc`
# Use the `--path` flag to override the default target file.
#
# The command does nothing if the target file already contains the completion code.
# """
# install_autocomplete(shell, path)
@cli.command()
@click.argument("shell", type=click.Choice(["bash", "fish", "zsh"]))
@click.option(
"--path",
"-p",
type=Path,
default=None,
help="Path to the file to which the shell completion function should be added.",
)
def autocomplete(shell: str, path: Path):
"""Enable CLI shell completion for the given shell.

This command works by appending a pre-defined piece of shell code to the user's shell
configuration file.

The default target file will depend on the selected shell type (bash, fish or zsh):
- bash: `~/.bashrc`
- fish: `~/.config/fish/completions/{script_name}.fish`
- zsh: `~/.zshrc`
Use the `--path` flag to override the default target file.

The command does nothing if the target file already contains the completion code.
"""
install_autocomplete(shell, path)