|
#!/bin/zsh |
|
exit_status=$1 |
|
|
|
reset=$'%{\e[0;00m%}' |
|
gray=$'%{\e[0;90m%}' |
|
red=$'%{\e[0;31m%}' |
|
green=$'%{\e[0;32m%}' |
|
yellow=$'%{\e[0;33m%}' |
|
blue=$'%{\e[0;34m%}' |
|
magenta=$'%{\e[0;35m%}' |
|
cyan=$'%{\e[0;36m%}' |
|
white=$'%{\e[0;37m%}' |
|
|
|
function finish { |
|
exit_code_color |
|
prompt_type |
|
space |
|
append $reset |
|
} |
|
|
|
function prompt_type { |
|
git_status=$(git status 2> /dev/null) |
|
if [[ $? == 0 ]]; then |
|
git_prompt |
|
else |
|
normal_prompt |
|
fi |
|
} |
|
|
|
function append { |
|
echo -n $1 |
|
} |
|
|
|
function space { |
|
echo -n " " |
|
} |
|
|
|
function exit_code_color { |
|
if [[ $exit_status == 0 ]]; then |
|
append $blue |
|
else |
|
append $red |
|
fi |
|
} |
|
|
|
function minutes_since_last_commit { |
|
now=`date +%s` |
|
last_commit=`git log --pretty=format:'%at' -1 2>/dev/null` |
|
if $lastcommit ; then |
|
seconds_since_last_commit=$((now-last_commit)) |
|
minutes_since_last_commit=$((seconds_since_last_commit/60)) |
|
echo $minutes_since_last_commit |
|
else |
|
echo "-1" |
|
fi |
|
} |
|
|
|
function time_since_last_commit { |
|
minutes=$(minutes_since_last_commit) |
|
if [ "$minutes" -ne -1 ]; then |
|
space |
|
append $gray |
|
if [ "$minutes" -ge 518400 ]; then |
|
append $(($minutes/518400)) |
|
append y |
|
elif [ "$minutes" -ge 43200 ]; then |
|
append $(($minutes/43200)) |
|
append mo |
|
elif [ "$minutes" -ge 10080 ]; then |
|
append $(($minutes/10080)) |
|
append w |
|
elif [ "$minutes" -ge 1440 ]; then |
|
append $(($minutes/1440)) |
|
append d |
|
elif [ "$minutes" -ge 60 ]; then |
|
append $(($minutes/60)) |
|
append h |
|
else |
|
append $minutes |
|
append m |
|
fi |
|
fi |
|
} |
|
|
|
function branch_or_commit { |
|
branch=$(git branch --no-color | grep --color=never '^\*' | sed 's/^\*\ //') |
|
if [[ $branch == "(no"* ]]; then |
|
# when not on any branch, always color red |
|
echo "$red$(git rev-list -n 1 --abbrev-commit HEAD)" |
|
elif [[ "$branch" == "" ]]; then |
|
# you are not on any branch or commit |
|
echo "$red???" |
|
else |
|
echo $branch |
|
fi |
|
} |
|
|
|
function git_prompt { |
|
branch=$(branch_or_commit) |
|
subdir=$(git rev-parse --show-prefix) |
|
basedir=$(git rev-parse --show-cdup) |
|
basedir |
|
time_since_last_commit |
|
branch |
|
in_rebase |
|
merge |
|
stashes |
|
subdir |
|
} |
|
|
|
function in_rebase { |
|
if [ -n "$basedir" ]; then |
|
dir=$basedir |
|
else |
|
dir='.' |
|
fi |
|
if [[ -d $dir/.git/rebase-apply ]]; then |
|
space |
|
append $red |
|
append "[REBASE!]" |
|
fi |
|
} |
|
|
|
function branch { |
|
space |
|
if clean; then |
|
append $green |
|
else |
|
append $yellow |
|
fi |
|
append $branch |
|
} |
|
|
|
function normal_prompt { |
|
append "${PWD/$HOME/~}" |
|
} |
|
|
|
function clean { |
|
echo $git_status | grep "working tree clean" > /dev/null |
|
} |
|
|
|
function ahead { |
|
echo $git_status | grep 'Your branch is ahead' > /dev/null |
|
} |
|
|
|
function diverged { |
|
echo $git_status | grep 'have diverged' > /dev/null |
|
} |
|
|
|
function behind { |
|
echo $git_status | grep 'Your branch is behind' > /dev/null |
|
} |
|
|
|
function basedir { |
|
if [ -n "$basedir" ]; then |
|
dir=$(cd $basedir; pwd) |
|
else |
|
dir=$PWD |
|
fi |
|
append $(basename $dir) |
|
} |
|
|
|
function merge { |
|
append $magenta |
|
if ahead; then |
|
space |
|
append "↑" |
|
elif behind; then |
|
space |
|
append "↓" |
|
elif diverged; then |
|
space |
|
append "↕" |
|
fi |
|
} |
|
|
|
function stashes { |
|
number_of_stashes=$(git stash list | wc -l | tr -d '[[:space:]]') |
|
append $gray |
|
if [ $number_of_stashes -eq "0" ]; then |
|
# noop |
|
elif [ $number_of_stashes -eq "1" ]; then |
|
space |
|
append "Σ" |
|
elif [ $number_of_stashes -eq "2" ]; then |
|
space |
|
append "ΣΣ" |
|
else |
|
space |
|
append "Σ${number_of_stashes}" |
|
fi |
|
} |
|
|
|
function subdir { |
|
space |
|
append $cyan |
|
append "/${subdir%/}" |
|
} |
|
|
|
finish |