From a470aad440965df8eccd610a9ee0f7e980a6933e Mon Sep 17 00:00:00 2001 From: Thomas Nilsson Date: Fri, 19 May 2023 13:05:14 +0200 Subject: [PATCH] Do not modify the first subscript of array PROMPT_COMMAND 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. --- gitprompt.sh | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/gitprompt.sh b/gitprompt.sh index 999547e..67bbd10 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -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"