Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better completion detection for ml and better fix completion.
- Loading branch information
Showing
2 changed files
with
57 additions
and
17 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 |
---|---|---|
@@ -1,38 +1,77 @@ | ||
_contains() | ||
{ | ||
local n=$# | ||
local value=${!n} | ||
for ((i=1;i < $#;i++)) { | ||
if [ "${!i}" == "${value}" ]; then | ||
return 0 | ||
fi | ||
} | ||
return 1 | ||
} | ||
|
||
|
||
_find_prev_command() | ||
{ | ||
local prev_command="" | ||
for word in ${COMP_WORDS[@]}; do | ||
_contains "${orders_with_args[@]}" "${word}" | ||
if [[ $? -eq 0 ]]; then | ||
prev_command="${word}" | ||
fi | ||
done | ||
echo "${prev_command}" | ||
} | ||
|
||
_fix_nodes() | ||
{ | ||
local running=$( (cd ./nodes; ls *.json) | sed -e "s/\.json$//") | ||
COMPREPLY=( | ||
$(compgen -S' ' -W "all" -- ${cur}) | ||
$(compgen -W "${running}" -- ${cur}) ) | ||
} | ||
|
||
_fix() | ||
{ | ||
local cur prev opts base | ||
local cur prev_command | ||
|
||
local orders_with_args=( | ||
node | ||
nodes_with_role | ||
recipe | ||
role | ||
list_nodes_with_recipe | ||
list_nodes_with_role | ||
plugin | ||
) | ||
|
||
COMPREPLY=() | ||
cur="${COMP_WORDS[COMP_CWORD]}" | ||
prev_command=$( _find_prev_command ) | ||
|
||
echo "${prev_command}" | ||
|
||
if [[ "${cur}" == ":" ]]; then | ||
# fix uses ':' to separate command from arguments. We don't want it to | ||
# be cur because compgen won't do the right thing. | ||
cur='' | ||
fi | ||
|
||
prev="${COMP_WORDS[COMP_CWORD-1]}" | ||
|
||
if [[ "${prev}" == ":" ]]; then | ||
# fix uses ':' to separate command from arguments. We don't want it to | ||
# be prev because the case statement below won't do the right thing. | ||
prev="${COMP_WORDS[COMP_CWORD-2]}:" | ||
fi | ||
|
||
case "$prev" in | ||
node:) | ||
# Should use fix list_nodes but striping ansi escape sequences is hard. | ||
local running=$(ls ./nodes/ | sed -e "s/\.json$//") | ||
COMPREPLY=( $(compgen -W "${running}" -- ${cur}) ) | ||
case "${prev_command}" in | ||
node) | ||
_fix_nodes | ||
return 0 | ||
;; | ||
*) | ||
;; | ||
esac | ||
|
||
local orders=$(fix -l | grep "^ " | cut -d' ' -f5) | ||
COMPREPLY=($(compgen -o nospace -W "${orders}" -- ${cur})) | ||
return 0 | ||
|
||
COMPREPLY=( | ||
$(compgen -W "${orders_with_args}" -S':' -- ${cur}) | ||
$(compgen -S' ' -W "${orders}" -- ${cur}) ) | ||
return 0 | ||
} | ||
|
||
complete -F _fix fix | ||
complete -o nospace -F _fix fix |
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