Skip to content

Instantly share code, notes, and snippets.

@thekev
Created May 29, 2013 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thekev/5673122 to your computer and use it in GitHub Desktop.
Save thekev/5673122 to your computer and use it in GitHub Desktop.
# in your ~/.zshrc add these lines:
#
# source path/to/this/file
# precmd() { zsh_prompt }
#
NONE="%{\e[0m%}" # unsets color to term's fg color
K="%{\e[0;30m%}" # black
R="%{\e[0;31m%}" # red
G="%{\e[0;32m%}" # green
Y="%{\e[0;33m%}" # yellow
B="%{\e[0;34m%}" # blue
M="%{\e[0;35m%}" # magenta
C="%{\e[0;36m%}" # cyan
W="%{\e[0;37m%}" # white
EMK="%{\e[1;30m%}"
EMR="%{\e[1;31m%}"
EMG="%{\e[1;32m%}"
EMY="%{\e[1;33m%}"
EMB="%{\e[1;34m%}"
EMM="%{\e[1;35m%}"
EMC="%{\e[1;36m%}"
EMW="%{\e[1;37m%}"
BGK="%{\e[40m%}"
BGR="%{\e[41m%}"
BGG="%{\e[42m%}"
BGY="%{\e[43m%}"
BGB="%{\e[44m%}"
BGM="%{\e[45m%}"
BGC="%{\e[46m%}"
BGW="%{\e[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}]"
else
#default RPROMPT
echo "${EMK}[%* %D]${NONE}"
fi
}
zsh_prompt() {
RPROMPT="$(__git_prompt)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment