From 3569c97ba024aeffef80e2da4c7373ee768b9753 Mon Sep 17 00:00:00 2001 From: Martin Gondermann Date: Fri, 19 Sep 2014 20:08:48 +0200 Subject: [PATCH] Ditched the config variable GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR in favor of a more general approach with a placeholder. Fixes #76 --- README.md | 22 +++++++++++++++------- git-prompt-colors.sh | 11 ++++++----- gitprompt.sh | 25 ++++++++++++------------- 3 files changed, 33 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 72dc3bd..eb54d77 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,11 @@ if you have one. It now contains a function named ``define_git_prompt_colors()`` **Please see the updated ``git-prompt-colors.sh`` in the installation directory!** +--- + +**The variable `GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR` was replaced with a more general placeholder +named ``_LAST_COMMAND_INDICATOR_``, which is replaced by the state of the last executed command. It is now activated by default.** + ## Examples The prompt may look like the following: @@ -92,9 +97,6 @@ git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt # Set config variables first GIT_PROMPT_ONLY_IN_REPO=1 - # GIT_PROMPT_SHOW_LAST_COMMAND_INDICATOR=1 # uncomment if you want to show - # the exit code of the last command - # in your prompt # GIT_PROMPT_FETCH_REMOTE_STATUS=0 # uncomment to avoid fetching remote status # GIT_PROMPT_START=... # uncomment for custom prompt start sequence @@ -140,10 +142,16 @@ 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. +- There is an indicator at the start of the prompt, which shows + the result of the last executed command by if you put the placeholder + `_LAST_COMMAND_INDICATOR_` in any of the prompt templates. + It is now by default activated in the default `git-prompt-colors.sh`: + +```sh + GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}" + GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}" +``` + 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``: diff --git a/git-prompt-colors.sh b/git-prompt-colors.sh index c50d2f3..9d6a7ed 100644 --- a/git-prompt-colors.sh +++ b/git-prompt-colors.sh @@ -25,16 +25,17 @@ define_git_prompt_colors() { # 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 + 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 # template for displaying the current virtual environment # use the placeholder _VIRTUALENV_ will be replaced with # the name of the current virtual environment (currently CONDA and VIRTUAL_ENV) GIT_PROMPT_VIRTUALENV="(${Blue}_VIRTUALENV_${ResetColor}) " - - GIT_PROMPT_START_USER="${Yellow}${PathShort}${ResetColor}" - GIT_PROMPT_START_ROOT="${GIT_PROMPT_START_USER}" + + # _LAST_COMMAND_INDICATOR_ will be replaced by the appropriate GIT_PROMPT_COMMAND_OK OR GIT_PROMPT_COMMAND_FAIL + GIT_PROMPT_START_USER="_LAST_COMMAND_INDICATOR_ ${Yellow}${PathShort}${ResetColor}" + GIT_PROMPT_START_ROOT="_LAST_COMMAND_INDICATOR_ ${GIT_PROMPT_START_USER}" GIT_PROMPT_END_USER=" \n${White}${Time12a}${ResetColor} $ " GIT_PROMPT_END_ROOT=" \n${White}${Time12a}${ResetColor} # " diff --git a/gitprompt.sh b/gitprompt.sh index 759844d..9f6c290 100755 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -105,17 +105,15 @@ 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}}" + 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}}" + # Do this only once to define PROMPT_START and PROMPT_END if [[ -z "$PROMPT_START" || -z "$PROMPT_END" ]]; then @@ -157,7 +155,7 @@ function git_prompt_config() if [[ "$GIT_PROMPT_ONLY_IN_REPO" = 1 ]]; then EMPTY_PROMPT="$OLD_GITPROMPT" else - local ps="$LAST_COMMAND_INDICATOR" + local ps="" if [[ -n "$VIRTUAL_ENV" ]]; then VENV=$(basename "${VIRTUAL_ENV}") ps="${ps}${GIT_PROMPT_VIRTUALENV/_VIRTUALENV_/${VENV}}" @@ -166,7 +164,8 @@ function git_prompt_config() VENV=$(basename "${CONDA_DEFAULT_ENV}") ps="${ps}${GIT_PROMPT_VIRTUALENV/_VIRTUALENV_/${VENV}}" fi - EMPTY_PROMPT="$ps$PROMPT_START$($prompt_callback)$PROMPT_END" + ps="$ps$PROMPT_START$($prompt_callback)$PROMPT_END" + EMPTY_PROMPT="${ps/_LAST_COMMAND_INDICATOR_/${LAST_COMMAND_INDICATOR}}" fi # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes @@ -296,7 +295,7 @@ function updatePrompt() { __chk_gitvar_status 'CLEAN' '-eq 1' - __add_status "$ResetColor$GIT_PROMPT_SUFFIX" - NEW_PROMPT="$LAST_COMMAND_INDICATOR" + NEW_PROMPT="" if [[ -n "$VIRTUAL_ENV" ]]; then VENV=$(basename "${VIRTUAL_ENV}") NEW_PROMPT="$NEW_PROMPT${GIT_PROMPT_VIRTUALENV/_VIRTUALENV_/${VENV}}" @@ -312,7 +311,7 @@ function updatePrompt() { NEW_PROMPT="$EMPTY_PROMPT" fi - PS1="$NEW_PROMPT" + PS1="${NEW_PROMPT/_LAST_COMMAND_INDICATOR_/${LAST_COMMAND_INDICATOR}}" } function prompt_callback_default {