Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # START : git, coloration
- if [ -z "$PS1_ORIG" ]; then
- PS1_ORIG=$PS1
- OLD_PROMPT_COMMAND=$PROMPT_COMMAND
- fi
- RED='\001\033[31m\002'
- YELLOW='\001\033[33m\002'
- GREEN='\001\033[32m\002'
- BLUE='\001\033[34m\002'
- DARK_BLUE='\001\033[36m\002'
- COLOR_NONE='\001\033[00m\002'
- function parse_git_branch {
- local AHEAD='↑'
- local LATE='↓'
- local DIVERGE='↕'
- local NOT_CLEAN='⚡'
- git rev-parse --git-dir &> /dev/null
- if [ $? != 0 ]; then
- echo "NOT_A_GIT_REPO"
- else
- git_status="$(git status 2> /dev/null)"
- branch_pattern="^# On branch ([^${IFS}]*)"
- remote_pattern="# Your branch is (.*)"
- diverge_pattern="# Your branch and (.*) have diverged"
- if [[ ! ${git_status} =~ "working directory clean" ]]; then
- state="${RED}${NOT_CLEAN}"
- fi
- # add an else if or two here if you want to get more specific
- if [[ ${git_status} =~ ${remote_pattern} ]]; then
- if [[ ${BASH_REMATCH[1]} =~ "ahead of" ]]; then
- remote="${YELLOW}${AHEAD}"
- else
- remote="${YELLOW}${LATE}"
- fi
- fi
- if [[ ${git_status} =~ ${diverge_pattern} ]]; then
- remote="${YELLOW}${DIVERGE}"
- fi
- if [[ ${git_status} =~ ${branch_pattern} ]]; then
- branch=${BASH_REMATCH[1]}
- echo " (${branch})${remote}${state}"
- fi
- fi
- }
- function prompt_func() {
- previous_return_value=$?;
- local git_result=$(parse_git_branch)
- if [ "$git_result" == "NOT_A_GIT_REPO" ]; then
- eval ${OLD_PROMPT_COMMAND}
- PS1=$PS1_ORIG
- else
- local GIT_TOPLEVEL=$(git rev-parse --show-toplevel)
- local prompt="${BLUE}[${RED}${PWD/#${GIT_TOPLEVEL%/*}/}${GREEN}${git_result}${BLUE}]${COLOR_NONE}"
- local SEP="➔"
- eval ${OLD_PROMPT_COMMAND} #Set the title value
- if test $previous_return_value -eq 0; then
- PS1="${prompt}${SEP}${COLOR_NONE} "
- else
- PS1="${prompt}${RED}${SEP}${COLOR_NONE} "
- fi
- fi
- }
- PROMPT_COMMAND=prompt_func
- # END : git, coloration
Advertisement
Add Comment
Please, Sign In to add comment