From 71001b12156585a3b34bf47d95d738159df5d6ba Mon Sep 17 00:00:00 2001 From: Martin Gondermann Date: Fri, 27 Jun 2014 22:17:34 +0200 Subject: [PATCH] Implemented the possibility for an indicator which shows the result of the last command. To enable just write export GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR=1 just before sourcing the gitprompt.sh fixes #60 --- README.md | 3 +++ git-prompt-colors.sh | 3 +++ gitprompt.sh | 24 ++++++++++++++++++------ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ce3102a..31ea14c 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,9 @@ function prompt_callback { - If you want to show the git prompt only, if you are in a git repository you can set ``GIT_PROMPT_ONLY_IN_REPO=1`` before sourcing the gitprompt script +- 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 + + - You can get help on the git prompt with the function ``git_prompt_help``. Examples are available with ``git_prompt_examples``. diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh index 355811c..73e10a8 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -16,6 +16,9 @@ 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 +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 + GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}" GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}" GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ " diff --git a/gitprompt.sh b/gitprompt.sh index 49213d9..03d288a 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -43,6 +43,7 @@ function git_prompt_config() local Red="\[\033[0;31m\]" local Blue="\[\033[0;34m\]" local Cyan="\[\033[0;36m\]" + local Green="\[\033[0;32m\]" #Checking if root to change output _isroot=false @@ -88,6 +89,8 @@ function git_prompt_config() GIT_PROMPT_UNTRACKED="${Cyan}…" GIT_PROMPT_STASHED="${BoldBlue}⚑" GIT_PROMPT_CLEAN="${BoldGreen}✔" + GIT_PROMPT_COMMAND_OK="${Green}✔ " + GIT_PROMPT_COMMAND_FAIL="${Red}✘ " GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}" GIT_PROMPT_START_ROOT="${Yellow}${PathShort}${ResetColor}" @@ -97,7 +100,15 @@ function git_prompt_config() # Please do not add colors to these symbols GIT_PROMPT_SYMBOLS_AHEAD="↑·" GIT_PROMPT_SYMBOLS_BEHIND="↓·" - GIT_PROMPT_SYMBOLS_PREHASH=":" + GIT_PROMPT_SYMBOLS_PREHASH=":" + 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 fi if [ "x${GIT_PROMPT_START}" == "x" ]; then @@ -133,11 +144,11 @@ function git_prompt_config() EMPTY_PROMPT=$OLD_GITPROMPT else if [[ -n "${VIRTUAL_ENV}" ]]; then - EMPTY_PROMPT="(${Blue}$(basename "${VIRTUAL_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" + EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${VIRTUAL_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" elif [[ -n "${CONDA_DEFAULT_ENV}" ]]; then - EMPTY_PROMPT="(${Blue}$(basename "${CONDA_DEFAULT_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" + EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}(${Blue}$(basename "${CONDA_DEFAULT_ENV}")${ResetColor}) ${PROMPT_START}$($prompt_callback)${PROMPT_END}" else - EMPTY_PROMPT="${PROMPT_START}$($prompt_callback)${PROMPT_END}" + EMPTY_PROMPT="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${PROMPT_END}" fi fi @@ -159,6 +170,7 @@ function git_prompt_config() } function setGitPrompt() { + LAST_COMMAND_STATE=$? local EMPTY_PROMPT local __GIT_STATUS_CMD @@ -204,13 +216,13 @@ function updatePrompt() { local GIT_PROMPT_UNTRACKED local GIT_PROMPT_STASHED local GIT_PROMPT_CLEAN + local LAST_COMMAND_INDICATOR local PROMPT_LEADING_SPACE local PROMPT_START local PROMPT_END local EMPTY_PROMPT local GIT_PROMPT_FETCH_TIMEOUT local __GIT_STATUS_CMD - local Blue="\[\033[0;34m\]" git_prompt_config @@ -265,7 +277,7 @@ function updatePrompt() { STATUS="${STATUS}${ResetColor}${GIT_PROMPT_SUFFIX}" - PS1="${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" + PS1="${LAST_COMMAND_INDICATOR}${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" if [[ -n "${VIRTUAL_ENV}" ]]; then PS1="(${Blue}$(basename ${VIRTUAL_ENV})${ResetColor}) ${PS1}" fi