Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Last active April 28, 2021 21:33
Show Gist options
  • Save beccasaurus/fca077e56775f2bd23e1d2ab6fe96486 to your computer and use it in GitHub Desktop.
Save beccasaurus/fca077e56775f2bd23e1d2ab6fe96486 to your computer and use it in GitHub Desktop.
goto.sh
GOTO_PROJECT_ROOTS=("${MY_PROMPT_HOME:-$HOME}/Code")
GOTO_VERBOSE=true
goto() {
if (( $# == 0 ))
then
cd "${GOTO_PROJECT_ROOTS[0]}"
return 0
fi
(( $# < 1 )) && return 1
local projectFolderMatcher="$1"; shift
local projectNameMatcher="$1"; shift
local projectRoot
for projectRoot in "${GOTO_PROJECT_ROOTS[@]}"
do
local directory
while read -rd '' directory
do
[ "$directory" = "$projectRoot" ] && continue
local directoryName="${directory##*/}"
if [[ "$directoryName" = *$projectFolderMatcher* ]]
then
if [ -z "$projectNameMatcher" ]
then
[ "$GOTO_VERBOSE" = true ] && echo "cd $directory"
cd "$directory"
return 0
fi
local projectDirectory
while read -rd '' projectDirectory
do
[ "$projectDirectory" = "$directory" ] && continue
local projectName="${projectDirectory##*/}"
if [[ "$projectName" = *$projectNameMatcher* ]]
then
if (( $# > 0 ))
then
local searchingDirectory="$projectDirectory"
while (( $# > 0 ))
do
local match=
local dir
while read -rd '' dir
do
[ "$dir" = "$searchingDirectory" ] && continue
local dirName="${dir##*/}"
if [[ "$dirName" = *$1* ]]
then
match="$dir"
break
fi
done < <( find "$searchingDirectory" -maxdepth 1 -type d -print0 | sort )
if [ -n "$match" ]
then
searchingDirectory="$match"
else
break
fi
shift
if (( $# == 0 )) && [ -n "$match" ]
then
[ "$GOTO_VERBOSE" = true ] && echo "cd $match"
cd "$match"
return 0
fi
done
else
[ "$GOTO_VERBOSE" = true ] && echo "cd $projectDirectory"
cd "$projectDirectory"
return 0
fi
fi
done < <( find "$directory" -maxdepth 1 -type d -print0 | sort )
fi
done < <( find "$projectRoot" -maxdepth 1 -type d -print0 | sort )
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment