Skip to content

Commit

Permalink
Do not modify the first subscript of array PROMPT_COMMAND
Browse files Browse the repository at this point in the history
With Bash 5.1, PROMPT_COMMAND can be an array. Respect the users choice
and support array PROMPT_COMMAND.

If PROMPT_COMMAND isn't set, default to array as it doesn't require the
use of `make_prompt_command_clean` to function.
  • Loading branch information
knirch committed May 19, 2023
1 parent 08a4af2 commit a470aad
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions gitprompt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,25 @@ function add_to_end_of_prompt_command() {
}

function gp_install_prompt {
make_prompt_command_clean
add_to_end_of_prompt_command "setGitPrompt"
add_to_beginning_of_prompt_command "setLastCommandState"
# 5.1 supports PROMPT_COMMAND as an array
if ((BASH_VERSINFO[0] > 5 || BASH_VERSINFO[0] == 5 && BASH_VERSINFO[1] >= 1)); then
if [[ $(declare -p PROMPT_COMMAND 2>/dev/null) == "declare --"* ]]; then
make_prompt_command_clean
add_to_end_of_prompt_command "setGitPrompt"
add_to_beginning_of_prompt_command "setLastCommandState"
else
if [[ "${PROMPT_COMMAND[*]}" != *setGitPrompt* ]]; then
PROMPT_COMMAND+=(setGitPrompt)
fi
if [[ "${PROMPT_COMMAND[*]}" != *setLastCommandState* ]]; then
PROMPT_COMMAND=(setLastCommandState "${PROMPT_COMMAND[@]}")
fi
fi
else
make_prompt_command_clean
add_to_end_of_prompt_command "setGitPrompt"
add_to_beginning_of_prompt_command "setLastCommandState"
fi

set_git_prompt_dir
source "${__GIT_PROMPT_DIR}/git-prompt-help.sh"
Expand Down

0 comments on commit a470aad

Please sign in to comment.