Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Last active June 17, 2019 09:42
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save zkamvar/3299fc8ae5f1cade0aa3 to your computer and use it in GitHub Desktop.
Command prompt functions to go in .bashrc or .bash_profile that will color the command line if there is a git repository.
#
#
#
#
#
#
#
#
# THIS IS NOW LOCATED AT https://raw.githubusercontent.com/zkamvar/config-files/master/.bash_prompt
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
#
# color prompt to include branch information
function color_my_prompt {
local __user_and_host="\[\033[01;32m\]\u@\h"
local __cur_location="\t\w"
local __git_branch='`git symbolic-ref HEAD --short | sed -E s/^\(.+\)$/\(\\\\\1\)\/`'
local __prompt_tail="$"
local __last_color="\[\033[00m\]"
RED="\[\033[0;31m\]"
YELLOW="\[\033[0;33m\]"
LPURPLE="\[\033[0;35m\]"
GREEN="\[\033[0;32m\]"
LBLUE="\[\033[0;34m\]"
# Capture the output of the "git status" command.
git_status="$(git status 2> /dev/null)"
# Set color based on clean/staged/dirty.
if [[ ${git_status} =~ "othing to commit" ]]; then
#state="${GREEN}"
state="${LBLUE}"
elif [[ ${git_status} =~ "Changes to be committed" ]]; then
#state="${YELLOW}"
state="${LPURPLE}"
else
state="${RED}"
fi
# collect the number of commits ahead or behind origin
sym="$(echo \"${git_status}\" | sed -E 's/.*by[[:blank:]]([0-9]*)[[:blank:]]commit.*/\1/' | grep '[0-9]' 2> /dev/null)"
# add marker for origin status with number of commits ahead (+) or behind (-)
if [[ ${git_status} =~ "branch is ahead of" ]]; then
sym="[+"${sym}"]"
elif [[ ${git_status} =~ "branch is behind" ]]; then
sym="[-"${sym}"]"
else
sym=""
fi
export PS1="$__cur_location\n${state}$__git_branch${sym}$__last_color$__prompt_tail "
}
# Tell bash to execute this function just before displaying its prompt.
PROMPT_COMMAND=color_my_prompt
@zkamvar
Copy link
Author

zkamvar commented Jun 17, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment