From e1aeba7be6611cb6c74a32aacd170cfbad797328 Mon Sep 17 00:00:00 2001 From: Danil Kutkevich Date: Tue, 1 Oct 2013 17:40:56 +0400 Subject: [PATCH] Added callback function to tweak prompt dynamicly --- README.md | 24 +++++++++++++++++++----- gitprompt.sh | 15 ++++++++++++--- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8c8e7f3..a2de364 100644 --- a/README.md +++ b/README.md @@ -43,11 +43,25 @@ The symbols are as follows: ## Install -1. Clone this repository to your homedir e.g. ``git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt`` -1. configure your prompt and source the file ``gitprompt.sh`` from your ``~/.bashrc`` config file - * you can use ``GIT_PROMPT_START`` and ``GIT_PROMPT_END`` to tweak your prompt - * If you want to tweak the colors, currently you have to tweak it in the ``gitprompt.sh`` -1. Go in a git repository and test it! +1. Clone this repository to your homedir + e.g. ``git clone https://github.com/magicmonty/bash-git-prompt.git .bash-git-prompt`` +2. Source the file ``gitprompt.sh`` from your ``~/.bashrc`` config file +3. Go in a git repository and test it! + +## Configuration + +1. You can use ``GIT_PROMPT_START`` and ``GIT_PROMPT_END`` to tweak your prompt +2. If you want to tweak the colors, + currently you have to tweak it in the ``gitprompt.sh`` +3. You can define ``prompt_callback`` function to tweak your prompt dynamicly + +```sh +function prompt_callback { + if [ `jobs | wc -l` -ne 0 ]; then + echo -n " jobs:\j" + fi +} +``` **Enjoy!** diff --git a/gitprompt.sh b/gitprompt.sh index 945bc9e..e9bdeb6 100644 --- a/gitprompt.sh +++ b/gitprompt.sh @@ -73,7 +73,7 @@ function git_prompt_config() PROMPT_END="${GIT_PROMPT_END}" fi - EMPTY_PROMPT="${PROMPT_START}${PROMPT_END}" + EMPTY_PROMPT="${PROMPT_START}$($prompt_callback)${PROMPT_END}" # fetch remote revisions every other $GIT_PROMPT_FETCH_TIMEOUT (default 5) minutes GIT_PROMPT_FETCH_TIMEOUT=${1-5} @@ -182,7 +182,7 @@ function updatePrompt() { STATUS="${STATUS}${ResetColor}${GIT_PROMPT_SUFFIX}" - PS1="${PROMPT_START}${STATUS}${PROMPT_END}" + PS1="${PROMPT_START}$($prompt_callback)${STATUS}${PROMPT_END}" if [[ -n "${VIRTUAL_ENV}" ]]; then PS1="${Blue}($(basename "${VIRTUAL_ENV}"))${ResetColor} ${PS1}" fi @@ -192,6 +192,16 @@ function updatePrompt() { fi } +function prompt_callback_default { + return +} + +if [ "`type -t prompt_callback`" = 'function' ]; then + prompt_callback="prompt_callback" +else + prompt_callback="prompt_callback_default" +fi + if [ -z "$OLD_GITPROMPT" ]; then OLD_GITPROMPT=$PS1 fi @@ -203,4 +213,3 @@ else PROMPT_COMMAND=${PROMPT_COMMAND%\;}; # remove trailing semi-colon PROMPT_COMMAND="$PROMPT_COMMAND;setGitPrompt" fi -