From 9d21d97ecf2ec3a9d30deb896ef18bb0192feb10 Mon Sep 17 00:00:00 2001 From: Curtis McEnroe Date: Sat, 26 Jan 2013 17:46:57 -0500 Subject: Add git prompt --- .zsh/gitprompt.zsh | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 .zsh/gitprompt.zsh (limited to '.zsh/gitprompt.zsh') diff --git a/.zsh/gitprompt.zsh b/.zsh/gitprompt.zsh new file mode 100644 index 00000000..0a420786 --- /dev/null +++ b/.zsh/gitprompt.zsh @@ -0,0 +1,86 @@ +# Ported from http://sebastiancelis.com/2009/11/16/zsh-prompt-git-users/ + +function gitprompt_update +{ + unset __CURRENT_GIT_BRANCH + unset __CURRENT_GIT_BRANCH_STATUS + unset __CURRENT_GIT_BRANCH_IS_DIRTY + + local st="$(git status 2>/dev/null)" + if [[ -n "$st" ]]; then + local -a arr + arr=(${(f)st}) + + if [[ $arr[1] =~ 'Not currently on any branch.' ]]; then + __CURRENT_GIT_BRANCH='no-branch' + else + __CURRENT_GIT_BRANCH="${arr[1][(w)4]}"; + fi + + if [[ $arr[2] =~ 'Your branch is' ]]; then + if [[ $arr[2] =~ 'ahead' ]]; then + __CURRENT_GIT_BRANCH_STATUS='ahead' + elif [[ $arr[2] =~ 'diverged' ]]; then + __CURRENT_GIT_BRANCH_STATUS='diverged' + else + __CURRENT_GIT_BRANCH_STATUS='behind' + fi + fi + + if [[ ! $st =~ 'nothing' ]]; then # Untracked files count as clean + __CURRENT_GIT_BRANCH_IS_DIRTY='1' + fi + fi +} + +# Changed around the formatting here a bunch +function gitprompt +{ + if [ -n "$__CURRENT_GIT_BRANCH" ]; then + local s="%{${fg[yellow]}%}" + if [ -n "$__CURRENT_GIT_BRANCH_IS_DIRTY" ]; then + s+="⚡" + else + s+=":" + fi + s+="$__CURRENT_GIT_BRANCH" + case "$__CURRENT_GIT_BRANCH_STATUS" in + ahead) + s+="↑" + ;; + diverged) + s+="↕" + ;; + behind) + s+="↓" + ;; + esac + + echo $s + fi +} + +function gitprompt_preexec +{ + case "$1" in + g*) # Switched from git* to also detect my short aliases + __EXECUTED_GIT_COMMAND=1 + ;; + esac +} + +function gitprompt_precmd +{ + if [ -n "$__EXECUTED_GIT_COMMAND" ]; then + gitprompt_update + unset __EXECUTED_GIT_COMMAND + fi +} + +typeset -ga preexec_functions +typeset -ga precmd_functions +typeset -ga chpwd_functions + +preexec_functions+='gitprompt_preexec' +precmd_functions+='gitprompt_precmd' +chpwd_functions+='gitprompt_update' -- cgit 1.4.1