diff --git a/README.md b/README.md index 7f8131b..3fa86df 100644 --- a/README.md +++ b/README.md @@ -113,7 +113,15 @@ function prompt_callback { - You can show an additional indicator at the start of the prompt, which shows the result of the last executed command by setting ``GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR=1`` before sourcing the gitprompt - script + script. + If you want to display the exit code too, you can use the placeholder + ``_LAST_COMMAND_STATE_`` in ``GIT_PROMPT_COMMAND_OK`` or ``GIT_PROMPT_COMMAND_FAIL`` + in your ``.git-prompt-colors.sh``: + +```sh +GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # displays as ✔-0 +GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # displays as ✘-1 for exit code 1 +``` - It is now possible to disable the fetching of the remote repository either globally by setting ``GIT_PROMPT_FETCH_REMOTE_STATUS=0`` in your .bashrc or diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh index 7eff7c7..4d2dd24 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -18,6 +18,12 @@ GIT_PROMPT_UNTRACKED="${Cyan}…" # the number of untracked files/dirs GIT_PROMPT_STASHED="${BoldBlue}⚑ " # the number of stashed files/dir GIT_PROMPT_CLEAN="${BoldGreen}✔" # a colored flag indicating a "clean" repo +# For the command indicator, the placeholder _LAST_COMMAND_STATE_ +# will be replaced with the exit code of the last command +# e.g. +# GIT_PROMPT_COMMAND_OK="${Green}✔-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of 0 +# GIT_PROMPT_COMMAND_FAIL="${Red}✘-_LAST_COMMAND_STATE_ " # indicator if the last command returned with an exit code of other than 0 + GIT_PROMPT_COMMAND_OK="${Green}✔ " # indicator if the last command returned with an exit code of 0 GIT_PROMPT_COMMAND_FAIL="${Red}✘ " # indicator if the last command returned with an exit code of other than 0 diff --git a/gitprompt.sh b/gitprompt.sh index 50863d0..da136a6 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -62,7 +62,6 @@ function gp_maybe_set_envar_to_path(){ function git_prompt_config() { - #Checking if root to change output _isroot=false [[ $UID -eq 0 ]] && _isroot=true @@ -94,11 +93,13 @@ function git_prompt_config() fi if [ "x${GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR}" == "x1" ]; then - if [ $LAST_COMMAND_STATE = 0 ]; then - LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_OK}"; - else - LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_FAIL}"; - fi + if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then + LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_OK}"; + else + LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_FAIL}"; + fi + + LAST_COMMAND_INDICATOR="${LAST_COMMAND_INDICATOR/_LAST_COMMAND_STATE_/${GIT_PROMPT_LAST_COMMAND_STATE}}" fi # Do this only once to define PROMPT_START and PROMPT_END @@ -161,9 +162,11 @@ function git_prompt_config() fi } -function setGitPrompt() { - LAST_COMMAND_STATE=$? +function setLastCommandState() { + GIT_PROMPT_LAST_COMMAND_STATE=$? +} +function setGitPrompt() { local EMPTY_PROMPT local __GIT_STATUS_CMD @@ -181,11 +184,11 @@ function setGitPrompt() { fi if [[ -e "${repo}/.bash-git-rc" ]]; then - source "${repo}/.bash-git-rc" + source "${repo}/.bash-git-rc" fi if [ "x${FETCH_REMOTE_STATUS}" == "x1" ]; then - checkUpstream + checkUpstream fi updatePrompt @@ -294,7 +297,7 @@ function prompt_callback_default { return } -function run { +function gp_install_prompt { if [ "`type -t prompt_callback`" = 'function' ]; then prompt_callback="prompt_callback" else @@ -323,8 +326,21 @@ function run { esac fi + if [ "x${GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR}" == "x1" ]; then + local setLastCommandStateEntry="setLastCommandState" + case ";$PROMPT_COMMAND;" in + *";$setLastCommandStateEntry;"*) + # echo "PROMPT_COMMAND already contains: $setLastCommandStateEntry" + :;; + *) + PROMPT_COMMAND="$setLastCommandStateEntry;$PROMPT_COMMAND" + # echo "PROMPT_COMMAND does not contain: $setLastCommandStateEntry" + ;; + esac + fi + git_prompt_dir source "$__GIT_PROMPT_DIR/git-prompt-help.sh" } -run +gp_install_prompt