function f { if [[ "$1" == "-v" ]]; then local VERBOSE=verbose shift fi FINDCMD="find " # exclusions FINDCMD+="-type d \( " FINDCMD+="-path '*/Android' -o " FINDCMD+="-path '*/.hg' -o " FINDCMD+="-path '*/.git' -o " FINDCMD+="-path '*/.MoonReader' " FINDCMD+=" \) -prune -o" # add an -iname condition for every arg; this is better than # combining them all into *$1*$2*$3* etc., because that would # only match for the specific order in which the args are given for i in "$@"; do FINDCMD+=" -iname '*$i*'" done FINDCMD+=" -print" [[ -n $VERBOSE ]] && echo FINDCMD=$FINDCMD eval "$FINDCMD" } function g() { if [[ "$1" == "-v" ]]; then local VERBOSE=verbose shift fi # case 1: invoked without args, go to home dir if [[ $# -eq 0 ]]; then [[ -n $VERBOSE ]] && echo gd: no argument, going to home dir cd return fi # treat all remaining args as a single string ARG="$*" TMPFILE="$TMPDIR"/find.tmp # case 2: arg ends with fwd slash; possibly user invoked word # completion; if it matches a direct subdir, use that, otherwise # error if [[ "${ARG: -1}" == '/' ]] then if [[ -d "$ARG" ]]; then [[ -n $VERBOSE ]] && echo gd: \"$ARG\" is name of direct subdir - OK cd "$ARG" return else echo gd: error: direct subdir \"$ARG\" does not exist [[ -n $VERBOSE ]] && echo gd: remove the slash if you want to go to a directory with a name containing \"${ARG%%/}\" return fi fi CONFIG_FILE=~/.godirrc if [[ -e "$CONFIG_FILE" ]]; then [[ -n $VERBOSE ]] && echo "gd: reading config file" source "$CONFIG_FILE" else [[ -n $VERBOSE ]] && echo "gd: no config file" fi [[ -z $MAXDEPTH ]] && MAXDEPTH=2 GD_PRUNE_FILE=~/gd.prune if [[ -e "$GD_PRUNE_FILE" ]] then [[ -n $VERBOSE ]] && echo "gd: reading prune file" cat "$GD_PRUNE_FILE" | while read LINE; do echo $LINE done else [[ -n $VERBOSE ]] && echo gd: no prune file fi # case 4: must perform search REM="$ARG" # remainder while [[ -n "$REM" ]] do ARG="${REM%%/*}" if [[ "$ARG" != "$REM" ]] then REM="${REM#*/}" else REM="" fi [[ -n $VERBOSE ]] && echo gd: searching subtree for \"$ARG\" [[ -n $VERBOSE ]] && echo gd: MAXDEPTH=$MAXDEPTH [[ -n $VERBOSE ]] && echo gd: PRUNE_EXPR=$PRUNE_EXPR NAME_OPT="-iname" #find -L -maxdepth $MAXDEPTH $PRUNE_EXPR -type d $NAME_OPT '*'"${ARG}"'*' '!' -exec test -e "{}/.nogd" ';' -print | sed -e 's/^\.\///' >"$TMPFILE" FINDCMD="find -L " FINDCMD+="-maxdepth $MAXDEPTH " FINDCMD+="-type d " for word in "$@"; do FINDCMD+="-ipath '*$word*' " done FINDCMD+="! -exec test -e "{}/.nogd" \\; " FINDCMD+="-print" [[ -n $VERBOSE ]] && echo FINDCMD="$FINDCMD" eval "$FINDCMD" | sed -e 's/^\.\///' >"$TMPFILE" N=$(cat "$TMPFILE"|wc -l) if [[ $N -eq 0 ]] then echo gd: \"$ARG\" not found rm "$TMPFILE" return fi if [[ $N -eq 1 ]] then TARGET_DIR=$(cat "$TMPFILE") [[ -n $VERBOSE ]] && echo gd: unique result: \"$TARGET_DIR\" - OK cd "$TARGET_DIR" rm "$TMPFILE" continue fi if [[ $N -gt 1 ]] then [[ -n $VERBOSE ]] && echo gd: $N directories found for \"$ARG\" cat -n "$TMPFILE" local PROMPT="choose 1-$N: " local NKEYSOPT="-n${#N}" local KEY read -n${#N} -p "$PROMPT" KEY # echo newline if necessary [[ ${#KEY} -eq ${#N} ]] && echo if [[ -z "$KEY" ]]; then [[ -n $VERBOSE ]] && echo "gd: no choice" rm $TMPFILE return fi if [[ ! "$KEY" =~ ^[0-9]+$ ]] then echo "gd: not a valid choice" rm $TMPFILE return fi # input is a number if [[ $KEY -lt 1 || $KEY -gt $N ]]; then echo "gd: not a valid choice" return fi # get the chosen line from the tempfile local TARGET_DIR=$(sed -n -e "$KEY p" "$TMPFILE") cd "$TARGET_DIR" rm $TMPFILE fi done }