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 654fb1e..bd3b5ac 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -37,4 +37,3 @@ define_git_prompt_colors() { if [[ -z "$GIT_PROMPT_SEPARATOR" || -z "$GIT_PROMPT_COMMAND_OK" ]]; then define_git_prompt_colors fi - diff --git a/gitprompt.sh b/gitprompt.sh index 5a1dd93..4d57920 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -69,7 +69,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,6 +93,17 @@ function git_prompt_config() echo 1>&2 "Cannot find git-prompt-colors.sh!" fi + if [ "$GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR" = 1 ]; then + if [ $GIT_PROMPT_LAST_COMMAND_STATE = 0 ]; then + LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_OK}"; + else + LAST_COMMAND_INDICATOR="${GIT_PROMPT_COMMAND_FAIL}"; + fi + + # replace _LAST_COMMAND_STATE_ token with the actual state + 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 if [[ -z "$PROMPT_START" || -z "$PROMPT_END" ]]; then @@ -155,9 +165,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 @@ -288,7 +300,7 @@ function prompt_callback_default { return } -function run { +function gp_install_prompt { if [ "`type -t prompt_callback`" = 'function' ]; then prompt_callback="prompt_callback" else @@ -317,8 +329,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