Created
September 2, 2015 15:55
-
-
Save Lorentz83/cad24ca44b53e4a33626 to your computer and use it in GitHub Desktop.
bash completion scripts
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
_acd_cli() | |
{ | |
COMPREPLY=() | |
local CURR=${COMP_WORDS[COMP_CWORD]} | |
local PREV=${COMP_WORDS[COMP_CWORD-1]} | |
local COMMAND=${COMP_WORDS[1]} | |
local COMPLETEDARGNUM=$((COMP_CWORD - 2)) | |
_acd_cli_in_set() { # checks if $1 is equal to any of the following parameters | |
local TEST="$1" | |
while shift ; do | |
if [[ "$TEST" == "$1" ]] ; then | |
return 0; | |
fi | |
done | |
return 1; | |
} | |
_acd_cli_escape_string() # thanks to http://unix.stackexchange.com/a/32396 | |
{ | |
local qs=${1//\\//\\\\} | |
for c in \[ \] \( \) \. \^ \$ \? \* \+; do | |
qs=${qs//"$c"/"\\$c"} | |
done | |
echo $qs | |
} | |
_acd_cli_complete_remote() # $1 is a filter appended to the end of the grep command while listing the remote files | |
{ | |
local LIST CMD | |
if [ -n "$CURR" ] ; then | |
if echo "$CURR" | egrep -q '/$' ; then | |
LIST=$CURR | |
else | |
LIST=$(dirname "$CURR") | |
test "$LIST" == "/" || LIST="$LIST/" | |
fi | |
local FILTER=`_acd_cli_escape_string "$CURR"` | |
# commands in a pipe are executed in a subshell and don't modify the parent shell, DO NOT USE A PIPE | |
mapfile -t CMD < <(acd_cli ls "$LIST" 2>/dev/null | sed -e "s#^[^ ]* [^ ]* \(.*\)#$LIST\1#" 2>/dev/null | egrep "$FILTER.*$1" 2>/dev/null ) | |
else | |
CMD=("/") | |
fi | |
COMPREPLY=("${CMD[@]}") | |
compopt -o filenames | |
if [[ "${#COMPREPLY[@]}" -eq 1 && "${COMPREPLY[0]}" = */ ]] ; then | |
compopt -o nospace | |
fi | |
if [[ "${#COMPREPLY[@]}" -eq 0 && "${CURR}" = */ ]] ; then | |
COMPREPLY[0]="$CURR" | |
compopt +o filenames | |
fi | |
return 0 | |
} | |
# complete with the command (1st parameter after the program name) | |
if _acd_cli_in_set "$PREV" acd_cli acdcli ; then | |
test "$COMP_CWORD" -gt 1 && return 0 | |
local CMD="add-child cat children clear-cache create download find find-md5 find-regex list-trash metadata mount move overwrite quota remove-child rename resolve restore stream sync trash tree umount upload usage version ls dir mkdir" | |
COMPREPLY=($(compgen -W "${CMD}" -- "$CURR")) | |
return 0 | |
fi | |
# stop completions for commands with no extra parameters | |
if _acd_cli_in_set "$COMMAND" version clear-cache list-trash usage quota; then | |
return 0 | |
fi | |
# commands which accept only 1 remote dir | |
if _acd_cli_in_set "$COMMAND" ls dir children ; then | |
if [[ "$COMPLETEDARGNUM" == 0 ]] ; then | |
_acd_cli_complete_remote '/$' | |
fi | |
return 0 | |
fi | |
# commands which accept only 1 remote location | |
if _acd_cli_in_set "$COMMAND" trash rm cat ; then | |
if [[ "$COMPLETEDARGNUM" == 0 ]] ; then | |
_acd_cli_complete_remote '' | |
fi | |
return 0 | |
fi | |
# commands which accept only 1 local dir | |
if _acd_cli_in_set "$COMMAND" mount umount ; then | |
if [[ "$COMPLETEDARGNUM" == 0 ]] ; then | |
compopt -o dirnames | |
fi | |
return 0 | |
fi | |
# commands with "remote" "local dir" parameters | |
if _acd_cli_in_set "$COMMAND" download dl ; then | |
case "$COMPLETEDARGNUM" in | |
0) _acd_cli_complete_remote '' | |
;; | |
1) compopt -o dirnames | |
;; | |
esac | |
return 0 | |
fi | |
# commands with "local" "remote dir" parameters | |
if _acd_cli_in_set "$COMMAND" upload ul ; then | |
case "$COMPLETEDARGNUM" in | |
0) compopt -o default | |
;; | |
1) _acd_cli_complete_remote '/$' | |
;; | |
esac | |
return 0 | |
fi | |
compopt -o default | |
return 0 | |
} | |
complete -F _acd_cli acd_cli | |
complete -F _acd_cli acdcli |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment