From 879649d5422ad99d229e1f089f1f7327423f5ca4 Mon Sep 17 00:00:00 2001 From: Etienne Mabille Date: Thu, 19 Dec 2019 22:20:44 +0100 Subject: [PATCH] fix(bash): save and restore "$_" (#753) --- src/init/starship.bash | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/init/starship.bash b/src/init/starship.bash index da02f6e4..cf7b30db 100644 --- a/src/init/starship.bash +++ b/src/init/starship.bash @@ -14,11 +14,16 @@ # Will be run before *every* command (even ones in pipes!) starship_preexec() { + # Save previous command's last argument, otherwise it will be set to "starship_preexec" + local PREV_LAST_ARG=$1 + # Avoid restarting the timer for commands in the same pipeline if [ "$PREEXEC_READY" = "true" ]; then PREEXEC_READY=false STARSHIP_START_TIME=$(date +%s) fi + + : "$PREV_LAST_ARG" } # Will be run before the prompt is drawn @@ -44,7 +49,7 @@ starship_precmd() { # If the user appears to be using https://github.com/rcaloras/bash-preexec, # then hook our functions into their framework. if [[ $preexec_functions ]]; then - preexec_functions+=(starship_preexec) + preexec_functions+=('starship_preexec "$_"') precmd_functions+=(starship_precmd) else # We want to avoid destroying an existing DEBUG hook. If we detect one, create @@ -52,12 +57,12 @@ else # re-trap DEBUG to use this new function. This prevents a trap clobber. dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')" if [[ -z "$dbg_trap" ]]; then - trap starship_preexec DEBUG - elif [[ "$dbg_trap" != "starship_preexec" && "$dbg_trap" != "starship_preexec_all" ]]; then + trap 'starship_preexec "$_"' DEBUG + elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then function starship_preexec_all(){ - $dbg_trap; starship_preexec + local PREV_LAST_ARG=$1 ; $dbg_trap; starship_preexec; : "$PREV_LAST_ARG"; } - trap starship_preexec_all DEBUG + trap 'starship_preexec_all "$_"' DEBUG fi # Finally, prepare the precmd function and set up the start time.