From ed3162d200925148319a5adcc1907c1c89ee0390 Mon Sep 17 00:00:00 2001 From: Thomas Forgione Date: Thu, 9 Nov 2017 09:55:18 +0100 Subject: [PATCH] cdg asks if multiple solutions are possible --- zsh/functions.zsh | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/zsh/functions.zsh b/zsh/functions.zsh index 7080061..60abd3e 100644 --- a/zsh/functions.zsh +++ b/zsh/functions.zsh @@ -90,16 +90,31 @@ if [ -d "$GCLONE_PATH" ]; then echo "This function expects a single parameter" return 1 else - dir=`find $GCLONE_PATH -maxdepth 3 -name $1` + found_dirs=`find $GCLONE_PATH -maxdepth 3 -name $1 | sort` + total=`echo $found_dirs | wc -l` - if [[ "" == "$dir" ]]; then + if [[ "" == "$found_dirs" ]]; then echo "$1 not found" return 2 - elif [ `echo $dir | wc -l` -gt 1 ]; then - echo "Multiple entries for $1 found" + elif [ $total -gt 1 ]; then + counter=0 + echo "Multiple entries for $1 found:" + echo $found_dirs | while read found_dir; do + counter=$(($counter+1)) + echo "["$counter"]" $found_dir + done + read choice + if [[ "$choice" == "" ]]; then + cd `echo $found_dirs | head -1 | tail -1` + elif [[ "$choice" =~ '^[0-9]+$' ]] && [ $choice -le $total ] && [ $choice -gt 0 ]; then + cd `echo $found_dirs | head -$choice | tail -1` + else + echo "I expected a number that was less than the max..." + return 3 + fi return 3 else - cd $dir + cd $found_dirs fi fi }