fix(bash): save and restore "$_" (#753)

This commit is contained in:
Etienne Mabille 2019-12-19 22:20:44 +01:00 committed by Matan Kushner
parent aab35674d2
commit 879649d542
1 changed files with 10 additions and 5 deletions

View File

@ -14,11 +14,16 @@
# Will be run before *every* command (even ones in pipes!) # Will be run before *every* command (even ones in pipes!)
starship_preexec() { 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 # Avoid restarting the timer for commands in the same pipeline
if [ "$PREEXEC_READY" = "true" ]; then if [ "$PREEXEC_READY" = "true" ]; then
PREEXEC_READY=false PREEXEC_READY=false
STARSHIP_START_TIME=$(date +%s) STARSHIP_START_TIME=$(date +%s)
fi fi
: "$PREV_LAST_ARG"
} }
# Will be run before the prompt is drawn # 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, # If the user appears to be using https://github.com/rcaloras/bash-preexec,
# then hook our functions into their framework. # then hook our functions into their framework.
if [[ $preexec_functions ]]; then if [[ $preexec_functions ]]; then
preexec_functions+=(starship_preexec) preexec_functions+=('starship_preexec "$_"')
precmd_functions+=(starship_precmd) precmd_functions+=(starship_precmd)
else else
# We want to avoid destroying an existing DEBUG hook. If we detect one, create # 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. # re-trap DEBUG to use this new function. This prevents a trap clobber.
dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')" dbg_trap="$(trap -p DEBUG | cut -d' ' -f3 | tr -d \')"
if [[ -z "$dbg_trap" ]]; then if [[ -z "$dbg_trap" ]]; then
trap starship_preexec DEBUG trap 'starship_preexec "$_"' DEBUG
elif [[ "$dbg_trap" != "starship_preexec" && "$dbg_trap" != "starship_preexec_all" ]]; then elif [[ "$dbg_trap" != 'starship_preexec "$_"' && "$dbg_trap" != 'starship_preexec_all "$_"' ]]; then
function starship_preexec_all(){ 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 fi
# Finally, prepare the precmd function and set up the start time. # Finally, prepare the precmd function and set up the start time.