Skip to content

Instantly share code, notes, and snippets.

@zwily
Created November 9, 2012 19:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zwily/631fcef80c0e00a8b8ce to your computer and use it in GitHub Desktop.
Save zwily/631fcef80c0e00a8b8ce to your computer and use it in GitHub Desktop.
NONE="\[\033[0m\]" # unsets color to term's fg color
K="\[\033[0;30m\]" # black
R="\[\033[0;31m\]" # red
G="\[\033[0;32m\]" # green
Y="\[\033[0;33m\]" # yellow
B="\[\033[0;34m\]" # blue
M="\[\033[0;35m\]" # magenta
C="\[\033[0;36m\]" # cyan
W="\[\033[0;37m\]" # white
EMK="\[\033[1;30m\]"
EMR="\[\033[1;31m\]"
EMG="\[\033[1;32m\]"
EMY="\[\033[1;33m\]"
EMB="\[\033[1;34m\]"
EMM="\[\033[1;35m\]"
EMC="\[\033[1;36m\]"
EMW="\[\033[1;37m\]"
BGK="\[\033[40m\]"
BGR="\[\033[41m\]"
BGG="\[\033[42m\]"
BGY="\[\033[43m\]"
BGB="\[\033[44m\]"
BGM="\[\033[45m\]"
BGC="\[\033[46m\]"
BGW="\[\033[47m\]"
# returns a truncated path (used in prompt)
truncated_pwd () {
local maxlen=25
if [[ $PWD == $HOME* ]]; then
newPWD="~${PWD#$HOME}"
else
newPWD=${PWD}
fi
if [ ${#newPWD} -gt $maxlen ]; then
local pwdoffset=$(( ${#newPWD} - $maxlen + 3 ))
newPWD="…${newPWD:$pwdoffset:$maxlen}"
fi
echo $newPWD
}
__git_prompt ()
{
local g="$(git rev-parse --git-dir 2>/dev/null)"
if [ -n "$g" ]; then
local r
local b
if [ -d "$g/../.dotest" ]
then
if test -f "$g/../.dotest/rebasing"
then
r="|REBASE"
elif test -f "$g/../.dotest/applying"
then
r="|AM"
else
r="|AM/REBASE"
fi
b="$(git symbolic-ref HEAD 2>/dev/null)"
elif [ -f "$g/.dotest-merge/interactive" ]
then
r="|REBASE-i"
b="$(cat "$g/.dotest-merge/head-name")"
elif [ -d "$g/.dotest-merge" ]
then
r="|REBASE-m"
b="$(cat "$g/.dotest-merge/head-name")"
elif [ -f "$g/MERGE_HEAD" ]
then
r="|MERGING"
b="$(git symbolic-ref HEAD 2>/dev/null)"
else
if [ -f "$g/BISECT_LOG" ]
then
r="|BISECTING"
fi
if ! b="$(git symbolic-ref HEAD 2>/dev/null)"
then
if ! b="$(git describe --exact-match HEAD 2>/dev/null)"
then
b="$(cut -c1-7 "$g/HEAD")..."
fi
fi
fi
local git_status="$(git status 2> /dev/null)"
local remote
local ahead_pattern="# Your branch is ahead of .* by ([0-9]+) commit"
local behind_pattern="# Your branch is behind .* by ([0-9]+) commit"
local diverge_pattern="and have ([0-9]+) and ([0-9]+) different"
if [[ ${git_status} =~ ${ahead_pattern} ]]; then
remote="${M}↑${BASH_REMATCH[1]}"
elif [[ ${git_status} =~ ${behind_pattern} ]]; then
remote="${B}↓${BASH_REMATCH[1]}"
elif [[ ${git_status} =~ ${diverge_pattern} ]]; then
remote="${M}↑${BASH_REMATCH[1]}${Y}↓${BASH_REMATCH[2]}"
fi
local state
if [[ ! ${git_status}} =~ "working directory clean" ]]; then
state="${R}●"
fi
echo " [${b##refs/heads/}$r$remote$state${NONE}]"
fi
}
bash_prompt() {
UC=$EMG # user's color
[ $UID -eq "0" ] && UC=$R # root's color
# This commented out line shows the pwd in the tab and window bar and stuff,
# but it confuses the shell a little bit wrt newlines. (Not sure why, since it's
# surrounded in \[ \])
# prompt="\[\e]1;$(truncated_pwd)\a\e]2;\u@\h: $PWD\a\]\h:\w$(__git_prompt) \u \$ "
prompt="\h:\w$(__git_prompt) \u \$ "
PS1="$prompt"
}
PROMPT_COMMAND=bash_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment