From e53f05c21e13bbfeb313bb87568b467b0c32c817 Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 13:32:55 -0500 Subject: [PATCH 1/2] Adds two commands to replicate pushd/popd before completing goto --- goto.bash | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/goto.bash b/goto.bash index 2a5c4ba..aeceb2a 100644 --- a/goto.bash +++ b/goto.bash @@ -45,6 +45,12 @@ function goto() -u|--unregister) # Unregister an alias _goto_unregister_alias "$@" ;; + -p|--push) # Push the current directory onto the pushd stack, then goto + _goto_directory_push "$@" + ;; + -o|--pop) # Pop the top directory off of the pushd stack, then change that directory + _goto_directory_pop + ;; -l|--list) _goto_list_aliases ;; @@ -73,6 +79,10 @@ OPTIONS: goto -r|--register -u, --unregister: unregisters an alias goto -u|--unregister + -p, --push: pushes the current directory onto the stack, then performs goto + goto -p|--push + -o, --pop: pops the top directory from the stack, then changes to that directory + goto -o|--pop -l, --list: lists aliases goto -l|--list -c, --cleanup: cleans up non existent directory aliases @@ -161,6 +171,25 @@ function _goto_unregister_alias echo "Alias '$1' unregistered successfully." } +# Pushes the current directory onto the stack, then goto +function _goto_directory_push() +{ + if [ "$#" -ne "1" ]; then + _goto_error "usage: goto -p|--push " + return + fi + + pushd . 2>&1 1>/dev/null + + _goto_directory $@ +} + +# Pops the top directory from the stack, then goto +function _goto_directory_pop() +{ + popd 2>&1 1>/dev/null +} + # Unregisters aliases whose directories no longer exist. function _goto_cleanup() { @@ -235,7 +264,7 @@ function _complete_goto_commands() local IFS=$' \t\n' # shellcheck disable=SC2207 - COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -c --cleanup -v --version" -- "$1")) + COMPREPLY=($(compgen -W "-r --register -u --unregister -p --push -o --pop -l --list -c --cleanup -v --version" -- "$1")) } # Completes the goto function with the available aliases @@ -289,6 +318,9 @@ function _complete_goto() if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then # prompt with aliases only if user tries to unregister one _complete_goto_aliases "$cur" + elif [[ $prev = "-p" ]] || [[ $prev = "--push" ]]; then + # prompt with aliases only if user tries to push + _complete_goto_aliases "$cur" fi elif [ "$COMP_CWORD" -eq "3" ]; then # if we are on the third argument From 873ee5a05d9b92dd20f19f0116737e00a2e550bf Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 13:45:18 -0500 Subject: [PATCH 2/2] adds documentation for push/pop commands --- README.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/README.md b/README.md index b5c8737..b5cf731 100644 --- a/README.md +++ b/README.md @@ -44,11 +44,34 @@ To change to an aliased directory, type: goto ``` +To first push the current directory onto the directory stack before changing directories, type: +```bash +goto -p +``` +or +```bash +goto --push +``` + #### Example: ```bash goto dev ``` +### Revert to a pushed directory +To return to a pushed directory, type: +```bash +goto -o +``` +or +```bash +goto --pop +``` + +#### Notes + +This command is equivalent to `popd`, but within the `goto` command. + ### Register an alias To register a directory alias, type: ```bash