Skip to content

Instantly share code, notes, and snippets.

@Lorentz83
Created September 2, 2015 15:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lorentz83/cad24ca44b53e4a33626 to your computer and use it in GitHub Desktop.
Save Lorentz83/cad24ca44b53e4a33626 to your computer and use it in GitHub Desktop.
bash completion scripts
_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