From 21c3b66a77ea3ffd02aec15917493ee33815074e Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 12:55:41 -0500 Subject: [PATCH 1/2] Adds a command to expand an alias to its value --- goto.bash | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/goto.bash b/goto.bash index 2a5c4ba..c601d0a 100644 --- a/goto.bash +++ b/goto.bash @@ -48,6 +48,9 @@ function goto() -l|--list) _goto_list_aliases ;; + -x|--expand) # Expand an alias + _goto_expand_alias "$@" + ;; -h|--help) _goto_usage ;; @@ -75,6 +78,8 @@ OPTIONS: goto -u|--unregister -l, --list: lists aliases goto -l|--list + -x, --expand: expands an alias + goto -x|--expand -c, --cleanup: cleans up non existent directory aliases goto -c|--cleanup -h, --help: prints this help @@ -108,7 +113,24 @@ function _goto_list_aliases() fi } -# Registers and alias. +# Expands a registered alias. +function _goto_expand_alias() +{ + if [ "$#" -ne "1" ]; then + _goto_error "usage: goto -x|--expand " + return + fi + + local resolved=$(_goto_find_alias_directory $1) + if [ -z "$resolved" ]; then + _goto_error "alias '$1' does not exist" + return + fi + + echo "$resolved" +} + +# Registers an alias. function _goto_register_alias() { if [ "$#" -ne "2" ]; then @@ -235,7 +257,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 -l --list -x --expand -c --cleanup -v --version" -- "$1")) } # Completes the goto function with the available aliases From df46ea166befa84eaeb4ea7414bb743a505c62d6 Mon Sep 17 00:00:00 2001 From: Shawn Wallis Date: Fri, 9 Mar 2018 13:48:45 -0500 Subject: [PATCH 2/2] Adds auto-completion and documentation for expand command --- README.md | 20 ++++++++++++++++++++ goto.bash | 5 ++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b5c8737..86d9d29 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,26 @@ or goto --list ``` +### Expand an alias + +To expand an alias to its value, use: +```bash +goto -x +``` +or +```bash +goto --expand +``` + +#### Example +```bash +goto -x last_release +``` +or +```bash +goto --expand last_release +``` + ### Cleanup To cleanup the aliases from directories that are no longer accessible in your filesystem, use: diff --git a/goto.bash b/goto.bash index c601d0a..46b8147 100644 --- a/goto.bash +++ b/goto.bash @@ -309,7 +309,10 @@ function _complete_goto() prev="${COMP_WORDS[1]}" if [[ $prev = "-u" ]] || [[ $prev = "--unregister" ]]; then - # prompt with aliases only if user tries to unregister one + # prompt with aliases if user tries to unregister one + _complete_goto_aliases "$cur" + elif [[ $prev = "-x" ]] || [[ $prev = "--expand" ]]; then + # prompt with aliases if user tries to expand one _complete_goto_aliases "$cur" fi elif [ "$COMP_CWORD" -eq "3" ]; then