Skip to content

Commit

Permalink
Merge pull request #155741 from microsoft/tyriar/155648
Browse files Browse the repository at this point in the history
Simplify bash PROMPT_COMMAND handling
  • Loading branch information
Tyriar authored Jul 20, 2022
2 parents 0cf530c + dfc99af commit 316cf3e
Showing 1 changed file with 11 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,6 @@ if [ "$VSCODE_INJECTION" == "1" ]; then
builtin unset VSCODE_INJECTION
fi

# Disable shell integration if PROMPT_COMMAND is 2+ function calls since that is not handled.
if [[ "$PROMPT_COMMAND" =~ .*(' '.*\;)|(\;.*' ').* ]]; then
builtin unset VSCODE_SHELL_INTEGRATION
builtin return
fi

if [ -z "$VSCODE_SHELL_INTEGRATION" ]; then
builtin return
fi
Expand Down Expand Up @@ -155,25 +149,16 @@ __vsc_update_prompt

__vsc_prompt_cmd_original() {
__vsc_status="$?"
if [[ ${IFS+set} ]]; then
__vsc_original_ifs="$IFS"
fi
if [[ "$__vsc_original_prompt_command" =~ .+\;.+ ]]; then
IFS=';'
# Evaluate the original PROMPT_COMMAND similarly to how bash would normally
# See https://unix.stackexchange.com/a/672843 for technique
if [[ ${#__vsc_original_prompt_command[@]} -gt 1 ]]; then
for cmd in "${__vsc_original_prompt_command[@]}"; do
__vsc_status="$?"
eval "${cmd:-}"
done
else
IFS=' '
eval "${__vsc_original_prompt_command:-}"
fi
builtin read -ra ADDR <<<"$__vsc_original_prompt_command"
if [[ ${__vsc_original_ifs+set} ]]; then
IFS="$__vsc_original_ifs"
unset __vsc_original_ifs
else
unset IFS
fi
for ((i = 0; i < ${#ADDR[@]}; i++)); do
(exit ${__vsc_status})
builtin eval ${ADDR[i]}
done
__vsc_precmd
}

Expand All @@ -182,13 +167,9 @@ __vsc_prompt_cmd() {
__vsc_precmd
}

if [[ "$PROMPT_COMMAND" =~ (.+\;.+) ]]; then
# item1;item2...
__vsc_original_prompt_command="$PROMPT_COMMAND"
else
# (item1, item2...)
__vsc_original_prompt_command=${PROMPT_COMMAND[@]}
fi
# PROMPT_COMMAND arrays and strings seem to be handled the same (handling only the first entry of
# the array?)
__vsc_original_prompt_command=$PROMPT_COMMAND

if [[ -z "${bash_preexec_imported:-}" ]]; then
if [[ -n "$__vsc_original_prompt_command" && "$__vsc_original_prompt_command" != "__vsc_prompt_cmd" ]]; then
Expand Down

0 comments on commit 316cf3e

Please sign in to comment.