bash completion for targets
authorFrederick Zhang <Frederick888@Tsundere.moe>
Wed, 26 Aug 2015 17:11:09 +0000 (01:11 +0800)
committerFrederick Zhang <Frederick888@Tsundere.moe>
Wed, 26 Aug 2015 17:11:09 +0000 (01:11 +0800)
src/etc/cargo.bashcomp.sh

index 3924e1e31f7824c4618727dfd4d0a9e25426c27b..00e0067cf48c0ba03d5f37a12c982c41896e0b1d 100644 (file)
@@ -62,6 +62,9 @@ _cargo()
                        --example)
                                COMPREPLY=( $( compgen -W "$(_get_examples)" -- "$cur" ) )
                                ;;
+                       --target)
+                               COMPREPLY=( $( compgen -W "$(_get_targets)" -- "$cur" ) )
+                               ;;
                        help)
                                COMPREPLY=( $( compgen -W "$__cargo_commands" -- "$cur" ) )
                                ;;
@@ -95,4 +98,23 @@ _get_examples(){
                echo "${names[@]}"
        fi
 }
+
+_get_targets(){
+       local TARGETS=()
+       local FIND_PATHS=( "/" )
+       local CURRENT_PATH=$(_locate_manifest)
+       while [[ "$CURRENT_PATH" != "/" ]]; do
+           FIND_PATHS+=( "$CURRENT_PATH" )
+           CURRENT_PATH=$(dirname $CURRENT_PATH)
+       done
+       for FIND_PATH in ${FIND_PATHS[@]}; do
+           if [[ -f "$FIND_PATH"/.cargo/config ]]; then
+               LINES=( `grep "$FIND_PATH"/.cargo/config -e "^\[target\."` )
+               for LINE in ${LINES[@]}; do
+                   TARGETS+=(`sed 's/^\[target\.\(.*\)\]$/\1/' <<< $LINE`)
+               done
+           fi
+       done
+       echo "${TARGETS[@]}"
+}
 # vim:ft=sh