Merge pull request #17 from shawalli/feature/push_pop_commands

Add Push/Pop Commands
This commit is contained in:
Lazarus Lazaridis 2018-03-12 09:26:53 +02:00 committed by GitHub
commit 2db8b0bf1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 56 additions and 1 deletions

View File

@ -53,11 +53,34 @@ To change to an aliased directory, type:
goto <alias> goto <alias>
``` ```
To first push the current directory onto the directory stack before changing directories, type:
```bash
goto -p <alias>
```
or
```bash
goto --push <alias>
```
#### Example: #### Example:
```bash ```bash
goto dev 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 ### Register an alias
To register a directory alias, type: To register a directory alias, type:
```bash ```bash

34
goto.sh
View File

@ -47,6 +47,12 @@ goto()
-u|--unregister) # Unregister an alias -u|--unregister) # Unregister an alias
_goto_unregister_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) -l|--list)
_goto_list_aliases _goto_list_aliases
;; ;;
@ -84,6 +90,10 @@ OPTIONS:
goto -r|--register <alias> <directory> goto -r|--register <alias> <directory>
-u, --unregister: unregisters an alias -u, --unregister: unregisters an alias
goto -u|--unregister <alias> goto -u|--unregister <alias>
-p, --push: pushes the current directory onto the stack, then performs goto
goto -p|--push <alias>
-o, --pop: pops the top directory from the stack, then changes to that directory
goto -o|--pop
-l, --list: lists aliases -l, --list: lists aliases
goto -l|--list goto -l|--list
-x, --expand: expands an alias -x, --expand: expands an alias
@ -210,6 +220,25 @@ _goto_unregister_alias()
echo "Alias '$1' unregistered successfully." 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 <alias>"
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. # Unregisters aliases whose directories no longer exist.
_goto_cleanup() _goto_cleanup()
{ {
@ -292,7 +321,7 @@ _complete_goto_commands()
local IFS=$' \t\n' local IFS=$' \t\n'
# shellcheck disable=SC2207 # shellcheck disable=SC2207
COMPREPLY=($(compgen -W "-r --register -u --unregister -l --list -x --expand -c --cleanup -v --version" -- "$1")) COMPREPLY=($(compgen -W "-r --register -u --unregister -p --push -o --pop -l --list -x --expand -c --cleanup -v --version" -- "$1"))
} }
# Completes the goto function with the available aliases # Completes the goto function with the available aliases
@ -350,6 +379,9 @@ _complete_goto_bash()
elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then
# prompt with aliases if user tries to expand one # prompt with aliases if user tries to expand one
_complete_goto_aliases "$cur" _complete_goto_aliases "$cur"
elif [[ $prev = "-p" ]] || [[ $prev = "--push" ]]; then
# prompt with aliases only if user tries to push
_complete_goto_aliases "$cur"
fi fi
elif [ "$COMP_CWORD" -eq "3" ]; then elif [ "$COMP_CWORD" -eq "3" ]; then
# if we are on the third argument # if we are on the third argument