Last active
October 12, 2015 11:38
-
-
Save thecarlhall/4021017 to your computer and use it in GitHub Desktop.
bash prompt for git
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parse_git_branch { | |
git rev-parse --git-dir &> /dev/null | |
git_status="$(git status 2> /dev/null)" | |
branch_pattern="^On branch ([^${IFS}]*)" | |
remote_pattern="# Your branch is ([[:alnum:]]*)" | |
diverge_pattern="# Your branch and (.*) have diverged" | |
if [[ ${git_status} =~ ${branch_pattern} ]]; then | |
branch=${BASH_REMATCH[1]} | |
if [[ ${git_status} =~ "Changes not staged for commit" ]]; then | |
state="${RED}⚡" | |
fi | |
if [[ ! ${git_status} =~ "working directory clean" ]]; then | |
state="${state}${YELLOW}+" | |
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" ]]; then | |
remote="${YELLOW}↑" | |
else | |
remote="${YELLOW}↓" | |
fi | |
fi | |
if [[ ${git_status} =~ ${diverge_pattern} ]]; then | |
remote="${YELLOW}↕" | |
fi | |
echo " (${branch})${remote}${state}" | |
fi | |
} | |
# Colored ls output | |
export CLICOLOR=1 | |
export LSCOLORS=dxfxcxdxbxegedabagacad | |
# export TERM="xterm-color" | |
BLUE="\[\033[0;34m\]" | |
CYAN="\[\033[0;36m\]" | |
GREEN="\[\033[0;32m\]" | |
RED="\[\033[0;31m\]" | |
MAGENTA="\[\033[0;35m\]" | |
YELLOW="\[\033[0;33m\]" | |
WHITE="\[\033[1;37m\]" | |
ENDCOLOR="\[\033[0m\]" | |
COLOR1=$CYAN | |
COLOR2=$GREEN | |
COLOR3=$CYAN | |
if [ $UID = 0 ]; then | |
# I am root | |
COLOR2="\[\033[0;31m\]" | |
fi | |
LONG_PROMPT="$COLOR2($COLOR3\u@$WHITE\h$COLOR2)\ | |
-($COLOR1$(date +%d-%b-%y)$COLOR2) \ | |
$COLOR2($COLOR3\#$COLOR2) \ | |
$COLOR2($COLOR3\w$COLOR2)$(parse_git_branch)\n \ | |
$COLOR1\$$ENDCOLOR " | |
NODATE_PROMPT="$COLOR2($COLOR3\u@$WHITE\h$COLOR2)\ | |
$COLOR2($COLOR3\#$COLOR2) \ | |
$COLOR2($COLOR3\w$COLOR2)$(parse_git_branch)\n \ | |
$COLOR1\$$ENDCOLOR " | |
SHORT_PROMPT="$COLOR2($COLOR3\u@$WHITE\h$COLOR2) \ | |
$COLOR2($COLOR3\w$COLOR2)$(parse_git_branch) \n \ | |
$COLOR1\$$ENDCOLOR " | |
export PS1=$SHORT_PROMPT | |
function prompt_func() { | |
PS1="$COLOR2($COLOR3\u@$WHITE\h$COLOR2) \ | |
$COLOR2($COLOR3\w$COLOR2)$(parse_git_branch) \n\ | |
$COLOR1\$$ENDCOLOR " | |
} | |
PROMPT_COMMAND=prompt_func |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment