diff --git a/completion/available/defaults.completion.bash b/completion/available/defaults.completion.bash index 5a8d0340..f7289599 100644 --- a/completion/available/defaults.completion.bash +++ b/completion/available/defaults.completion.bash @@ -13,12 +13,12 @@ _defaults_domains() COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} - local domains=$( defaults domains | sed -e 's/, /:/g' | tr : '\n' | sed -e 's/ /\\ /g' | grep -i "^$cur" ) - local IFS=$'\n' - COMPREPLY=( $domains ) - if [[ $( echo '-app' | grep "^$cur" ) ]]; then - COMPREPLY[${#COMPREPLY[@]}]="-app" - fi + local domains=$( defaults domains | sed -e 's/, /:/g' | tr : '\n' | sed -e 's/ /\\ /g' | grep -i "^$cur" ) + local IFS=$'\n' + COMPREPLY=( $domains ) + if [[ $( echo '-app' | grep "^$cur" ) ]]; then + COMPREPLY[${#COMPREPLY[@]}]="-app" + fi return 0 } @@ -26,119 +26,119 @@ _defaults_domains() _defaults() { - local cur prev host_opts cmds cmd domain keys key_index + local cur prev host_opts cmds cmd domain keys key_index cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]} - host_opts='-currentHost -host' - cmds='read read-type write rename delete domains find help' + host_opts='-currentHost -host' + cmds='read read-type write rename delete domains find help' - if [[ $COMP_CWORD -eq 1 ]]; then - COMPREPLY=( $( compgen -W "$host_opts $cmds" -- $cur ) ) - return 0 - elif [[ $COMP_CWORD -eq 2 ]]; then - if [[ "$prev" == "-currentHost" ]]; then - COMPREPLY=( $( compgen -W "$cmds" -- $cur ) ) - return 0 - elif [[ "$prev" == "-host" ]]; then - return 0 - _known_hosts -a - else - _defaults_domains - return 0 - fi - elif [[ $COMP_CWORD -eq 3 ]]; then - if [[ ${COMP_WORDS[1]} == "-host" ]]; then - _defaults_domains - return 0 - fi + if [[ $COMP_CWORD -eq 1 ]]; then + COMPREPLY=( $( compgen -W "$host_opts $cmds" -- $cur ) ) + return 0 + elif [[ $COMP_CWORD -eq 2 ]]; then + if [[ "$prev" == "-currentHost" ]]; then + COMPREPLY=( $( compgen -W "$cmds" -- $cur ) ) + return 0 + elif [[ "$prev" == "-host" ]]; then + return 0 + _known_hosts -a + else + _defaults_domains + return 0 + fi + elif [[ $COMP_CWORD -eq 3 ]]; then + if [[ ${COMP_WORDS[1]} == "-host" ]]; then + _defaults_domains + return 0 + fi fi - # Both a domain and command have been specified + # Both a domain and command have been specified - if [[ ${COMP_WORDS[1]} == [${cmds// /|}] ]]; then - cmd=${COMP_WORDS[1]} - domain=${COMP_WORDS[2]} - key_index=3 - if [[ "$domain" == "-app" ]]; then - if [[ $COMP_CWORD -eq 3 ]]; then - # Completing application name. Can't help here, sorry - return 0 - fi - domain="-app ${COMP_WORDS[3]}" - key_index=4 - fi - elif [[ ${COMP_WORDS[2]} == "-currentHost" ]] && [[ ${COMP_WORDS[2]} == [${cmds// /|}] ]]; then - cmd=${COMP_WORDS[2]} - domain=${COMP_WORDS[3]} - key_index=4 - if [[ "$domain" == "-app" ]]; then - if [[ $COMP_CWORD -eq 4 ]]; then - # Completing application name. Can't help here, sorry - return 0 - fi - domain="-app ${COMP_WORDS[4]}" - key_index=5 - fi - elif [[ ${COMP_WORDS[3]} == "-host" ]] && [[ ${COMP_WORDS[3]} == [${cmds// /|}] ]]; then - cmd=${COMP_WORDS[3]} - domain=${COMP_WORDS[4]} - key_index=5 - if [[ "$domain" == "-app" ]]; then - if [[ $COMP_CWORD -eq 5 ]]; then - # Completing application name. Can't help here, sorry - return 0 - fi - domain="-app ${COMP_WORDS[5]}" - key_index=6 - fi - fi + if [[ ${COMP_WORDS[1]} == [${cmds// /|}] ]]; then + cmd=${COMP_WORDS[1]} + domain=${COMP_WORDS[2]} + key_index=3 + if [[ "$domain" == "-app" ]]; then + if [[ $COMP_CWORD -eq 3 ]]; then + # Completing application name. Can't help here, sorry + return 0 + fi + domain="-app ${COMP_WORDS[3]}" + key_index=4 + fi + elif [[ ${COMP_WORDS[2]} == "-currentHost" ]] && [[ ${COMP_WORDS[2]} == [${cmds// /|}] ]]; then + cmd=${COMP_WORDS[2]} + domain=${COMP_WORDS[3]} + key_index=4 + if [[ "$domain" == "-app" ]]; then + if [[ $COMP_CWORD -eq 4 ]]; then + # Completing application name. Can't help here, sorry + return 0 + fi + domain="-app ${COMP_WORDS[4]}" + key_index=5 + fi + elif [[ ${COMP_WORDS[3]} == "-host" ]] && [[ ${COMP_WORDS[3]} == [${cmds// /|}] ]]; then + cmd=${COMP_WORDS[3]} + domain=${COMP_WORDS[4]} + key_index=5 + if [[ "$domain" == "-app" ]]; then + if [[ $COMP_CWORD -eq 5 ]]; then + # Completing application name. Can't help here, sorry + return 0 + fi + domain="-app ${COMP_WORDS[5]}" + key_index=6 + fi + fi - keys=$( defaults read $domain 2>/dev/null | sed -n -e '/^ [^}) ]/p' | sed -e 's/^ \([^" ]\{1,\}\) = .*$/\1/g' -e 's/^ "\([^"]\{1,\}\)" = .*$/\1/g' | sed -e 's/ /\\ /g' ) + keys=$( defaults read $domain 2>/dev/null | sed -n -e '/^ [^}) ]/p' | sed -e 's/^ \([^" ]\{1,\}\) = .*$/\1/g' -e 's/^ "\([^"]\{1,\}\)" = .*$/\1/g' | sed -e 's/ /\\ /g' ) - case $cmd in - read|read-type) - # Complete key - local IFS=$'\n' - COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) - ;; - write) - if [[ $key_index -eq $COMP_CWORD ]]; then - # Complete key - local IFS=$'\n' - COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) - elif [[ $((key_index+1)) -eq $COMP_CWORD ]]; then - # Complete value type - # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces - local value_types='-string -data -integer -float -boolean -date -array -array-add -dict -dict-add' - local cur_type=$( defaults read-type $domain ${COMP_WORDS[key_index]} 2>/dev/null | sed -e 's/^Type is \(.*\)/-\1/' -e's/dictionary/dict/' | grep "^$cur" ) - if [[ $cur_type ]]; then - COMPREPLY=( $cur_type ) - else - COMPREPLY=( $( compgen -W "$value_types" -- $cur ) ) - fi - elif [[ $((key_index+2)) -eq $COMP_CWORD ]]; then - # Complete value - # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces - COMPREPLY=( $( defaults read $domain ${COMP_WORDS[key_index]} 2>/dev/null | grep -i "^${cur//\\/\\\\}" ) ) - fi - ;; - rename) - if [[ $key_index -eq $COMP_CWORD ]] || - [[ $((key_index+1)) -eq $COMP_CWORD ]]; then - # Complete source and destination keys - local IFS=$'\n' - COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) - fi - ;; - delete) - if [[ $key_index -eq $COMP_CWORD ]]; then - # Complete key - local IFS=$'\n' - COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) - fi - ;; - esac + case $cmd in + read|read-type) + # Complete key + local IFS=$'\n' + COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) + ;; + write) + if [[ $key_index -eq $COMP_CWORD ]]; then + # Complete key + local IFS=$'\n' + COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) + elif [[ $((key_index+1)) -eq $COMP_CWORD ]]; then + # Complete value type + # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces + local value_types='-string -data -integer -float -boolean -date -array -array-add -dict -dict-add' + local cur_type=$( defaults read-type $domain ${COMP_WORDS[key_index]} 2>/dev/null | sed -e 's/^Type is \(.*\)/-\1/' -e's/dictionary/dict/' | grep "^$cur" ) + if [[ $cur_type ]]; then + COMPREPLY=( $cur_type ) + else + COMPREPLY=( $( compgen -W "$value_types" -- $cur ) ) + fi + elif [[ $((key_index+2)) -eq $COMP_CWORD ]]; then + # Complete value + # Unfortunately ${COMP_WORDS[key_index]} fails on keys with spaces + COMPREPLY=( $( defaults read $domain ${COMP_WORDS[key_index]} 2>/dev/null | grep -i "^${cur//\\/\\\\}" ) ) + fi + ;; + rename) + if [[ $key_index -eq $COMP_CWORD ]] || + [[ $((key_index+1)) -eq $COMP_CWORD ]]; then + # Complete source and destination keys + local IFS=$'\n' + COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) + fi + ;; + delete) + if [[ $key_index -eq $COMP_CWORD ]]; then + # Complete key + local IFS=$'\n' + COMPREPLY=( $( echo "$keys" | grep -i "^${cur//\\/\\\\}" ) ) + fi + ;; + esac return 0 }